From 138f4620217673e7077450c6c8a3d6b848384afb Mon Sep 17 00:00:00 2001 From: Mikhail Mikheev Date: Wed, 18 Sep 2019 17:08:16 +0400 Subject: [PATCH] Add setDefaultSafe action --- src/components/Sidebar/SafeList.jsx | 2 +- .../safe/store/actions/setDefaultSafe.js | 8 ++++ src/routes/safe/store/reducer/safe.js | 46 +++++++++++-------- src/routes/safeList/store/selectors/index.js | 2 +- 4 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 src/routes/safe/store/actions/setDefaultSafe.js diff --git a/src/components/Sidebar/SafeList.jsx b/src/components/Sidebar/SafeList.jsx index 0f8d14bd..74dafe98 100644 --- a/src/components/Sidebar/SafeList.jsx +++ b/src/components/Sidebar/SafeList.jsx @@ -61,7 +61,7 @@ const SafeList = ({ safes, onSafeClick }: SafeListProps) => { {' '} ETH - PIZDOS)) + Make default diff --git a/src/routes/safe/store/actions/setDefaultSafe.js b/src/routes/safe/store/actions/setDefaultSafe.js new file mode 100644 index 00000000..6987a0eb --- /dev/null +++ b/src/routes/safe/store/actions/setDefaultSafe.js @@ -0,0 +1,8 @@ +// @flow +import { createAction } from 'redux-actions' + +export const SET_DEFAULT_SAFE = 'SET_DEFAULT_SAFE' + +const setDefaultSafe = createAction(SET_DEFAULT_SAFE) + +export default setDefaultSafe diff --git a/src/routes/safe/store/reducer/safe.js b/src/routes/safe/store/reducer/safe.js index bc942637..e0a355b6 100644 --- a/src/routes/safe/store/reducer/safe.js +++ b/src/routes/safe/store/reducer/safe.js @@ -14,10 +14,11 @@ 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 { SET_DEFAULT_SAFE } from '~/routes/safe/store/actions/setDefaultSafe' export const SAFE_REDUCER_ID = 'safes' -export type State = Map +type SafeReducerState = Map export const buildSafe = (storedSafe: SafeProps) => { const names = storedSafe.owners.map((owner: OwnerProps) => owner.name) @@ -61,15 +62,15 @@ export const safesInitialState = async (): Promise => { return safes } -export default handleActions( +export default handleActions( { - [UPDATE_SAFE]: (state: State, action: ActionType): State => { + [UPDATE_SAFE]: (state: SafeReducerState, action: ActionType): SafeReducerState => { const safe = action.payload const safeAddress = safe.address - return state.update(safeAddress, (prevSafe) => prevSafe.merge(safe)) + return state.updateIn(['safes', safeAddress], (prevSafe) => prevSafe.merge(safe)) }, - [ACTIVATE_TOKEN_FOR_ALL_SAFES]: (state: State, action: ActionType): State => { + [ACTIVATE_TOKEN_FOR_ALL_SAFES]: (state: SafeReducerState, action: ActionType): SafeReducerState => { const tokenAddress = action.payload const newState = state.withMutations((map) => { @@ -77,13 +78,13 @@ export default handleActions( const safeActiveTokens = map.getIn([safeAddress, 'activeTokens']) const activeTokens = safeActiveTokens.push(tokenAddress) - map.update(safeAddress, (prevSafe) => prevSafe.merge({ activeTokens })) + map.updateIn(['safes', safeAddress], (prevSafe) => prevSafe.merge({ activeTokens })) }) }) return newState }, - [ADD_SAFE]: (state: State, action: ActionType): State => { + [ADD_SAFE]: (state: SafeReducerState, action: ActionType): SafeReducerState => { const { safe }: { safe: SafeProps } = action.payload // if you add a new safe it needs to be set as a record @@ -91,45 +92,45 @@ export default handleActions( // with initial props and it would overwrite existing ones if (state.has(safe.address)) { - return state.update(safe.address, (prevSafe) => prevSafe.merge(safe)) + return state.updateIn(['safes', safe.address], (prevSafe) => prevSafe.merge(safe)) } - return state.set(safe.address, SafeRecord(safe)) + return state.setIn(['safes', safe.address], SafeRecord(safe)) }, - [REMOVE_SAFE]: (state: State, action: ActionType): State => { + [REMOVE_SAFE]: (state: SafeReducerState, action: ActionType): SafeReducerState => { const safeAddress = action.payload - return state.delete(safeAddress) + return state.deleteIn(['safes', safeAddress]) }, - [ADD_SAFE_OWNER]: (state: State, action: ActionType): State => { + [ADD_SAFE_OWNER]: (state: SafeReducerState, action: ActionType): SafeReducerState => { const { safeAddress, ownerName, ownerAddress } = action.payload - return state.update(safeAddress, (prevSafe) => prevSafe.merge({ + return state.updateIn(['safes', safeAddress], (prevSafe) => prevSafe.merge({ owners: prevSafe.owners.push(makeOwner({ address: ownerAddress, name: ownerName })), })) }, - [REMOVE_SAFE_OWNER]: (state: State, action: ActionType): State => { + [REMOVE_SAFE_OWNER]: (state: SafeReducerState, action: ActionType): SafeReducerState => { const { safeAddress, ownerAddress } = action.payload - return state.update(safeAddress, (prevSafe) => prevSafe.merge({ + return state.updateIn(['safes', safeAddress], (prevSafe) => prevSafe.merge({ owners: prevSafe.owners.filter((o) => o.address.toLowerCase() !== ownerAddress.toLowerCase()), })) }, - [REPLACE_SAFE_OWNER]: (state: State, action: ActionType): State => { + [REPLACE_SAFE_OWNER]: (state: SafeReducerState, action: ActionType): SafeReducerState => { const { safeAddress, oldOwnerAddress, ownerName, ownerAddress, } = action.payload - return state.update(safeAddress, (prevSafe) => prevSafe.merge({ + return state.updateIn(['safes', 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): State => { + [EDIT_SAFE_OWNER]: (state: SafeReducerState, action: ActionType): SafeReducerState => { const { safeAddress, ownerAddress, ownerName } = action.payload - return state.update(safeAddress, (prevSafe) => { + return state.updateIn(['safes', safeAddress], (prevSafe) => { const ownerToUpdateIndex = prevSafe.owners.findIndex( (o) => o.address.toLowerCase() === ownerAddress.toLowerCase(), ) @@ -137,6 +138,11 @@ export default handleActions( return prevSafe.merge({ owners: updatedOwners }) }) }, + [SET_DEFAULT_SAFE]: (state: SafeReducerState, action: ActionType): SafeReducerState => state.set('defaultSafe', action.payload), }, - Map(), + Map({ + // $FlowFixMe + defaultSafe: '', + safes: Map(), + }), ) diff --git a/src/routes/safeList/store/selectors/index.js b/src/routes/safeList/store/selectors/index.js index 1f2978fe..4b7960d3 100644 --- a/src/routes/safeList/store/selectors/index.js +++ b/src/routes/safeList/store/selectors/index.js @@ -5,7 +5,7 @@ import { type GlobalState } from '~/store/index' import { type Safe } from '~/routes/safe/store/models/safe' import { SAFE_REDUCER_ID } from '~/routes/safe/store/reducer/safe' -export const safesMapSelector = (state: GlobalState): Map => state[SAFE_REDUCER_ID] +export const safesMapSelector = (state: GlobalState): Map => state[SAFE_REDUCER_ID].get('safes') export const safesListSelector: Selector> = createSelector( safesMapSelector,