refactor tokens reducer
This commit is contained in:
parent
78883eacb1
commit
fe35b811c4
|
@ -14,14 +14,13 @@ type AddTokenProps = {
|
|||
|
||||
export const addToken = createAction<string, *, *>(
|
||||
ADD_TOKEN,
|
||||
(safeAddress: string, token: Token): AddTokenProps => ({
|
||||
safeAddress,
|
||||
(token: Token): AddTokenProps => ({
|
||||
token,
|
||||
}),
|
||||
)
|
||||
|
||||
const saveToken = (safeAddress: string, token: Token) => async (dispatch: ReduxDispatch<GlobalState>) => {
|
||||
dispatch(addToken(safeAddress, token))
|
||||
dispatch(addToken(token))
|
||||
|
||||
const activeTokens = await getActiveTokens(safeAddress)
|
||||
await setActiveTokens(safeAddress, activeTokens.push(token.toJS()))
|
||||
|
|
|
@ -5,8 +5,6 @@ import { type Token } from '~/logic/tokens/store/model/token'
|
|||
import { ADD_TOKEN } from '~/logic/tokens/store/actions/addToken'
|
||||
import { REMOVE_TOKEN } from '~/logic/tokens/store/actions/removeToken'
|
||||
import { ADD_TOKENS } from '~/logic/tokens/store/actions/saveTokens'
|
||||
import { DISABLE_TOKEN } from '~/logic/tokens/store/actions/disableToken'
|
||||
import { ENABLE_TOKEN } from '~/logic/tokens/store/actions/enableToken'
|
||||
|
||||
export const TOKEN_REDUCER_ID = 'tokens'
|
||||
|
||||
|
@ -15,33 +13,27 @@ export type State = Map<string, Map<string, Token>>
|
|||
export default handleActions<State, *>(
|
||||
{
|
||||
[ADD_TOKENS]: (state: State, action: ActionType<Function>): State => {
|
||||
const { safeAddress, tokens } = action.payload
|
||||
const { tokens } = action.payload
|
||||
|
||||
return state.update(safeAddress, (prevState: Map<string, Token>) => (prevState ? prevState.merge(tokens) : tokens))
|
||||
const newState = state.withMutations((map) => {
|
||||
tokens.forEach((token) => {
|
||||
map.set(token.address, token)
|
||||
})
|
||||
})
|
||||
|
||||
return newState
|
||||
},
|
||||
[ADD_TOKEN]: (state: State, action: ActionType<Function>): State => {
|
||||
const { safeAddress, token } = action.payload
|
||||
const { token } = action.payload
|
||||
const { address: tokenAddress } = token
|
||||
|
||||
return state.setIn([safeAddress, tokenAddress], token)
|
||||
return state.set(tokenAddress, token)
|
||||
},
|
||||
[REMOVE_TOKEN]: (state: State, action: ActionType<Function>): State => {
|
||||
const { safeAddress, token } = action.payload
|
||||
const { token } = action.payload
|
||||
const { address: tokenAddress } = token
|
||||
|
||||
return state.removeIn([safeAddress, tokenAddress])
|
||||
},
|
||||
[DISABLE_TOKEN]: (state: State, action: ActionType<Function>): State => {
|
||||
const { safeAddress, token } = action.payload
|
||||
const { address: tokenAddress } = token
|
||||
|
||||
return state.setIn([safeAddress, tokenAddress, 'status'], false)
|
||||
},
|
||||
[ENABLE_TOKEN]: (state: State, action: ActionType<Function>): State => {
|
||||
const { safeAddress, token } = action.payload
|
||||
const { address: tokenAddress } = token
|
||||
|
||||
return state.setIn([safeAddress, tokenAddress, 'status'], true)
|
||||
return state.remove(tokenAddress)
|
||||
},
|
||||
},
|
||||
Map(),
|
||||
|
|
|
@ -20,16 +20,11 @@ export const tokensSelector: Selector<GlobalState, RouterProps, Map<string, Toke
|
|||
},
|
||||
)
|
||||
|
||||
export const tokenListSelector: Selector<GlobalState, RouterProps, List<Token>> = createSelector(
|
||||
tokensSelector,
|
||||
export const tokenListSelector: Selector<GlobalState, Map<string, Token>, List<Token>> = createSelector(
|
||||
tokensStateSelector,
|
||||
(tokens: Map<string, Token>) => tokens.toList(),
|
||||
)
|
||||
|
||||
export const activeTokensSelector: Selector<GlobalState, RouterProps, List<Token>> = createSelector(
|
||||
tokenListSelector,
|
||||
(tokens: List<Token>) => tokens.filter((token: Token) => token.get('status')),
|
||||
)
|
||||
|
||||
export const orderedTokenListSelector: Selector<GlobalState, RouterProps, List<Token>> = createSelector(
|
||||
tokenListSelector,
|
||||
(tokens: List<Token>) => tokens.sortBy((token: Token) => token.get('symbol')),
|
||||
|
@ -43,12 +38,3 @@ export const tokenAddressesSelector: Selector<GlobalState, RouterProps, List<str
|
|||
return addresses
|
||||
},
|
||||
)
|
||||
|
||||
export const activeTokenAdressesSelector: Selector<GlobalState, RouterProps, List<string>> = createSelector(
|
||||
activeTokensSelector,
|
||||
(tokens: List<Token>) => {
|
||||
const addresses = List().withMutations(list => tokens.map(token => list.push(token.address)))
|
||||
|
||||
return addresses
|
||||
},
|
||||
)
|
||||
|
|
|
@ -12,7 +12,7 @@ import { type Safe } from '~/routes/safe/store/model/safe'
|
|||
import { type Owner } from '~/routes/safe/store/model/owner'
|
||||
import { type GlobalState } from '~/store'
|
||||
import { sameAddress } from '~/logic/wallets/ethAddresses'
|
||||
import { activeTokensSelector, orderedTokenListSelector, tokensSelector } from '~/logic/tokens/store/selectors'
|
||||
import { orderedTokenListSelector, tokensSelector } from '~/logic/tokens/store/selectors'
|
||||
import { type Token } from '~/logic/tokens/store/model/token'
|
||||
import { safeParamAddressSelector } from '../store/selectors'
|
||||
|
||||
|
@ -53,11 +53,11 @@ const extendedSafeTokensSelector: Selector<GlobalState, RouterProps, List<Token>
|
|||
(safeTokens: Map<string, string>, tokensList: Map<string, Token>) => {
|
||||
// const extendedTokens = safeTokens.map(token => tokensList.get(token.address).set('balance', token.balance))
|
||||
const extendedTokens = Map().withMutations((map) => {
|
||||
safeTokens.forEach(({ address, balance }: { address: string, balance: string }) => {
|
||||
const baseToken = tokensList.get(address)
|
||||
safeTokens.forEach((token: { address: string, balance: string }) => {
|
||||
const baseToken = tokensList.get(token.address)
|
||||
|
||||
if (baseToken) {
|
||||
map.set(address, baseToken.set(balance))
|
||||
map.set(token.address, baseToken.set(token.balance))
|
||||
}
|
||||
})
|
||||
})
|
||||
|
@ -69,8 +69,8 @@ const extendedSafeTokensSelector: Selector<GlobalState, RouterProps, List<Token>
|
|||
export default createStructuredSelector<Object, *>({
|
||||
safe: safeSelector,
|
||||
provider: providerNameSelector,
|
||||
tokens: extendedSafeTokensSelector,
|
||||
activeTokens: activeTokensSelector,
|
||||
tokens: orderedTokenListSelector,
|
||||
activeTokens: extendedSafeTokensSelector,
|
||||
granted: grantedSelector,
|
||||
userAddress: userAccountSelector,
|
||||
network: networkSelector,
|
||||
|
|
|
@ -9,7 +9,10 @@ import { getOwners, getSafeName } from '~/logic/safe/utils'
|
|||
import { getGnosisSafeContract } from '~/logic/contracts/safeContracts'
|
||||
import { getWeb3 } from '~/logic/wallets/getWeb3'
|
||||
|
||||
const buildOwnersFrom = (safeOwners: string[], storedOwners: Map<string, string>) => safeOwners.map((ownerAddress: string) => {
|
||||
const buildOwnersFrom = (
|
||||
safeOwners: string[],
|
||||
storedOwners: Map<string, string>, // eslint-disable-next-line
|
||||
) => safeOwners.map((ownerAddress: string) => {
|
||||
const ownerName = storedOwners.get(ownerAddress.toLowerCase()) || 'UNKNOWN'
|
||||
return makeOwner({ name: ownerName, address: ownerAddress })
|
||||
})
|
||||
|
@ -34,7 +37,7 @@ export const buildSafe = async (safeAddress: string, safeName: string) => {
|
|||
|
||||
export default (safeAddress: string) => async (dispatch: ReduxDispatch<GlobalState>) => {
|
||||
try {
|
||||
const safeName = await getSafeName(safeAddress) || 'LOADED SAFE'
|
||||
const safeName = (await getSafeName(safeAddress)) || 'LOADED SAFE'
|
||||
const safeRecord = await buildSafe(safeAddress, safeName)
|
||||
|
||||
return dispatch(updateSafe(safeRecord))
|
||||
|
|
|
@ -9,7 +9,7 @@ export type SafeProps = {
|
|||
address: string,
|
||||
threshold: number,
|
||||
owners: List<Owner>,
|
||||
tokens: List<Map<string, string>>,
|
||||
tokens?: List<Map<string, string>>,
|
||||
}
|
||||
|
||||
const SafeRecord: RecordFactory<SafeProps> = Record({
|
||||
|
|
|
@ -10,7 +10,7 @@ import { aMinedSafe } from '~/test/builder/safe.redux.builder'
|
|||
import { travelToTokens } from '~/test/builder/safe.dom.utils'
|
||||
import { sleep } from '~/utils/timer'
|
||||
import { buildMathPropsFrom } from '~/test/utils/buildReactRouterProps'
|
||||
import { tokenListSelector, activeTokensSelector } from '~/logic/tokens/store/selectors'
|
||||
import { tokenListSelector } from '~/logic/tokens/store/selectors'
|
||||
import { getActiveTokenAddresses } from '~/logic/tokens/utils/tokensStorage'
|
||||
import { enableFirstToken, testToken } from '~/test/builder/tokens.dom.utils'
|
||||
import * as fetchTokensModule from '~/logic/tokens/store/actions/fetchTokens'
|
||||
|
|
|
@ -10,7 +10,6 @@ import { testToken } from '~/test/builder/tokens.dom.utils'
|
|||
import * as fetchTokensModule from '~/logic/tokens/store/actions/fetchTokens'
|
||||
import * as enhancedFetchModule from '~/utils/fetch'
|
||||
import addToken from '~/logic/tokens/store/actions/addToken'
|
||||
import { activeTokensSelector } from '~/logic/tokens/store/selectors'
|
||||
|
||||
describe('DOM > Feature > Add new ERC 20 Tokens', () => {
|
||||
// let web3
|
||||
|
|
|
@ -10,7 +10,6 @@ import { testToken } from '~/test/builder/tokens.dom.utils'
|
|||
import * as fetchTokensModule from '~/logic/tokens/store/actions/fetchTokens'
|
||||
import * as enhancedFetchModule from '~/utils/fetch'
|
||||
import addToken from '~/logic/tokens/store/actions/addToken'
|
||||
import { activeTokensSelector, tokenListSelector } from '~/logic/tokens/store/selectors'
|
||||
import removeTokenAction from '~/logic/tokens/store/actions/removeToken'
|
||||
import { makeToken } from '~/logic/tokens/store/model/token'
|
||||
|
||||
|
|
Loading…
Reference in New Issue