From e4217ebbaf67beef9ebbdefdeb017e13f9a0789e Mon Sep 17 00:00:00 2001 From: Mikhail Mikheev Date: Tue, 10 Sep 2019 17:31:02 +0400 Subject: [PATCH] Fix condition for recalculating active tokens --- src/logic/tokens/utils/tokensStorage.js | 2 +- .../safe/store/middleware/safeStorage.js | 45 ++++++++++--------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/logic/tokens/utils/tokensStorage.js b/src/logic/tokens/utils/tokensStorage.js index b2b4a3b0..7cd06968 100644 --- a/src/logic/tokens/utils/tokensStorage.js +++ b/src/logic/tokens/utils/tokensStorage.js @@ -56,7 +56,7 @@ export const removeTokenFromStorage = async (safeAddress: string, token: Token) export const removeFromActiveTokens = async (safeAddress: string, token: Token) => { const activeTokens = await getActiveTokens() - const index = activeTokens.findIndex(activeToken => activeToken.name === token.name) + const index = activeTokens.findIndex((activeToken) => activeToken.name === token.name) if (index !== -1) { await saveActiveTokens(safeAddress, activeTokens.delete(index)) diff --git a/src/routes/safe/store/middleware/safeStorage.js b/src/routes/safe/store/middleware/safeStorage.js index c26a8837..3a3e8766 100644 --- a/src/routes/safe/store/middleware/safeStorage.js +++ b/src/routes/safe/store/middleware/safeStorage.js @@ -1,5 +1,6 @@ // @flow import type { Store, AnyAction } from 'redux' +import { List } from 'immutable' import { ADD_SAFE } from '~/routes/safe/store/actions/addSafe' import { UPDATE_SAFE } from '~/routes/safe/store/actions/updateSafe' import { REMOVE_SAFE } from '~/routes/safe/store/actions/removeSafe' @@ -28,6 +29,21 @@ const watchedActions = [ ACTIVATE_TOKEN_FOR_ALL_SAFES, ] +const recalculateActiveTokens = (state: GlobalState): void => { + const tokens = tokensSelector(state) + const activeTokenAddresses = getActiveTokensAddressesForAllSafes(state) + + const activeTokens: List = tokens.withMutations((map) => { + map.forEach((token: Token) => { + if (!activeTokenAddresses.has(token.address)) { + map.remove(token.address) + } + }) + }) + + saveActiveTokens(activeTokens) +} + const safeStorageMware = (store: Store) => (next: Function) => async (action: AnyAction) => { const handledAction = next(action) @@ -38,21 +54,7 @@ const safeStorageMware = (store: Store) => (next: Function) => asyn switch (action.type) { case ACTIVATE_TOKEN_FOR_ALL_SAFES: { - let { activeTokens } = action.payload - if (activeTokens) { - const tokens = tokensSelector(state) - const activeTokenAddresses = getActiveTokensAddressesForAllSafes(state) - - activeTokens = tokens.withMutations((map) => { - map.forEach((token: Token) => { - if (!activeTokenAddresses.has(token.address)) { - map.remove(token.address) - } - }) - }) - - saveActiveTokens(activeTokens) - } + recalculateActiveTokens(state) break } case ADD_SAFE: { @@ -61,10 +63,13 @@ const safeStorageMware = (store: Store) => (next: Function) => asyn break } case UPDATE_SAFE: { - const { safeAddress, owners } = action.payload + const { safeAddress, owners, activeTokens } = action.payload if (safeAddress && owners) { setOwners(safeAddress, owners) } + if (activeTokens) { + recalculateActiveTokens(state) + } break } case REMOVE_SAFE: { @@ -81,7 +86,7 @@ const safeStorageMware = (store: Store) => (next: Function) => asyn case REMOVE_SAFE_OWNER: { const { safeAddress, ownerAddress } = action.payload const { owners } = safes.get(safeAddress) - setOwners(safeAddress, owners.filter(o => o.address.toLowerCase() !== ownerAddress.toLowerCase())) + setOwners(safeAddress, owners.filter((o) => o.address.toLowerCase() !== ownerAddress.toLowerCase())) break } case REPLACE_SAFE_OWNER: { @@ -92,7 +97,7 @@ const safeStorageMware = (store: Store) => (next: Function) => asyn setOwners( safeAddress, owners - .filter(o => o.address.toLowerCase() !== oldOwnerAddress.toLowerCase()) + .filter((o) => o.address.toLowerCase() !== oldOwnerAddress.toLowerCase()) .push(makeOwner({ address: ownerAddress, name: ownerName })), ) break @@ -100,8 +105,8 @@ const safeStorageMware = (store: Store) => (next: Function) => asyn case EDIT_SAFE_OWNER: { const { safeAddress, ownerAddress, ownerName } = action.payload const { owners } = safes.get(safeAddress) - const ownerToUpdateIndex = owners.findIndex(o => o.address.toLowerCase() === ownerAddress.toLowerCase()) - setOwners(safeAddress, owners.update(ownerToUpdateIndex, owner => owner.set('name', ownerName))) + const ownerToUpdateIndex = owners.findIndex((o) => o.address.toLowerCase() === ownerAddress.toLowerCase()) + setOwners(safeAddress, owners.update(ownerToUpdateIndex, (owner) => owner.set('name', ownerName))) break } default: