aboutsummaryrefslogtreecommitdiff
path: root/src/modules/lists.js
diff options
context:
space:
mode:
authorAlexander Tumin <iamtakingiteasy@eientei.org>2022-08-06 17:26:43 +0300
committerAlexander Tumin <iamtakingiteasy@eientei.org>2022-08-06 17:56:54 +0300
commit171f6f08943dd1d87120df3e4894ddcfd5e1d246 (patch)
tree0ee81442d29f9fa166add813dcf81046995593cf /src/modules/lists.js
parent610720f164dc9fcf36f9df33bddec5ac9c654e1e (diff)
Lists implementation
Diffstat (limited to 'src/modules/lists.js')
-rw-r--r--src/modules/lists.js94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/modules/lists.js b/src/modules/lists.js
new file mode 100644
index 00000000..58700f41
--- /dev/null
+++ b/src/modules/lists.js
@@ -0,0 +1,94 @@
+import { remove, find } from 'lodash'
+
+export const defaultState = {
+ allLists: [],
+ allListsObject: {}
+}
+
+export const mutations = {
+ setLists (state, value) {
+ state.allLists = value
+ },
+ setList (state, { id, title }) {
+ if (!state.allListsObject[id]) {
+ state.allListsObject[id] = {}
+ }
+ state.allListsObject[id].title = title
+
+ if (!find(state.allLists, { id })) {
+ state.allLists.push({ id, title })
+ } else {
+ find(state.allLists, { id }).title = title
+ }
+ },
+ setListAccounts (state, { id, accountIds }) {
+ if (!state.allListsObject[id]) {
+ state.allListsObject[id] = {}
+ }
+ state.allListsObject[id].accountIds = accountIds
+ },
+ deleteList (state, { id }) {
+ delete state.allListsObject[id]
+ remove(state.allLists, list => list.id === id)
+ }
+}
+
+const actions = {
+ setLists ({ commit }, value) {
+ commit('setLists', value)
+ },
+ createList ({ rootState, commit }, { title }) {
+ return rootState.api.backendInteractor.createList({ title })
+ .then((list) => {
+ commit('setList', { id: list.id, title })
+ return list
+ })
+ },
+ fetchList ({ rootState, commit }, { id }) {
+ return rootState.api.backendInteractor.getList({ id })
+ .then((list) => commit('setList', { id: list.id, title: list.title }))
+ },
+ fetchListAccounts ({ rootState, commit }, { id }) {
+ return rootState.api.backendInteractor.getListAccounts({ id })
+ .then((accountIds) => commit('setListAccounts', { id, accountIds }))
+ },
+ setList ({ rootState, commit }, { id, title }) {
+ rootState.api.backendInteractor.updateList({ id, title })
+ commit('setList', { id, title })
+ },
+ setListAccounts ({ rootState, commit }, { id, accountIds }) {
+ const saved = rootState.lists.allListsObject[id].accountIds
+ const added = accountIds.filter(id => !saved.includes(id))
+ const removed = saved.filter(id => !accountIds.includes(id))
+ commit('setListAccounts', { id, accountIds })
+ if (added.length > 0) {
+ rootState.api.backendInteractor.addAccountsToList({ id, accountIds: added })
+ }
+ if (removed.length > 0) {
+ rootState.api.backendInteractor.removeAccountsFromList({ id, accountIds: removed })
+ }
+ },
+ deleteList ({ rootState, commit }, { id }) {
+ rootState.api.backendInteractor.deleteList({ id })
+ commit('deleteList', { id })
+ }
+}
+
+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