rewrite active tokens logic to use updateSafe action
This commit is contained in:
parent
400a19f901
commit
d7127fb968
|
@ -46,7 +46,6 @@ export default (safeAddress: string, update: boolean = false) => async (dispatch
|
||||||
const safeProps: SafeProps = await buildSafe(safeAddress, safeName)
|
const safeProps: SafeProps = await buildSafe(safeAddress, safeName)
|
||||||
|
|
||||||
if (update) {
|
if (update) {
|
||||||
console.log(safeProps)
|
|
||||||
dispatch(updateSafe(safeProps))
|
dispatch(updateSafe(safeProps))
|
||||||
} else {
|
} else {
|
||||||
dispatch(addSafe(safeProps))
|
dispatch(addSafe(safeProps))
|
||||||
|
|
|
@ -1,30 +1,38 @@
|
||||||
// @flow
|
// @flow
|
||||||
import { createAction } from 'redux-actions'
|
import { List } from 'immutable'
|
||||||
import type { Dispatch as ReduxDispatch } from 'redux'
|
import type { Dispatch as ReduxDispatch, GetState } from 'redux'
|
||||||
import { type GlobalState } from '~/store'
|
import { type GlobalState } from '~/store'
|
||||||
import type { SafeToken } from '~/routes/safe/store/models/safeToken'
|
import type { SafeToken } from '~/routes/safe/store/models/safeToken'
|
||||||
import SafeTokenRecord from '~/routes/safe/store/models/safeToken'
|
import SafeTokenRecord from '~/routes/safe/store/models/safeToken'
|
||||||
|
import { safeTokensSelector } from '~/routes/safe/store/selectors/index'
|
||||||
|
import { SAFE_PARAM_ADDRESS } from '~/routes/routes'
|
||||||
|
import updateSafe from './updateSafe'
|
||||||
|
|
||||||
export const UPDATE_SAFE_TOKENS = 'UPDATE_SAFE_TOKENS'
|
const generateMatchProps = (safeAddress: string) => ({
|
||||||
|
match: {
|
||||||
type ActionReturn = {
|
params: {
|
||||||
token: SafeToken,
|
[SAFE_PARAM_ADDRESS]: safeAddress,
|
||||||
}
|
},
|
||||||
|
},
|
||||||
export const updateTokenAction = createAction<string, Function, ActionReturn>(
|
})
|
||||||
UPDATE_SAFE_TOKENS,
|
|
||||||
(safeAddress: string, token: SafeToken): ActionReturn => ({
|
|
||||||
safeAddress,
|
|
||||||
token,
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
|
|
||||||
const updateActiveTokens = (safeAddress: string, tokenAddress: string) => async (
|
const updateActiveTokens = (safeAddress: string, tokenAddress: string) => async (
|
||||||
dispatch: ReduxDispatch<GlobalState>,
|
dispatch: ReduxDispatch<GlobalState>,
|
||||||
|
getState: GetState<GlobalState>,
|
||||||
) => {
|
) => {
|
||||||
const token: SafeToken = SafeTokenRecord({ address: tokenAddress })
|
const token: SafeToken = SafeTokenRecord({ address: tokenAddress })
|
||||||
|
const state = getState()
|
||||||
|
const safeTokens: List<SafeToken> = safeTokensSelector(state, generateMatchProps(safeAddress))
|
||||||
|
const index = safeTokens.findIndex(safeToken => safeToken.address === token.address)
|
||||||
|
|
||||||
dispatch(updateTokenAction(safeAddress, token))
|
let updatedTokens
|
||||||
|
if (index !== -1) {
|
||||||
|
updatedTokens = safeTokens.delete(index)
|
||||||
|
} else {
|
||||||
|
updatedTokens = safeTokens.push(token)
|
||||||
|
}
|
||||||
|
|
||||||
|
dispatch(updateSafe({ address: safeAddress, tokens: updatedTokens }))
|
||||||
}
|
}
|
||||||
|
|
||||||
export default updateActiveTokens
|
export default updateActiveTokens
|
||||||
|
|
|
@ -8,7 +8,6 @@ import type { SafeToken } from '~/routes/safe/store/models/safeToken'
|
||||||
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 { UPDATE_SAFE_TOKENS } from '~/routes/safe/store/actions/updateActiveTokens'
|
|
||||||
|
|
||||||
export const SAFE_REDUCER_ID = 'safes'
|
export const SAFE_REDUCER_ID = 'safes'
|
||||||
|
|
||||||
|
@ -72,21 +71,6 @@ export default handleActions<State, *>(
|
||||||
|
|
||||||
return state.set(safe.address, SafeRecord(safe))
|
return state.set(safe.address, SafeRecord(safe))
|
||||||
},
|
},
|
||||||
[UPDATE_SAFE_TOKENS]: (state: State, action: ActionType<Function>): State => {
|
|
||||||
const { safeAddress, token: updatedToken } = action.payload
|
|
||||||
|
|
||||||
const tokens: List<SafeToken> = state.getIn([safeAddress, 'tokens'])
|
|
||||||
const index = tokens.findIndex(token => token.address === updatedToken.address)
|
|
||||||
|
|
||||||
let newState
|
|
||||||
if (index !== -1) {
|
|
||||||
newState = state.setIn([safeAddress, 'tokens'], tokens.delete(index))
|
|
||||||
} else {
|
|
||||||
newState = state.setIn([safeAddress, 'tokens'], tokens.push(updatedToken))
|
|
||||||
}
|
|
||||||
|
|
||||||
return newState
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
Map(),
|
Map(),
|
||||||
)
|
)
|
||||||
|
|
|
@ -3,9 +3,6 @@ import { List, Map } from 'immutable'
|
||||||
import { createSelector, type Selector } from 'reselect'
|
import { createSelector, type Selector } from 'reselect'
|
||||||
import { type GlobalState } from '~/store/index'
|
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 { userAccountSelector } from '~/logic/wallets/store/selectors'
|
|
||||||
import { type Owner } from '~/routes/safe/store/models/owner'
|
|
||||||
import { sameAddress } from '~/logic/wallets/ethAddresses'
|
|
||||||
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]
|
||||||
|
@ -14,11 +11,3 @@ export const safesListSelector: Selector<GlobalState, {}, List<Safe>> = createSe
|
||||||
safesMapSelector,
|
safesMapSelector,
|
||||||
(safes: Map<string, Safe>): List<Safe> => safes.toList(),
|
(safes: Map<string, Safe>): List<Safe> => safes.toList(),
|
||||||
)
|
)
|
||||||
|
|
||||||
// export const safesByOwnerSelector: Selector<GlobalState, {}, List<Safe>> = createSelector(
|
|
||||||
// userAccountSelector,
|
|
||||||
// safesListSelector,
|
|
||||||
// (userAddress: string, safes: List<Safe>): List<Safe> => safes.filter(
|
|
||||||
// (safe: Safe) => safe.owners.filter((owner: Owner) => sameAddress(owner.get('address'), userAddress)).count() > 0,
|
|
||||||
// ),
|
|
||||||
// )
|
|
||||||
|
|
Loading…
Reference in New Issue