refs #850 Replace ListMembership with typescript
This commit is contained in:
parent
7c01aa877a
commit
921d3f07e3
|
@ -2003,6 +2003,12 @@
|
|||
"integrity": "sha512-yALhelO3i0hqZwhjtcr6dYyaLoCHbAMshwtj6cGxTvHZAKXHsYGdff6E8EPw3xLKY0ELUTQ69Q1rQiJENnccMA==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/lodash": {
|
||||
"version": "4.14.123",
|
||||
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.123.tgz",
|
||||
"integrity": "sha512-pQvPkc4Nltyx7G1Ww45OjVqUsJP4UsZm+GWJpigXgkikZqJgRm4c48g027o6tdgubWHwFRF15iFd+Y4Pmqv6+Q==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/node": {
|
||||
"version": "11.11.4",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-11.11.4.tgz",
|
||||
|
@ -13466,9 +13472,9 @@
|
|||
"dev": true
|
||||
},
|
||||
"megalodon": {
|
||||
"version": "0.6.2",
|
||||
"resolved": "https://registry.npmjs.org/megalodon/-/megalodon-0.6.2.tgz",
|
||||
"integrity": "sha512-EmNs0M6e2AiX9hutoiXo0FUkghZ1HdyLpS8mVkrMMN8btBR2x1hVrAF/8WAFePeJQrEMYjRyQSEJfykJ/4rwaQ==",
|
||||
"version": "0.6.3",
|
||||
"resolved": "https://registry.npmjs.org/megalodon/-/megalodon-0.6.3.tgz",
|
||||
"integrity": "sha512-rxh9Kbbwm9Hnn/e8xdzH2Fw5kD/TamgyGFEzOcsnKCqF4iI2qHuojCBm7KeWohgRlwJ9oq7QYVReEVTipqI8kQ==",
|
||||
"requires": {
|
||||
"@types/oauth": "0.9.1",
|
||||
"@types/request": "2.48.1",
|
||||
|
|
|
@ -145,7 +145,7 @@
|
|||
"i18next-sync-fs-backend": "^1.1.0",
|
||||
"is-empty": "^1.2.0",
|
||||
"lodash": "^4.17.11",
|
||||
"megalodon": "0.6.2",
|
||||
"megalodon": "0.6.3",
|
||||
"moment": "^2.21.0",
|
||||
"mousetrap": "^1.6.2",
|
||||
"nedb": "^1.8.0",
|
||||
|
@ -173,6 +173,7 @@
|
|||
"@mapbox/stylelint-processor-arbitrary-tags": "^0.2.0",
|
||||
"@types/i18next": "^12.1.0",
|
||||
"@types/jest": "^24.0.11",
|
||||
"@types/lodash": "^4.14.123",
|
||||
"@types/node": "^11.11.4",
|
||||
"@typescript-eslint/eslint-plugin": "^1.5.0",
|
||||
"@typescript-eslint/parser": "^1.5.0",
|
||||
|
|
|
@ -19,7 +19,6 @@ export interface ModalsModuleState extends ModalsState {
|
|||
|
||||
const state = (): ModalsState => ({})
|
||||
|
||||
|
||||
const getters: GetterTree<ModalsState, RootState> = {
|
||||
modalOpened: (_state, _getters, rootState) => {
|
||||
const imageViewer = rootState.TimelineSpace.Modals.ImageViewer.modalOpen
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { Module, MutationTree, ActionTree, GetterTree } from 'vuex'
|
||||
import { Attachment } from 'megalodon'
|
||||
import { RootState } from '@/store';
|
||||
import { RootState } from '@/store'
|
||||
|
||||
export interface ImageViewerState {
|
||||
modalOpen: boolean,
|
||||
|
|
|
@ -1,80 +0,0 @@
|
|||
import Mastodon from 'megalodon'
|
||||
import _ from 'lodash'
|
||||
|
||||
const ListMembership = {
|
||||
namespaced: true,
|
||||
state: {
|
||||
modalOpen: false,
|
||||
account: null,
|
||||
lists: [],
|
||||
belongToLists: []
|
||||
},
|
||||
mutations: {
|
||||
changeModal (state, value) {
|
||||
state.modalOpen = value
|
||||
},
|
||||
changeAccount (state, account) {
|
||||
state.account = account
|
||||
},
|
||||
changeBelongToLists (state, lists) {
|
||||
state.belongToLists = lists
|
||||
},
|
||||
changeLists (state, lists) {
|
||||
state.lists = lists
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
changeModal ({ commit }, value) {
|
||||
commit('changeModal', value)
|
||||
},
|
||||
setAccount ({ commit }, account) {
|
||||
commit('changeAccount', account)
|
||||
},
|
||||
fetchListMembership ({ commit, rootState }, account) {
|
||||
const client = new Mastodon(
|
||||
rootState.TimelineSpace.account.accessToken,
|
||||
rootState.TimelineSpace.account.baseURL + '/api/v1'
|
||||
)
|
||||
return client.get(`/accounts/${account.id}/lists`)
|
||||
.then(res => {
|
||||
commit('changeBelongToLists', res.data.map(l => l.id))
|
||||
return res.data
|
||||
})
|
||||
},
|
||||
fetchLists ({ commit, rootState }) {
|
||||
const client = new Mastodon(
|
||||
rootState.TimelineSpace.account.accessToken,
|
||||
rootState.TimelineSpace.account.baseURL + '/api/v1'
|
||||
)
|
||||
return client.get('/lists')
|
||||
.then(res => {
|
||||
commit('changeLists', res.data)
|
||||
return res.data
|
||||
})
|
||||
},
|
||||
async changeBelongToLists ({ rootState, commit, state }, belongToLists) {
|
||||
// Calcurate diff
|
||||
const removedLists = _.difference(state.belongToLists, belongToLists)
|
||||
const addedLists = _.difference(belongToLists, state.belongToLists)
|
||||
commit('changeBelongToLists', belongToLists)
|
||||
const client = new Mastodon(
|
||||
rootState.TimelineSpace.account.accessToken,
|
||||
rootState.TimelineSpace.account.baseURL + '/api/v1'
|
||||
)
|
||||
const removedPromise = removedLists.map(id => {
|
||||
return client.del(`/lists/${id}/accounts`, {
|
||||
account_ids: [state.account.id]
|
||||
})
|
||||
})
|
||||
const addedPromise = addedLists.map(id => {
|
||||
return client.post(`/lists/${id}/accounts`, {
|
||||
account_ids: [state.account.id]
|
||||
})
|
||||
})
|
||||
const res = await Promise.all(removedPromise.concat(addedPromise))
|
||||
return res
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default ListMembership
|
|
@ -0,0 +1,98 @@
|
|||
import Mastodon, { Account, List, Response } from 'megalodon'
|
||||
import lodash from 'lodash'
|
||||
import { Module, MutationTree, ActionTree } from 'vuex'
|
||||
import { RootState } from '@/store'
|
||||
|
||||
export interface ListMembershipState {
|
||||
modalOpen: boolean,
|
||||
account: Account | null,
|
||||
lists: Array<List>,
|
||||
belongToLists: Array<List>
|
||||
}
|
||||
|
||||
const state = (): ListMembershipState => ({
|
||||
modalOpen: false,
|
||||
account: null,
|
||||
lists: [],
|
||||
belongToLists: []
|
||||
})
|
||||
|
||||
export const MUTATION_TYPES = {
|
||||
CHANGE_MODAL: 'changeModal',
|
||||
CHANGE_ACCOUNT: 'changeAccount',
|
||||
CHANGE_BELONG_TO_LISTS: 'changeBelongToLists',
|
||||
CHANGE_LISTS: 'changeLists'
|
||||
}
|
||||
|
||||
const mutations: MutationTree<ListMembershipState> = {
|
||||
[MUTATION_TYPES.CHANGE_MODAL]: (state, value: boolean) => {
|
||||
state.modalOpen = value
|
||||
},
|
||||
[MUTATION_TYPES.CHANGE_ACCOUNT]: (state, account: Account) => {
|
||||
state.account = account
|
||||
},
|
||||
[MUTATION_TYPES.CHANGE_BELONG_TO_LISTS]: (state, lists: Array<List>) => {
|
||||
state.belongToLists = lists
|
||||
},
|
||||
[MUTATION_TYPES.CHANGE_LISTS]: (state, lists: Array<List>) => {
|
||||
state.lists = lists
|
||||
}
|
||||
}
|
||||
|
||||
const actions: ActionTree<ListMembershipState, RootState> = {
|
||||
changeModal: ({ commit }, value: boolean) => {
|
||||
commit(MUTATION_TYPES.CHANGE_MODAL, value)
|
||||
},
|
||||
setAccount: ({ commit }, account: Account) => {
|
||||
commit(MUTATION_TYPES.CHANGE_ACCOUNT, account)
|
||||
},
|
||||
fetchListMembership: async ({ commit, rootState }, account: Account) => {
|
||||
const client = new Mastodon(
|
||||
rootState.TimelineSpace.account.accessToken!,
|
||||
rootState.TimelineSpace.account.baseURL + '/api/v1'
|
||||
)
|
||||
const res: Response<Array<List>> = await client.get<Array<List>>(`/accounts/${account.id}/lists`)
|
||||
commit(MUTATION_TYPES.CHANGE_BELONG_TO_LISTS, res.data.map(l => l.id))
|
||||
return res.data
|
||||
},
|
||||
fetchLists: async ({ commit, rootState }) => {
|
||||
const client = new Mastodon(
|
||||
rootState.TimelineSpace.account.accessToken!,
|
||||
rootState.TimelineSpace.account.baseURL + '/api/v1'
|
||||
)
|
||||
const res: Response<Array<List>> = await client.get<Array<List>>('/lists')
|
||||
commit(MUTATION_TYPES.CHANGE_LISTS, res.data)
|
||||
return res.data
|
||||
},
|
||||
changeBelongToLists: async ({ rootState, commit, state }, belongToLists: Array<List>) => {
|
||||
// Calcurate diff
|
||||
const removedLists = lodash.difference(state.belongToLists, belongToLists)
|
||||
const addedLists = lodash.difference(belongToLists, state.belongToLists)
|
||||
commit(MUTATION_TYPES.CHANGE_BELONG_TO_LISTS, belongToLists)
|
||||
const client = new Mastodon(
|
||||
rootState.TimelineSpace.account.accessToken!,
|
||||
rootState.TimelineSpace.account.baseURL + '/api/v1'
|
||||
)
|
||||
const removedPromise = removedLists.map(id => {
|
||||
return client.del<{}>(`/lists/${id}/accounts`, {
|
||||
account_ids: [state.account!.id]
|
||||
})
|
||||
})
|
||||
const addedPromise = addedLists.map(id => {
|
||||
return client.post<{}>(`/lists/${id}/accounts`, {
|
||||
account_ids: [state.account!.id]
|
||||
})
|
||||
})
|
||||
const res = await Promise.all(removedPromise.concat(addedPromise))
|
||||
return res
|
||||
}
|
||||
}
|
||||
|
||||
const ListMembership: Module<ListMembershipState, RootState> = {
|
||||
namespaced: true,
|
||||
state: state,
|
||||
mutations: mutations,
|
||||
actions: actions
|
||||
}
|
||||
|
||||
export default ListMembership
|
Loading…
Reference in New Issue