From 967a769b6de0cca2bf1c57f05a1195e31faaa9f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Mart=C3=ADnez?= Date: Fri, 12 Jul 2019 17:52:53 +0200 Subject: [PATCH] Add owner management actions to storage middleware --- src/logic/safe/utils/safeStorage.js | 2 +- .../safe/store/middleware/safeStorage.js | 101 ++++++++++++++---- 2 files changed, 79 insertions(+), 24 deletions(-) diff --git a/src/logic/safe/utils/safeStorage.js b/src/logic/safe/utils/safeStorage.js index 82c45762..88681021 100644 --- a/src/logic/safe/utils/safeStorage.js +++ b/src/logic/safe/utils/safeStorage.js @@ -42,7 +42,7 @@ export const getOwners = async (safeAddress: string): Map => { return data ? Map(data) : Map() } -export const removeOwners = async (safeAddress: string): Map => { +export const removeOwners = async (safeAddress: string) => { try { await removeFromStorage(`${OWNERS_KEY}-${safeAddress}`) } catch (err) { diff --git a/src/routes/safe/store/middleware/safeStorage.js b/src/routes/safe/store/middleware/safeStorage.js index f6dc81aa..445d4cba 100644 --- a/src/routes/safe/store/middleware/safeStorage.js +++ b/src/routes/safe/store/middleware/safeStorage.js @@ -1,18 +1,35 @@ // @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' +import { ADD_SAFE_OWNER } from '~/routes/safe/store/actions/addSafeOwner' +import { REMOVE_SAFE_OWNER } from '~/routes/safe/store/actions/removeSafeOwner' +import { REPLACE_SAFE_OWNER } from '~/routes/safe/store/actions/replaceSafeOwner' +import { EDIT_SAFE_OWNER } from '~/routes/safe/store/actions/editSafeOwner' import { type GlobalState } from '~/store/' -import { saveSafes, setOwners, removeOwners } from '~/logic/safe/utils' +import { + saveSafes, getOwners, setOwners, removeOwners, +} from '~/logic/safe/utils' import { safesMapSelector } from '~/routes/safeList/store/selectors' import { getActiveTokensAddressesForAllSafes } from '~/routes/safe/store/selectors' import { tokensSelector } from '~/logic/tokens/store/selectors' import type { Token } from '~/logic/tokens/store/model/token' +import { makeOwner } from '~/routes/safe/store/models/owner' import { saveActiveTokens } from '~/logic/tokens/utils/tokensStorage' import { ACTIVATE_TOKEN_FOR_ALL_SAFES } from '~/routes/safe/store/actions/activateTokenForAllSafes' -const watchedActions = [ADD_SAFE, UPDATE_SAFE, REMOVE_SAFE, ACTIVATE_TOKEN_FOR_ALL_SAFES] +const watchedActions = [ + ADD_SAFE, + UPDATE_SAFE, + REMOVE_SAFE, + ADD_SAFE_OWNER, + REMOVE_SAFE_OWNER, + REPLACE_SAFE_OWNER, + EDIT_SAFE_OWNER, + ACTIVATE_TOKEN_FOR_ALL_SAFES, +] const safeStorageMware = (store: Store) => (next: Function) => async (action: AnyAction) => { const handledAction = next(action) @@ -20,36 +37,74 @@ const safeStorageMware = (store: Store) => (next: Function) => asyn if (watchedActions.includes(action.type)) { const state: GlobalState = store.getState() const safes = safesMapSelector(state) - saveSafes(safes.toJSON()) + await saveSafes(safes.toJSON()) - // recalculate active tokens - if (action.payload.activeTokens || action.type === ACTIVATE_TOKEN_FOR_ALL_SAFES) { - const tokens = tokensSelector(state) - const activeTokenAddresses = getActiveTokensAddressesForAllSafes(state) + let owners + const { safeAddress, ownerName, ownerAddress } = action.payload + switch (action.type) { + case ACTIVATE_TOKEN_FOR_ALL_SAFES: + const { activeTokens } = action.payload + if (activeTokens) { + const tokens = tokensSelector(state) + const activeTokenAddresses = getActiveTokensAddressesForAllSafes(state) - const activeTokens = tokens.withMutations((map) => { - map.forEach((token: Token) => { - if (!activeTokenAddresses.has(token.address)) { - map.remove(token.address) - } + const activeTokens = tokens.withMutations((map) => { + map.forEach((token: Token) => { + if (!activeTokenAddresses.has(token.address)) { + map.remove(token.address) + } + }) }) - }) - saveActiveTokens(activeTokens) - } + saveActiveTokens(activeTokens) + } + break - if (action.type === ADD_SAFE) { + case ADD_SAFE: const { safe } = action.payload setOwners(safe.address, safe.owners) - } else if (action.type === UPDATE_SAFE) { - const { address, owners } = action.payload + break - if (address && owners) { - setOwners(address, owners) + case UPDATE_SAFE: + owners = action.payload.owners + if (safeAddress && owners) { + setOwners(safeAddress, owners) } - } else if (action.type === REMOVE_SAFE) { - const safeAddress = action.payload - removeOwners(safeAddress) + break + + case REMOVE_SAFE: + await removeOwners(safeAddress) + break + + case ADD_SAFE_OWNER: + owners = List(safes.get(safeAddress).owners) + setOwners(safeAddress, owners.push(makeOwner({ address: ownerAddress, name: ownerName }))) + break + + case REMOVE_SAFE_OWNER: + owners = List(safes.get(safeAddress).owners) + setOwners(safeAddress, owners.filter(o => o.address.toLowerCase() !== ownerAddress.toLowerCase())) + break + + case REPLACE_SAFE_OWNER: + const { oldOwnerAddress } = action.payload + owners = List(safes.get(safeAddress).owners) + setOwners( + safeAddress, + owners + .filter(o => o.address.toLowerCase() !== oldOwnerAddress.toLowerCase()) + .push(makeOwner({ address: ownerAddress, name: ownerName })), + ) + break + + case EDIT_SAFE_OWNER: + owners = List(safes.get(safeAddress).owners) + const ownerToUpdateIndex = owners.findIndex(o => o.address.toLowerCase() === ownerAddress.toLowerCase()) + setOwners(safeAddress, owners.update(ownerToUpdateIndex, owner => owner.set('name', ownerName))) + break + + default: + break } }