Add owner management actions to storage middleware
This commit is contained in:
parent
9564f568c1
commit
967a769b6d
|
@ -42,7 +42,7 @@ export const getOwners = async (safeAddress: string): Map<string, string> => {
|
|||
return data ? Map(data) : Map()
|
||||
}
|
||||
|
||||
export const removeOwners = async (safeAddress: string): Map<string, string> => {
|
||||
export const removeOwners = async (safeAddress: string) => {
|
||||
try {
|
||||
await removeFromStorage(`${OWNERS_KEY}-${safeAddress}`)
|
||||
} catch (err) {
|
||||
|
|
|
@ -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<GlobalState>) => (next: Function) => async (action: AnyAction) => {
|
||||
const handledAction = next(action)
|
||||
|
@ -20,36 +37,74 @@ const safeStorageMware = (store: Store<GlobalState>) => (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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue