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()
|
return data ? Map(data) : Map()
|
||||||
}
|
}
|
||||||
|
|
||||||
export const removeOwners = async (safeAddress: string): Map<string, string> => {
|
export const removeOwners = async (safeAddress: string) => {
|
||||||
try {
|
try {
|
||||||
await removeFromStorage(`${OWNERS_KEY}-${safeAddress}`)
|
await removeFromStorage(`${OWNERS_KEY}-${safeAddress}`)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
|
@ -1,18 +1,35 @@
|
||||||
// @flow
|
// @flow
|
||||||
import type { Store, AnyAction } from 'redux'
|
import type { Store, AnyAction } from 'redux'
|
||||||
|
import { List } from 'immutable'
|
||||||
import { ADD_SAFE } from '~/routes/safe/store/actions/addSafe'
|
import { ADD_SAFE } from '~/routes/safe/store/actions/addSafe'
|
||||||
import { UPDATE_SAFE } from '~/routes/safe/store/actions/updateSafe'
|
import { UPDATE_SAFE } from '~/routes/safe/store/actions/updateSafe'
|
||||||
import { REMOVE_SAFE } from '~/routes/safe/store/actions/removeSafe'
|
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 { 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 { safesMapSelector } from '~/routes/safeList/store/selectors'
|
||||||
import { getActiveTokensAddressesForAllSafes } from '~/routes/safe/store/selectors'
|
import { getActiveTokensAddressesForAllSafes } from '~/routes/safe/store/selectors'
|
||||||
import { tokensSelector } from '~/logic/tokens/store/selectors'
|
import { tokensSelector } from '~/logic/tokens/store/selectors'
|
||||||
import type { Token } from '~/logic/tokens/store/model/token'
|
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 { saveActiveTokens } from '~/logic/tokens/utils/tokensStorage'
|
||||||
import { ACTIVATE_TOKEN_FOR_ALL_SAFES } from '~/routes/safe/store/actions/activateTokenForAllSafes'
|
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 safeStorageMware = (store: Store<GlobalState>) => (next: Function) => async (action: AnyAction) => {
|
||||||
const handledAction = next(action)
|
const handledAction = next(action)
|
||||||
|
@ -20,10 +37,14 @@ const safeStorageMware = (store: Store<GlobalState>) => (next: Function) => asyn
|
||||||
if (watchedActions.includes(action.type)) {
|
if (watchedActions.includes(action.type)) {
|
||||||
const state: GlobalState = store.getState()
|
const state: GlobalState = store.getState()
|
||||||
const safes = safesMapSelector(state)
|
const safes = safesMapSelector(state)
|
||||||
saveSafes(safes.toJSON())
|
await saveSafes(safes.toJSON())
|
||||||
|
|
||||||
// recalculate active tokens
|
let owners
|
||||||
if (action.payload.activeTokens || action.type === ACTIVATE_TOKEN_FOR_ALL_SAFES) {
|
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 tokens = tokensSelector(state)
|
||||||
const activeTokenAddresses = getActiveTokensAddressesForAllSafes(state)
|
const activeTokenAddresses = getActiveTokensAddressesForAllSafes(state)
|
||||||
|
|
||||||
|
@ -37,19 +58,53 @@ const safeStorageMware = (store: Store<GlobalState>) => (next: Function) => asyn
|
||||||
|
|
||||||
saveActiveTokens(activeTokens)
|
saveActiveTokens(activeTokens)
|
||||||
}
|
}
|
||||||
|
break
|
||||||
|
|
||||||
if (action.type === ADD_SAFE) {
|
case ADD_SAFE:
|
||||||
const { safe } = action.payload
|
const { safe } = action.payload
|
||||||
setOwners(safe.address, safe.owners)
|
setOwners(safe.address, safe.owners)
|
||||||
} else if (action.type === UPDATE_SAFE) {
|
break
|
||||||
const { address, owners } = action.payload
|
|
||||||
|
|
||||||
if (address && owners) {
|
case UPDATE_SAFE:
|
||||||
setOwners(address, owners)
|
owners = action.payload.owners
|
||||||
|
if (safeAddress && owners) {
|
||||||
|
setOwners(safeAddress, owners)
|
||||||
}
|
}
|
||||||
} else if (action.type === REMOVE_SAFE) {
|
break
|
||||||
const safeAddress = action.payload
|
|
||||||
removeOwners(safeAddress)
|
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