Add setDefaultSafe action

This commit is contained in:
Mikhail Mikheev 2019-09-18 17:08:16 +04:00
parent 699f1f8663
commit 138f462021
4 changed files with 36 additions and 22 deletions

View File

@ -61,7 +61,7 @@ const SafeList = ({ safes, onSafeClick }: SafeListProps) => {
{' '} {' '}
ETH ETH
</Paragraph> </Paragraph>
PIZDOS)) Make default
</ListItem> </ListItem>
</Link> </Link>
<Hairline /> <Hairline />

View File

@ -0,0 +1,8 @@
// @flow
import { createAction } from 'redux-actions'
export const SET_DEFAULT_SAFE = 'SET_DEFAULT_SAFE'
const setDefaultSafe = createAction<string, *>(SET_DEFAULT_SAFE)
export default setDefaultSafe

View File

@ -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 { REMOVE_SAFE_OWNER } from '~/routes/safe/store/actions/removeSafeOwner'
import { REPLACE_SAFE_OWNER } from '~/routes/safe/store/actions/replaceSafeOwner' import { REPLACE_SAFE_OWNER } from '~/routes/safe/store/actions/replaceSafeOwner'
import { EDIT_SAFE_OWNER } from '~/routes/safe/store/actions/editSafeOwner' 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 const SAFE_REDUCER_ID = 'safes'
export type State = Map<string, Safe> type SafeReducerState = Map<string, *>
export const buildSafe = (storedSafe: SafeProps) => { export const buildSafe = (storedSafe: SafeProps) => {
const names = storedSafe.owners.map((owner: OwnerProps) => owner.name) const names = storedSafe.owners.map((owner: OwnerProps) => owner.name)
@ -61,15 +62,15 @@ export const safesInitialState = async (): Promise<State> => {
return safes return safes
} }
export default handleActions<State, *>( export default handleActions<SafeReducerState, *>(
{ {
[UPDATE_SAFE]: (state: State, action: ActionType<Function>): State => { [UPDATE_SAFE]: (state: SafeReducerState, action: ActionType<Function>): SafeReducerState => {
const safe = action.payload const safe = action.payload
const safeAddress = safe.address 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<Function>): State => { [ACTIVATE_TOKEN_FOR_ALL_SAFES]: (state: SafeReducerState, action: ActionType<Function>): SafeReducerState => {
const tokenAddress = action.payload const tokenAddress = action.payload
const newState = state.withMutations((map) => { const newState = state.withMutations((map) => {
@ -77,13 +78,13 @@ export default handleActions<State, *>(
const safeActiveTokens = map.getIn([safeAddress, 'activeTokens']) const safeActiveTokens = map.getIn([safeAddress, 'activeTokens'])
const activeTokens = safeActiveTokens.push(tokenAddress) const activeTokens = safeActiveTokens.push(tokenAddress)
map.update(safeAddress, (prevSafe) => prevSafe.merge({ activeTokens })) map.updateIn(['safes', safeAddress], (prevSafe) => prevSafe.merge({ activeTokens }))
}) })
}) })
return newState return newState
}, },
[ADD_SAFE]: (state: State, action: ActionType<Function>): State => { [ADD_SAFE]: (state: SafeReducerState, action: ActionType<Function>): SafeReducerState => {
const { safe }: { safe: SafeProps } = action.payload const { safe }: { safe: SafeProps } = action.payload
// if you add a new safe it needs to be set as a record // if you add a new safe it needs to be set as a record
@ -91,45 +92,45 @@ export default handleActions<State, *>(
// with initial props and it would overwrite existing ones // with initial props and it would overwrite existing ones
if (state.has(safe.address)) { 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<Function>): State => { [REMOVE_SAFE]: (state: SafeReducerState, action: ActionType<Function>): SafeReducerState => {
const safeAddress = action.payload const safeAddress = action.payload
return state.delete(safeAddress) return state.deleteIn(['safes', safeAddress])
}, },
[ADD_SAFE_OWNER]: (state: State, action: ActionType<Function>): State => { [ADD_SAFE_OWNER]: (state: SafeReducerState, action: ActionType<Function>): SafeReducerState => {
const { safeAddress, ownerName, ownerAddress } = action.payload 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 })), owners: prevSafe.owners.push(makeOwner({ address: ownerAddress, name: ownerName })),
})) }))
}, },
[REMOVE_SAFE_OWNER]: (state: State, action: ActionType<Function>): State => { [REMOVE_SAFE_OWNER]: (state: SafeReducerState, action: ActionType<Function>): SafeReducerState => {
const { safeAddress, ownerAddress } = action.payload 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()), owners: prevSafe.owners.filter((o) => o.address.toLowerCase() !== ownerAddress.toLowerCase()),
})) }))
}, },
[REPLACE_SAFE_OWNER]: (state: State, action: ActionType<Function>): State => { [REPLACE_SAFE_OWNER]: (state: SafeReducerState, action: ActionType<Function>): SafeReducerState => {
const { const {
safeAddress, oldOwnerAddress, ownerName, ownerAddress, safeAddress, oldOwnerAddress, ownerName, ownerAddress,
} = action.payload } = action.payload
return state.update(safeAddress, (prevSafe) => prevSafe.merge({ return state.updateIn(['safes', safeAddress], (prevSafe) => prevSafe.merge({
owners: prevSafe.owners owners: prevSafe.owners
.filter((o) => o.address.toLowerCase() !== oldOwnerAddress.toLowerCase()) .filter((o) => o.address.toLowerCase() !== oldOwnerAddress.toLowerCase())
.push(makeOwner({ address: ownerAddress, name: ownerName })), .push(makeOwner({ address: ownerAddress, name: ownerName })),
})) }))
}, },
[EDIT_SAFE_OWNER]: (state: State, action: ActionType<Function>): State => { [EDIT_SAFE_OWNER]: (state: SafeReducerState, action: ActionType<Function>): SafeReducerState => {
const { safeAddress, ownerAddress, ownerName } = action.payload const { safeAddress, ownerAddress, ownerName } = action.payload
return state.update(safeAddress, (prevSafe) => { return state.updateIn(['safes', safeAddress], (prevSafe) => {
const ownerToUpdateIndex = prevSafe.owners.findIndex( const ownerToUpdateIndex = prevSafe.owners.findIndex(
(o) => o.address.toLowerCase() === ownerAddress.toLowerCase(), (o) => o.address.toLowerCase() === ownerAddress.toLowerCase(),
) )
@ -137,6 +138,11 @@ export default handleActions<State, *>(
return prevSafe.merge({ owners: updatedOwners }) return prevSafe.merge({ owners: updatedOwners })
}) })
}, },
[SET_DEFAULT_SAFE]: (state: SafeReducerState, action: ActionType<Function>): SafeReducerState => state.set('defaultSafe', action.payload),
}, },
Map(), Map({
// $FlowFixMe
defaultSafe: '',
safes: Map(),
}),
) )

View File

@ -5,7 +5,7 @@ import { type GlobalState } from '~/store/index'
import { type Safe } from '~/routes/safe/store/models/safe' import { type Safe } from '~/routes/safe/store/models/safe'
import { SAFE_REDUCER_ID } from '~/routes/safe/store/reducer/safe' import { SAFE_REDUCER_ID } from '~/routes/safe/store/reducer/safe'
export const safesMapSelector = (state: GlobalState): Map<string, Safe> => state[SAFE_REDUCER_ID] export const safesMapSelector = (state: GlobalState): Map<string, Safe> => state[SAFE_REDUCER_ID].get('safes')
export const safesListSelector: Selector<GlobalState, {}, List<Safe>> = createSelector( export const safesListSelector: Selector<GlobalState, {}, List<Safe>> = createSelector(
safesMapSelector, safesMapSelector,