2020-03-15 09:47:56 +01:00
|
|
|
import generator, { Entity } from 'megalodon'
|
2019-04-10 13:58:11 +02:00
|
|
|
import { Module, MutationTree, ActionTree } from 'vuex'
|
2019-04-14 16:11:24 +02:00
|
|
|
import { RootState } from '@/store'
|
2019-04-10 13:58:11 +02:00
|
|
|
|
2019-06-06 16:44:50 +02:00
|
|
|
export type AddListMemberState = {
|
2019-05-27 15:56:54 +02:00
|
|
|
modalOpen: boolean
|
2020-03-15 09:47:56 +01:00
|
|
|
accounts: Array<Entity.Account>
|
2019-05-27 15:56:54 +02:00
|
|
|
targetListId: string | null
|
2019-04-10 13:58:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const state = (): AddListMemberState => ({
|
|
|
|
modalOpen: false,
|
|
|
|
accounts: [],
|
|
|
|
targetListId: null
|
|
|
|
})
|
|
|
|
|
|
|
|
export const MUTATION_TYPES = {
|
|
|
|
CHANGE_MODAL: 'changeModal',
|
|
|
|
UPDATE_ACCOUNTS: 'updateAccounts',
|
|
|
|
SET_LIST_ID: 'setListId'
|
|
|
|
}
|
|
|
|
|
|
|
|
const mutations: MutationTree<AddListMemberState> = {
|
|
|
|
[MUTATION_TYPES.CHANGE_MODAL]: (state, value: boolean) => {
|
|
|
|
state.modalOpen = value
|
|
|
|
},
|
2020-03-15 09:47:56 +01:00
|
|
|
[MUTATION_TYPES.UPDATE_ACCOUNTS]: (state, accounts: Array<Entity.Account>) => {
|
2019-04-10 13:58:11 +02:00
|
|
|
state.accounts = accounts
|
|
|
|
},
|
2019-05-27 15:56:54 +02:00
|
|
|
[MUTATION_TYPES.SET_LIST_ID]: (state, id: string) => {
|
2019-04-10 13:58:11 +02:00
|
|
|
state.targetListId = id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-14 16:11:24 +02:00
|
|
|
const actions: ActionTree<AddListMemberState, RootState> = {
|
2019-04-10 13:58:11 +02:00
|
|
|
changeModal: ({ commit }, value: boolean) => {
|
|
|
|
commit(MUTATION_TYPES.CHANGE_MODAL, value)
|
|
|
|
},
|
2020-03-15 09:47:56 +01:00
|
|
|
search: async ({ commit, rootState }, name: string): Promise<Array<Entity.Account>> => {
|
|
|
|
const client = generator(
|
|
|
|
rootState.TimelineSpace.sns,
|
|
|
|
rootState.TimelineSpace.account.baseURL,
|
|
|
|
rootState.TimelineSpace.account.accessToken,
|
2019-10-27 06:41:03 +01:00
|
|
|
rootState.App.userAgent,
|
|
|
|
rootState.App.proxyConfiguration
|
|
|
|
)
|
2020-03-15 09:47:56 +01:00
|
|
|
const res = await client.searchAccount(name, { following: true })
|
2019-04-10 13:58:11 +02:00
|
|
|
commit(MUTATION_TYPES.UPDATE_ACCOUNTS, res.data)
|
|
|
|
return res.data
|
|
|
|
},
|
|
|
|
add: async ({ state, rootState }, account: Account): Promise<{}> => {
|
2020-03-15 09:47:56 +01:00
|
|
|
const client = generator(
|
|
|
|
rootState.TimelineSpace.sns,
|
|
|
|
rootState.TimelineSpace.account.baseURL,
|
|
|
|
rootState.TimelineSpace.account.accessToken,
|
2019-10-27 06:41:03 +01:00
|
|
|
rootState.App.userAgent,
|
|
|
|
rootState.App.proxyConfiguration
|
|
|
|
)
|
2020-03-15 09:47:56 +01:00
|
|
|
const res = await client.addAccountsToList(state.targetListId!, [account.id])
|
2019-04-10 13:58:11 +02:00
|
|
|
return res.data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-14 16:11:24 +02:00
|
|
|
const AddListMember: Module<AddListMemberState, RootState> = {
|
2019-04-10 13:58:11 +02:00
|
|
|
namespaced: true,
|
|
|
|
state: state,
|
|
|
|
mutations: mutations,
|
|
|
|
actions: actions
|
|
|
|
}
|
|
|
|
|
|
|
|
export default AddListMember
|