commit: 963e1638581a0ea693b6ec6fac202750f59ae502
parent 8eff0814681190619acf1a185f5f429c81ee4479
Author: Sean King <seanking2919@protonmail.com>
Date: Thu, 6 Apr 2023 22:17:03 -0600
Slightly refactor lists store
Diffstat:
M | src/stores/lists.js | 208 | ++++++++++++++++++++++++++++++++++++++----------------------------------------- |
1 file changed, 101 insertions(+), 107 deletions(-)
diff --git a/src/stores/lists.js b/src/stores/lists.js
@@ -2,115 +2,109 @@ import { defineStore } from 'pinia'
import { remove, find } from 'lodash'
-export const defaultState = {
- allLists: [],
- allListsObject: {}
-}
-
-export const getters = {
- findListTitle (state) {
- return (id) => {
- if (!this.allListsObject[id]) return
- return this.allListsObject[id].title
- }
- },
- findListAccounts (state) {
- return (id) => [...this.allListsObject[id].accountIds]
- }
-}
-
-export const actions = {
- setLists (value) {
- this.allLists = value
- },
- createList ({ title }) {
- return window.vuex.state.api.backendInteractor.createList({ title })
- .then((list) => {
- this.setList({ listId: list.id, title })
- return list
- })
- },
- fetchList ({ listId }) {
- return window.vuex.state.api.backendInteractor.getList({ listId })
- .then((list) => this.setList({ listId: list.id, title: list.title }))
- },
- fetchListAccounts ({ listId }) {
- return window.vuex.state.api.backendInteractor.getListAccounts({ listId })
- .then((accountIds) => {
- if (!this.allListsObject[listId]) {
- this.allListsObject[listId] = { accountIds: [] }
- }
- this.allListsObject[listId].accountIds = accountIds
- })
- },
- setList ({ listId, title }) {
- if (!this.allListsObject[listId]) {
- this.allListsObject[listId] = { accountIds: [] }
- }
- this.allListsObject[listId].title = title
-
- const entry = find(this.allLists, { id: listId })
- if (!entry) {
- this.allLists.push({ id: listId, title })
- } else {
- entry.title = title
+export const useListsStore = defineStore('lists', {
+ state: () => ({
+ allLists: [],
+ allListsObject: {}
+ }),
+ getters: {
+ findListTitle (state) {
+ return (id) => {
+ if (!this.allListsObject[id]) return
+ return this.allListsObject[id].title
+ }
+ },
+ findListAccounts (state) {
+ return (id) => [...this.allListsObject[id].accountIds]
}
},
- setListAccounts ({ listId, accountIds }) {
- const saved = this.allListsObject[listId].accountIds || []
- const added = accountIds.filter(id => !saved.includes(id))
- const removed = saved.filter(id => !accountIds.includes(id))
- if (!this.allListsObject[listId]) {
- this.allListsObject[listId] = { accountIds: [] }
- }
- this.allListsObject[listId].accountIds = accountIds
- if (added.length > 0) {
- window.vuex.state.api.backendInteractor.addAccountsToList({ listId, accountIds: added })
- }
- if (removed.length > 0) {
- window.vuex.state.api.backendInteractor.removeAccountsFromList({ listId, accountIds: removed })
+ actions: {
+ setLists (value) {
+ this.allLists = value
+ },
+ createList ({ title }) {
+ return window.vuex.state.api.backendInteractor.createList({ title })
+ .then((list) => {
+ this.setList({ listId: list.id, title })
+ return list
+ })
+ },
+ fetchList ({ listId }) {
+ return window.vuex.state.api.backendInteractor.getList({ listId })
+ .then((list) => this.setList({ listId: list.id, title: list.title }))
+ },
+ fetchListAccounts ({ listId }) {
+ return window.vuex.state.api.backendInteractor.getListAccounts({ listId })
+ .then((accountIds) => {
+ if (!this.allListsObject[listId]) {
+ this.allListsObject[listId] = { accountIds: [] }
+ }
+ this.allListsObject[listId].accountIds = accountIds
+ })
+ },
+ setList ({ listId, title }) {
+ if (!this.allListsObject[listId]) {
+ this.allListsObject[listId] = { accountIds: [] }
+ }
+ this.allListsObject[listId].title = title
+
+ const entry = find(this.allLists, { id: listId })
+ if (!entry) {
+ this.allLists.push({ id: listId, title })
+ } else {
+ entry.title = title
+ }
+ },
+ setListAccounts ({ listId, accountIds }) {
+ const saved = this.allListsObject[listId].accountIds || []
+ const added = accountIds.filter(id => !saved.includes(id))
+ const removed = saved.filter(id => !accountIds.includes(id))
+ if (!this.allListsObject[listId]) {
+ this.allListsObject[listId] = { accountIds: [] }
+ }
+ this.allListsObject[listId].accountIds = accountIds
+ if (added.length > 0) {
+ window.vuex.state.api.backendInteractor.addAccountsToList({ listId, accountIds: added })
+ }
+ if (removed.length > 0) {
+ window.vuex.state.api.backendInteractor.removeAccountsFromList({ listId, accountIds: removed })
+ }
+ },
+ addListAccount ({ listId, accountId }) {
+ return window.vuex.state
+ .api
+ .backendInteractor
+ .addAccountsToList({ listId, accountIds: [accountId] })
+ .then((result) => {
+ if (!this.allListsObject[listId]) {
+ this.allListsObject[listId] = { accountIds: [] }
+ }
+ this.allListsObject[listId].accountIds.push(accountId)
+ return result
+ })
+ },
+ removeListAccount ({ listId, accountId }) {
+ return window.vuex.state
+ .api
+ .backendInteractor
+ .removeAccountsFromList({ listId, accountIds: [accountId] })
+ .then((result) => {
+ if (!this.allListsObject[listId]) {
+ this.allListsObject[listId] = { accountIds: [] }
+ }
+ const { accountIds } = this.allListsObject[listId]
+ const set = new Set(accountIds)
+ set.delete(accountId)
+ this.allListsObject[listId].accountIds = [...set]
+
+ return result
+ })
+ },
+ deleteList ({ listId }) {
+ window.vuex.state.api.backendInteractor.deleteList({ listId })
+
+ delete this.allListsObject[listId]
+ remove(this.allLists, list => list.id === listId)
}
- },
- addListAccount ({ listId, accountId }) {
- return window.vuex.state
- .api
- .backendInteractor
- .addAccountsToList({ listId, accountIds: [accountId] })
- .then((result) => {
- if (!this.allListsObject[listId]) {
- this.allListsObject[listId] = { accountIds: [] }
- }
- this.allListsObject[listId].accountIds.push(accountId)
- return result
- })
- },
- removeListAccount ({ listId, accountId }) {
- return window.vuex.state
- .api
- .backendInteractor
- .removeAccountsFromList({ listId, accountIds: [accountId] })
- .then((result) => {
- if (!this.allListsObject[listId]) {
- this.allListsObject[listId] = { accountIds: [] }
- }
- const { accountIds } = this.allListsObject[listId]
- const set = new Set(accountIds)
- set.delete(accountId)
- this.allListsObject[listId].accountIds = [...set]
-
- return result
- })
- },
- deleteList ({ listId }) {
- window.vuex.state.api.backendInteractor.deleteList({ listId })
-
- delete this.allListsObject[listId]
- remove(this.allLists, list => list.id === listId)
}
-}
-
-export const useListsStore = defineStore('lists', {
- state: () => (defaultState),
- getters,
- actions
})