Handle owner management actions in safe reducer

This commit is contained in:
Germán Martínez 2019-07-12 17:57:37 +02:00
parent 4c483cdd5d
commit f5a4381170
1 changed files with 41 additions and 1 deletions

View File

@ -4,12 +4,16 @@ import { handleActions, type ActionType } from 'redux-actions'
import { ADD_SAFE, buildOwnersFrom } from '~/routes/safe/store/actions/addSafe' import { ADD_SAFE, buildOwnersFrom } from '~/routes/safe/store/actions/addSafe'
import SafeRecord, { type Safe, type SafeProps } from '~/routes/safe/store/models/safe' import SafeRecord, { type Safe, type SafeProps } from '~/routes/safe/store/models/safe'
import TokenBalance from '~/routes/safe/store/models/tokenBalance' import TokenBalance from '~/routes/safe/store/models/tokenBalance'
import { type OwnerProps } from '~/routes/safe/store/models/owner' import { makeOwner, type OwnerProps } from '~/routes/safe/store/models/owner'
import { loadFromStorage } from '~/utils/storage' import { loadFromStorage } from '~/utils/storage'
import { SAFES_KEY } from '~/logic/safe/utils' import { SAFES_KEY } from '~/logic/safe/utils'
import { UPDATE_SAFE } from '~/routes/safe/store/actions/updateSafe' import { UPDATE_SAFE } from '~/routes/safe/store/actions/updateSafe'
import { ACTIVATE_TOKEN_FOR_ALL_SAFES } from '~/routes/safe/store/actions/activateTokenForAllSafes' import { ACTIVATE_TOKEN_FOR_ALL_SAFES } from '~/routes/safe/store/actions/activateTokenForAllSafes'
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'
export const SAFE_REDUCER_ID = 'safes' export const SAFE_REDUCER_ID = 'safes'
@ -97,6 +101,42 @@ export default handleActions<State, *>(
return state.delete(safeAddress) return state.delete(safeAddress)
}, },
[ADD_SAFE_OWNER]: (state: State, action: ActionType<Function>): State => {
const { safeAddress, ownerName, ownerAddress } = action.payload
return state.update(safeAddress, prevSafe => prevSafe.merge({
owners: prevSafe.owners.push(makeOwner({ address: ownerAddress, name: ownerName })),
}))
},
[REMOVE_SAFE_OWNER]: (state: State, action: ActionType<Function>): State => {
const { safeAddress, ownerAddress } = action.payload
return state.update(safeAddress, prevSafe => prevSafe.merge({
owners: prevSafe.owners.filter(o => o.address.toLowerCase() !== ownerAddress.toLowerCase()),
}))
},
[REPLACE_SAFE_OWNER]: (state: State, action: ActionType<Function>): State => {
const {
safeAddress, oldOwnerAddress, ownerName, ownerAddress,
} = action.payload
return state.update(safeAddress, prevSafe => prevSafe.merge({
owners: prevSafe.owners
.filter(o => o.address.toLowerCase() !== oldOwnerAddress.toLowerCase())
.push(makeOwner({ address: ownerAddress, name: ownerName })),
}))
},
[EDIT_SAFE_OWNER]: (state: State, action: ActionType<Function>): State => {
const { safeAddress, ownerAddress, ownerName } = action.payload
return state.update(safeAddress, (prevSafe) => {
const ownerToUpdateIndex = prevSafe.owners.findIndex(
o => o.address.toLowerCase() === ownerAddress.toLowerCase(),
)
const updatedOwners = prevSafe.owners.update(ownerToUpdateIndex, owner => owner.set('name', ownerName))
return prevSafe.merge({ owners: updatedOwners })
})
},
}, },
Map(), Map(),
) )