2020-03-15 09:47:56 +01:00
|
|
|
import generator, { Entity } from 'megalodon'
|
2019-04-14 13:32:48 +02:00
|
|
|
import { Module, MutationTree, ActionTree } from 'vuex'
|
|
|
|
import { RootState } from '@/store'
|
2019-04-14 16:25:10 +02:00
|
|
|
import { RemoveAccountFromList } from '@/types/removeAccountFromList'
|
2019-04-14 13:32:48 +02:00
|
|
|
|
2019-06-06 16:44:50 +02:00
|
|
|
export type EditState = {
|
2020-03-15 09:47:56 +01:00
|
|
|
members: Array<Entity.Account>
|
2019-04-14 13:32:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const state = (): EditState => ({
|
|
|
|
members: []
|
|
|
|
})
|
|
|
|
|
|
|
|
export const MUTATION_TYPES = {
|
|
|
|
CHANGE_MEMBERS: 'changeMembers'
|
|
|
|
}
|
|
|
|
|
|
|
|
const mutations: MutationTree<EditState> = {
|
2020-03-15 09:47:56 +01:00
|
|
|
[MUTATION_TYPES.CHANGE_MEMBERS]: (state, members: Array<Entity.Account>) => {
|
2019-04-14 13:32:48 +02:00
|
|
|
state.members = members
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-28 16:25:21 +02:00
|
|
|
export const ACTION_TYPES = {
|
|
|
|
FETCH_MEMBERS: 'fetchMembers',
|
|
|
|
REMOVE_ACCOUNT: 'removeAccount'
|
|
|
|
}
|
|
|
|
|
2019-04-14 13:32:48 +02:00
|
|
|
const actions: ActionTree<EditState, RootState> = {
|
2022-04-28 16:25:21 +02:00
|
|
|
[ACTION_TYPES.FETCH_MEMBERS]: async ({ commit, rootState }, listId: string): Promise<Array<Entity.Account>> => {
|
2020-03-15 09:47:56 +01:00
|
|
|
const client = generator(
|
|
|
|
rootState.TimelineSpace.sns,
|
|
|
|
rootState.TimelineSpace.account.baseURL,
|
|
|
|
rootState.TimelineSpace.account.accessToken,
|
2020-06-30 17:07:55 +02:00
|
|
|
rootState.App.userAgent
|
2019-10-27 06:41:03 +01:00
|
|
|
)
|
2020-03-15 09:47:56 +01:00
|
|
|
const res = await client.getAccountsInList(listId, { limit: 0 })
|
2019-04-14 13:32:48 +02:00
|
|
|
commit(MUTATION_TYPES.CHANGE_MEMBERS, res.data)
|
|
|
|
return res.data
|
|
|
|
},
|
2022-04-28 16:25:21 +02:00
|
|
|
[ACTION_TYPES.REMOVE_ACCOUNT]: async ({ rootState }, remove: RemoveAccountFromList): Promise<{}> => {
|
2020-03-15 09:47:56 +01:00
|
|
|
const client = generator(
|
|
|
|
rootState.TimelineSpace.sns,
|
|
|
|
rootState.TimelineSpace.account.baseURL,
|
|
|
|
rootState.TimelineSpace.account.accessToken,
|
2020-06-30 17:07:55 +02:00
|
|
|
rootState.App.userAgent
|
2019-10-27 06:41:03 +01:00
|
|
|
)
|
2020-03-15 09:47:56 +01:00
|
|
|
return client.deleteAccountsFromList(remove.listId, [remove.account.id])
|
2019-04-14 13:32:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
state: state,
|
|
|
|
mutations: mutations,
|
|
|
|
actions: actions
|
|
|
|
} as Module<EditState, RootState>
|