refs #850 Replace Followers in SideBar with typescript

This commit is contained in:
AkiraFukushima 2019-04-12 00:42:18 +09:00
parent 864da847c7
commit c29c538eb2
3 changed files with 64 additions and 48 deletions

View File

@ -1,7 +1,7 @@
import Mastodon, { Account, Relationship, Response, Results } from 'megalodon'
import Timeline from './AccountProfile/Timeline'
import Follows from './AccountProfile/Follows'
import Followers from './AccountProfile/Followers'
import Followers, { FollowersState } from './AccountProfile/Followers'
import { Module, MutationTree, ActionTree } from 'vuex'
import { RootState } from '@/store'
@ -11,7 +11,9 @@ export interface AccountProfileState {
loading: boolean
}
export interface AccountProfileModuleState extends AccountProfileState {}
export interface AccountProfileModuleState extends AccountProfileState {
Followers: FollowersState
}
const state = (): AccountProfileState => ({
account: null,

View File

@ -1,46 +0,0 @@
import Mastodon from 'megalodon'
const Followers = {
namespaced: true,
state: {
followers: [],
relationships: []
},
mutations: {
updateFollowers (state, users) {
state.followers = users
},
updateRelationships (state, relations) {
state.relationships = relations
}
},
actions: {
fetchFollowers ({ commit, rootState }, account) {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get(`/accounts/${account.id}/followers`, { limit: 80 })
.then(res => {
commit('updateFollowers', res.data)
return res.data
})
},
fetchRelationships ({ commit, rootState }, accounts) {
const ids = accounts.map(a => a.id)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get(`/accounts/relationships`, {
id: ids
})
.then(res => {
commit('updateRelationships', res.data)
return res.data
})
}
}
}
export default Followers

View File

@ -0,0 +1,60 @@
import Mastodon, { Account, Relationship, Response } from 'megalodon'
import { Module, MutationTree, ActionTree } from 'vuex'
import { RootState } from '@/store'
export interface FollowersState {
followers: Array<Account>,
relationships: Array<Relationship>
}
const state = (): FollowersState => ({
followers: [],
relationships: []
})
export const MUTATION_TYPES = {
UPDATE_FOLLOWERS: 'updateFollowers',
UPDATE_RELATIONSHIPS: 'updateRelationships'
}
const mutations: MutationTree<FollowersState> = {
[MUTATION_TYPES.UPDATE_FOLLOWERS]: (state, users: Array<Account>) => {
state.followers = users
},
[MUTATION_TYPES.UPDATE_RELATIONSHIPS]: (state, relations: Array<Relationship>) => {
state.relationships = relations
}
}
const actions: ActionTree<FollowersState, RootState> = {
fetchFollowers: async ({ commit, rootState }, account: Account) => {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const res: Response<Array<Account>> = await client.get<Array<Account>>(`/accounts/${account.id}/followers`, { limit: 80 })
commit(MUTATION_TYPES.UPDATE_FOLLOWERS, res.data)
return res.data
},
fetchRelationships: async ({ commit, rootState }, accounts: Array<Account>) => {
const ids = accounts.map(a => a.id)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const res: Response<Array<Relationship>> = await client.get<Array<Relationship>>(`/accounts/relationships`, {
id: ids
})
commit(MUTATION_TYPES.UPDATE_RELATIONSHIPS, res.data)
return res.data
}
}
const Followers: Module<FollowersState, RootState> = {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
}
export default Followers