refactor add safe action

This commit is contained in:
Mikhail Mikheev 2019-04-01 15:06:37 +04:00
parent 477221d415
commit 22d258bacc
6 changed files with 20 additions and 14 deletions

View File

@ -4,6 +4,9 @@ import { createAction } from 'redux-actions'
import { type Safe, makeSafe } from '~/routes/safe/store/model/safe' import { type Safe, makeSafe } from '~/routes/safe/store/model/safe'
import { saveSafes, setOwners } from '~/utils/localStorage' import { saveSafes, setOwners } from '~/utils/localStorage'
import { makeOwner, type Owner } from '~/routes/safe/store/model/owner' import { makeOwner, type Owner } from '~/routes/safe/store/model/owner'
import type { Dispatch as ReduxDispatch, GetState } from 'redux'
import { type GlobalState } from '~/store/index'
import { safesMapSelector } from '~/routes/safeList/store/selectors/index';
export const ADD_SAFE = 'ADD_SAFE' export const ADD_SAFE = 'ADD_SAFE'
@ -30,8 +33,9 @@ const saveSafe = (
threshold: number, threshold: number,
ownersName: string[], ownersName: string[],
ownersAddress: string[], ownersAddress: string[],
) => async (dispatch: ReduxDispatch<GlobalState>) => { ) => async (dispatch: ReduxDispatch<GlobalState>, getState: GetState<GlobalState> ) => {
const owners: List<Owner> = buildOwnersFrom(ownersName, ownersAddress) const owners: List<Owner> = buildOwnersFrom(ownersName, ownersAddress)
const state: GlobalState = getState()
const safe: Safe = makeSafe({ const safe: Safe = makeSafe({
name, name,
@ -39,8 +43,13 @@ const saveSafe = (
threshold, threshold,
owners, owners,
}) })
setOwners(safe.address, safe.owners) const safes = safesMapSelector(state)
saveSafes(safes.toJSON()) const newSafes = safes.set(address, safe)
setOwners(address, owners)
saveSafes(newSafes.toJSON())
dispatch(addSafe(safe))
} }
export default addSafe export default saveSafe

View File

@ -3,4 +3,4 @@ import { createAction } from 'redux-actions'
export const ADD_TRANSACTIONS = 'ADD_TRANSACTIONS' export const ADD_TRANSACTIONS = 'ADD_TRANSACTIONS'
export default createAction(ADD_TRANSACTIONS) export default createAction<string, *>(ADD_TRANSACTIONS)

View File

@ -3,6 +3,6 @@ import { createAction } from 'redux-actions'
export const UPDATE_SAFE = 'UPDATE_SAFE' export const UPDATE_SAFE = 'UPDATE_SAFE'
const updateSafe = createAction(UPDATE_SAFE) const updateSafe = createAction<string, *>(UPDATE_SAFE)
export default updateSafe export default updateSafe

View File

@ -3,6 +3,6 @@ import { createAction } from 'redux-actions'
export const UPDATE_SAFES = 'UPDATE_SAFES' export const UPDATE_SAFES = 'UPDATE_SAFES'
const updateSafesInBatch = createAction(UPDATE_SAFES) const updateSafesInBatch = createAction<string, *>(UPDATE_SAFES)
export default updateSafesInBatch export default updateSafesInBatch

View File

@ -67,13 +67,9 @@ export default handleActions<State, *>(
return state.set(safeAddress, safe) return state.set(safeAddress, safe)
}, },
[ADD_SAFE]: (state: State, action: ActionType<typeof addSafe>): State => { [ADD_SAFE]: (state: State, action: ActionType<typeof addSafe>): State => {
const safe: Safe = makeSafe(action.payload) const { safe }: { safe: Safe } = action.payload
setOwners(safe.get('address'), safe.get('owners'))
const safes = state.set(action.payload.address, safe) return state.set(safe.address, safe)
saveSafes(safes.toJSON())
return state.set(action.payload.address, safe)
}, },
}, },
Map(), Map(),

View File

@ -6,8 +6,9 @@ import { type Safe } from '~/routes/safe/store/model/safe'
import { userAccountSelector } from '~/logic/wallets/store/selectors' import { userAccountSelector } from '~/logic/wallets/store/selectors'
import { type Owner } from '~/routes/safe/store/model/owner' import { type Owner } from '~/routes/safe/store/model/owner'
import { sameAddress } from '~/logic/wallets/ethAddresses' import { sameAddress } from '~/logic/wallets/ethAddresses'
import { SAFE_REDUCER_ID } from '~/routes/safe/store/reducer/safe'
export const safesMapSelector = (state: GlobalState): Map<string, Safe> => state.safes export const safesMapSelector = (state: GlobalState): Map<string, Safe> => state[SAFE_REDUCER_ID]
const safesListSelector: Selector<GlobalState, {}, List<Safe>> = createSelector( const safesListSelector: Selector<GlobalState, {}, List<Safe>> = createSelector(
safesMapSelector, safesMapSelector,