From febf0d0c771664f919ba36aa6a9e6fcd33de4d09 Mon Sep 17 00:00:00 2001 From: Alexander Tumin Date: Sun, 31 Jul 2022 11:12:49 +0300 Subject: [PATCH] Optimize list updates, send accounts API calls only with changes --- src/modules/lists.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/modules/lists.js b/src/modules/lists.js index bf741858..58700f41 100644 --- a/src/modules/lists.js +++ b/src/modules/lists.js @@ -57,12 +57,16 @@ const actions = { commit('setList', { id, title }) }, setListAccounts ({ rootState, commit }, { id, accountIds }) { - rootState.api.backendInteractor.addAccountsToList({ id, accountIds }) - rootState.api.backendInteractor.removeAccountsFromList({ - id, - accountIds: rootState.lists.allListsObject[id].accountIds.filter(id => !accountIds.includes(id)) - }) + 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 }) @@ -76,7 +80,7 @@ export const getters = { return state.allListsObject[id].title }, findListAccounts: state => id => { - return state.allListsObject[id].accountIds + return [...state.allListsObject[id].accountIds] } }