2022-08-06 14:26:43 +00:00
|
|
|
import { remove, find } from 'lodash'
|
|
|
|
|
|
|
|
export const defaultState = {
|
|
|
|
allLists: [],
|
|
|
|
allListsObject: {}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const mutations = {
|
|
|
|
setLists (state, value) {
|
|
|
|
state.allLists = value
|
|
|
|
},
|
2022-08-15 20:19:33 +00:00
|
|
|
setList (state, { listId, title }) {
|
|
|
|
if (!state.allListsObject[listId]) {
|
|
|
|
state.allListsObject[listId] = { accountIds: [] }
|
2022-08-06 14:26:43 +00:00
|
|
|
}
|
2022-08-15 20:19:33 +00:00
|
|
|
state.allListsObject[listId].title = title
|
2022-08-06 14:26:43 +00:00
|
|
|
|
2022-08-15 20:31:05 +00:00
|
|
|
if (!find(state.allLists, { id: listId })) {
|
|
|
|
state.allLists.push({ id: listId, title })
|
2022-08-06 14:26:43 +00:00
|
|
|
} else {
|
2022-08-15 20:31:05 +00:00
|
|
|
find(state.allLists, { id: listId }).title = title
|
2022-08-06 14:26:43 +00:00
|
|
|
}
|
|
|
|
},
|
2022-08-15 20:19:33 +00:00
|
|
|
setListAccounts (state, { listId, accountIds }) {
|
|
|
|
if (!state.allListsObject[listId]) {
|
|
|
|
state.allListsObject[listId] = { accountIds: [] }
|
2022-08-06 14:26:43 +00:00
|
|
|
}
|
2022-08-15 20:19:33 +00:00
|
|
|
state.allListsObject[listId].accountIds = accountIds
|
2022-08-06 14:26:43 +00:00
|
|
|
},
|
2022-08-15 20:19:33 +00:00
|
|
|
addListAccount (state, { listId, accountId }) {
|
|
|
|
if (!state.allListsObject[listId]) {
|
|
|
|
state.allListsObject[listId] = { accountIds: [] }
|
|
|
|
}
|
|
|
|
state.allListsObject[listId].accountIds.push(accountId)
|
|
|
|
},
|
|
|
|
removeListAccount (state, { listId, accountId }) {
|
|
|
|
if (!state.allListsObject[listId]) {
|
|
|
|
state.allListsObject[listId] = { accountIds: [] }
|
|
|
|
}
|
|
|
|
const { accountIds } = state.allListsObject[listId]
|
|
|
|
const set = new Set(accountIds)
|
|
|
|
set.delete(accountId)
|
|
|
|
state.allListsObject[listId].accountIds = [...set]
|
|
|
|
},
|
|
|
|
deleteList (state, { listId }) {
|
|
|
|
delete state.allListsObject[listId]
|
|
|
|
remove(state.allLists, list => list.id === listId)
|
2022-08-06 14:26:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const actions = {
|
|
|
|
setLists ({ commit }, value) {
|
|
|
|
commit('setLists', value)
|
|
|
|
},
|
|
|
|
createList ({ rootState, commit }, { title }) {
|
|
|
|
return rootState.api.backendInteractor.createList({ title })
|
|
|
|
.then((list) => {
|
2022-08-15 20:19:33 +00:00
|
|
|
commit('setList', { listId: list.id, title })
|
2022-08-06 14:26:43 +00:00
|
|
|
return list
|
|
|
|
})
|
|
|
|
},
|
2022-08-15 20:19:33 +00:00
|
|
|
fetchList ({ rootState, commit }, { listId }) {
|
|
|
|
return rootState.api.backendInteractor.getList({ listId })
|
2022-08-15 20:31:05 +00:00
|
|
|
.then((list) => commit('setList', { listId: list.id, title: list.title }))
|
2022-08-06 14:26:43 +00:00
|
|
|
},
|
2022-08-15 20:19:33 +00:00
|
|
|
fetchListAccounts ({ rootState, commit }, { listId }) {
|
|
|
|
return rootState.api.backendInteractor.getListAccounts({ listId })
|
|
|
|
.then((accountIds) => commit('setListAccounts', { listId, accountIds }))
|
2022-08-06 14:26:43 +00:00
|
|
|
},
|
2022-08-15 20:19:33 +00:00
|
|
|
setList ({ rootState, commit }, { listId, title }) {
|
|
|
|
rootState.api.backendInteractor.updateList({ listId, title })
|
|
|
|
commit('setList', { listId, title })
|
2022-08-06 14:26:43 +00:00
|
|
|
},
|
2022-08-15 20:19:33 +00:00
|
|
|
setListAccounts ({ rootState, commit }, { listId, accountIds }) {
|
|
|
|
const saved = rootState.lists.allListsObject[listId].accountIds || []
|
2022-08-06 14:26:43 +00:00
|
|
|
const added = accountIds.filter(id => !saved.includes(id))
|
|
|
|
const removed = saved.filter(id => !accountIds.includes(id))
|
2022-08-15 20:19:33 +00:00
|
|
|
commit('setListAccounts', { listId, accountIds })
|
2022-08-06 14:26:43 +00:00
|
|
|
if (added.length > 0) {
|
2022-08-15 20:19:33 +00:00
|
|
|
rootState.api.backendInteractor.addAccountsToList({ listId, accountIds: added })
|
2022-08-06 14:26:43 +00:00
|
|
|
}
|
|
|
|
if (removed.length > 0) {
|
2022-08-15 20:19:33 +00:00
|
|
|
rootState.api.backendInteractor.removeAccountsFromList({ listId, accountIds: removed })
|
2022-08-06 14:26:43 +00:00
|
|
|
}
|
|
|
|
},
|
2022-08-15 20:19:33 +00:00
|
|
|
addListAccount ({ rootState, commit }, { listId, accountId }) {
|
|
|
|
return rootState
|
|
|
|
.api
|
|
|
|
.backendInteractor
|
|
|
|
.addAccountsToList({ listId, accountIds: [accountId] })
|
|
|
|
.then((result) => {
|
|
|
|
commit('addListAccount', { listId, accountId })
|
|
|
|
return result
|
|
|
|
})
|
|
|
|
},
|
|
|
|
removeListAccount ({ rootState, commit }, { listId, accountId }) {
|
|
|
|
return rootState
|
|
|
|
.api
|
|
|
|
.backendInteractor
|
|
|
|
.removeAccountsFromList({ listId, accountIds: [accountId] })
|
|
|
|
.then((result) => {
|
|
|
|
commit('removeListAccount', { listId, accountId })
|
|
|
|
return result
|
|
|
|
})
|
|
|
|
},
|
|
|
|
deleteList ({ rootState, commit }, { listId }) {
|
|
|
|
rootState.api.backendInteractor.deleteList({ listId })
|
|
|
|
commit('deleteList', { listId })
|
2022-08-06 14:26:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getters = {
|
|
|
|
findListTitle: state => id => {
|
|
|
|
if (!state.allListsObject[id]) return
|
|
|
|
return state.allListsObject[id].title
|
|
|
|
},
|
|
|
|
findListAccounts: state => id => {
|
|
|
|
return [...state.allListsObject[id].accountIds]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const lists = {
|
|
|
|
state: defaultState,
|
|
|
|
mutations,
|
|
|
|
actions,
|
|
|
|
getters
|
|
|
|
}
|
|
|
|
|
|
|
|
export default lists
|