wip commit: removing side effects from tokens reducer to actions
This commit is contained in:
parent
5c8dcb1d8f
commit
c62271bcb8
|
@ -1,6 +1,13 @@
|
||||||
// @flow
|
// @flow
|
||||||
import { createAction } from 'redux-actions'
|
import { createAction } from 'redux-actions'
|
||||||
import { type Token } from '~/logic/tokens/store/model/token'
|
import { type Token } from '~/logic/tokens/store/model/token'
|
||||||
|
import {
|
||||||
|
setActiveTokenAddresses,
|
||||||
|
getActiveTokenAddresses,
|
||||||
|
setToken,
|
||||||
|
} from '~/logic/tokens/utils/activeTokensStorage'
|
||||||
|
import type { Dispatch as ReduxDispatch } from 'redux'
|
||||||
|
import { type GlobalState } from '~/store/index'
|
||||||
|
|
||||||
export const ADD_TOKEN = 'ADD_TOKEN'
|
export const ADD_TOKEN = 'ADD_TOKEN'
|
||||||
|
|
||||||
|
@ -17,4 +24,14 @@ const addToken = createAction(
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
export default addToken
|
const saveToken = (safeAddress: string, token: Token) => (dispatch: ReduxDispatch<GlobalState>) => {
|
||||||
|
dispatch(addToken(safeAddress, token))
|
||||||
|
|
||||||
|
const tokenAddress = token.get('address')
|
||||||
|
const activeTokens = getActiveTokenAddresses(safeAddress)
|
||||||
|
setActiveTokenAddresses(safeAddress, activeTokens.push(tokenAddress))
|
||||||
|
setToken(safeAddress, token)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export default saveToken
|
||||||
|
|
|
@ -1,10 +1,17 @@
|
||||||
// @flow
|
// @flow
|
||||||
import { Map } from 'immutable'
|
import { Map, List } from 'immutable'
|
||||||
import { createAction } from 'redux-actions'
|
import { createAction } from 'redux-actions'
|
||||||
|
import type { Dispatch as ReduxDispatch } from 'redux'
|
||||||
import { type Token } from '~/logic/tokens/store/model/token'
|
import { type Token } from '~/logic/tokens/store/model/token'
|
||||||
|
import { ensureOnce } from '~/utils/singleton'
|
||||||
|
import { type GlobalState } from '~/store/index'
|
||||||
|
import { setActiveTokenAddresses } from '~/logic/tokens/utils/activeTokensStorage'
|
||||||
|
import { calculateActiveErc20TokensFrom } from '~/logic/tokens/utils/tokenHelpers'
|
||||||
|
|
||||||
export const ADD_TOKENS = 'ADD_TOKENS'
|
export const ADD_TOKENS = 'ADD_TOKENS'
|
||||||
|
|
||||||
|
const setTokensOnce = ensureOnce(setActiveTokenAddresses)
|
||||||
|
|
||||||
type TokenProps = {
|
type TokenProps = {
|
||||||
safeAddress: string,
|
safeAddress: string,
|
||||||
tokens: Map<string, Token>,
|
tokens: Map<string, Token>,
|
||||||
|
@ -18,4 +25,11 @@ const addTokens = createAction(
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
export default addTokens
|
const saveTokens = (safeAddress: string, tokens: Map<string, Token>) => (dispatch: ReduxDispatch<GlobalState>) => {
|
||||||
|
dispatch(addTokens(safeAddress, tokens))
|
||||||
|
|
||||||
|
const activeAddresses: List<Token> = calculateActiveErc20TokensFrom(tokens.toList())
|
||||||
|
setTokensOnce(safeAddress, activeAddresses)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default saveTokens
|
||||||
|
|
|
@ -11,8 +11,6 @@ const enableToken = createAction(ENABLE_TOKEN, (safeAddress: string, token: Toke
|
||||||
|
|
||||||
const setTokenEnabled = (safeAddress: string, token: Token) => (dispatch: ReduxDispatch<GlobalState>) => {
|
const setTokenEnabled = (safeAddress: string, token: Token) => (dispatch: ReduxDispatch<GlobalState>) => {
|
||||||
dispatch(enableToken(safeAddress, token))
|
dispatch(enableToken(safeAddress, token))
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default setTokenEnabled
|
export default setTokenEnabled
|
||||||
|
|
|
@ -9,7 +9,7 @@ import { type GlobalState } from '~/store/index'
|
||||||
import { makeToken, type Token, type TokenProps } from '~/logic/tokens/store/model/token'
|
import { makeToken, type Token, type TokenProps } from '~/logic/tokens/store/model/token'
|
||||||
import { ensureOnce } from '~/utils/singleton'
|
import { ensureOnce } from '~/utils/singleton'
|
||||||
import { getActiveTokenAddresses, getTokens } from '~/logic/tokens/utils/activeTokensStorage'
|
import { getActiveTokenAddresses, getTokens } from '~/logic/tokens/utils/activeTokensStorage'
|
||||||
import { getSafeEthToken } from '~/utils/tokens'
|
import { getSafeEthToken } from '~/logic/tokens/utils/tokenHelpers'
|
||||||
import { enhancedFetch } from '~/utils/fetch'
|
import { enhancedFetch } from '~/utils/fetch'
|
||||||
import addTokens from './addTokens'
|
import addTokens from './addTokens'
|
||||||
import { getRelayUrl } from '~/config/index'
|
import { getRelayUrl } from '~/config/index'
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
// @flow
|
// @flow
|
||||||
import { createAction } from 'redux-actions'
|
import { createAction } from 'redux-actions'
|
||||||
import { type Token } from '~/logic/tokens/store/model/token'
|
import { type Token } from '~/logic/tokens/store/model/token'
|
||||||
|
import { removeTokenFromStorage, removeFromActiveTokens } from '~/logic/tokens/utils/activeTokensStorage'
|
||||||
|
import { type GlobalState } from '~/store/index'
|
||||||
|
import type { Dispatch as ReduxDispatch } from 'redux'
|
||||||
|
|
||||||
export const REMOVE_TOKEN = 'REMOVE_TOKEN'
|
export const REMOVE_TOKEN = 'REMOVE_TOKEN'
|
||||||
|
|
||||||
|
@ -17,4 +20,12 @@ const removeToken = createAction(
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
export default removeToken
|
const deleteToken = (safeAddress: string, token: Token) => (dispatch: ReduxDispatch<GlobalState>) => {
|
||||||
|
dispatch(removeToken(safeAddress, token))
|
||||||
|
|
||||||
|
const tokenAddress = token.get('address')
|
||||||
|
removeFromActiveTokens(safeAddress, tokenAddress)
|
||||||
|
removeTokenFromStorage(safeAddress, token)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default deleteToken
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
// @flow
|
// @flow
|
||||||
import { List, Map } from 'immutable'
|
import { Map } from 'immutable'
|
||||||
import { handleActions, type ActionType } from 'redux-actions'
|
import { handleActions, type ActionType } from 'redux-actions'
|
||||||
import addToken, { ADD_TOKEN } from '~/logic/tokens/store/actions/addToken'
|
import addToken, { ADD_TOKEN } from '~/logic/tokens/store/actions/addToken'
|
||||||
import removeToken, { REMOVE_TOKEN } from '~/logic/tokens/store/actions/removeToken'
|
import removeToken, { REMOVE_TOKEN } from '~/logic/tokens/store/actions/removeToken'
|
||||||
|
@ -10,32 +10,17 @@ import enableToken, { ENABLE_TOKEN } from '~/logic/tokens/store/actions/enableTo
|
||||||
import {
|
import {
|
||||||
setActiveTokenAddresses,
|
setActiveTokenAddresses,
|
||||||
getActiveTokenAddresses,
|
getActiveTokenAddresses,
|
||||||
setToken,
|
|
||||||
removeTokenFromStorage,
|
|
||||||
} from '~/logic/tokens/utils/activeTokensStorage'
|
} from '~/logic/tokens/utils/activeTokensStorage'
|
||||||
import { ensureOnce } from '~/utils/singleton'
|
|
||||||
import { calculateActiveErc20TokensFrom } from '~/utils/tokens'
|
|
||||||
|
|
||||||
export const TOKEN_REDUCER_ID = 'tokens'
|
export const TOKEN_REDUCER_ID = 'tokens'
|
||||||
|
|
||||||
export type State = Map<string, Map<string, Token>>
|
export type State = Map<string, Map<string, Token>>
|
||||||
|
|
||||||
const setTokensOnce = ensureOnce(setActiveTokenAddresses)
|
|
||||||
|
|
||||||
const removeFromActiveTokens = (safeAddress: string, tokenAddress: string) => {
|
|
||||||
const activeTokens = getActiveTokenAddresses(safeAddress)
|
|
||||||
const index = activeTokens.indexOf(tokenAddress)
|
|
||||||
setActiveTokenAddresses(safeAddress, activeTokens.delete(index))
|
|
||||||
}
|
|
||||||
|
|
||||||
export default handleActions(
|
export default handleActions(
|
||||||
{
|
{
|
||||||
[ADD_TOKENS]: (state: State, action: ActionType<typeof addTokens>): State => {
|
[ADD_TOKENS]: (state: State, action: ActionType<typeof addTokens>): State => {
|
||||||
const { safeAddress, tokens } = action.payload
|
const { safeAddress, tokens } = action.payload
|
||||||
|
|
||||||
const activeAddresses: List<Token> = calculateActiveErc20TokensFrom(tokens.toList())
|
|
||||||
setTokensOnce(safeAddress, activeAddresses)
|
|
||||||
|
|
||||||
return state.update(safeAddress, (prevSafe: Map<string, Token>) => {
|
return state.update(safeAddress, (prevSafe: Map<string, Token>) => {
|
||||||
if (!prevSafe) {
|
if (!prevSafe) {
|
||||||
return tokens
|
return tokens
|
||||||
|
@ -48,17 +33,12 @@ export default handleActions(
|
||||||
const { safeAddress, token } = action.payload
|
const { safeAddress, token } = action.payload
|
||||||
|
|
||||||
const tokenAddress = token.get('address')
|
const tokenAddress = token.get('address')
|
||||||
const activeTokens = getActiveTokenAddresses(safeAddress)
|
|
||||||
setActiveTokenAddresses(safeAddress, activeTokens.push(tokenAddress))
|
|
||||||
setToken(safeAddress, token)
|
|
||||||
return state.setIn([safeAddress, tokenAddress], token)
|
return state.setIn([safeAddress, tokenAddress], token)
|
||||||
},
|
},
|
||||||
[REMOVE_TOKEN]: (state: State, action: ActionType<typeof removeToken>): State => {
|
[REMOVE_TOKEN]: (state: State, action: ActionType<typeof removeToken>): State => {
|
||||||
const { safeAddress, token } = action.payload
|
const { safeAddress, token } = action.payload
|
||||||
|
|
||||||
const tokenAddress = token.get('address')
|
const tokenAddress = token.get('address')
|
||||||
removeFromActiveTokens(safeAddress, tokenAddress)
|
|
||||||
removeTokenFromStorage(safeAddress, token)
|
|
||||||
return state.removeIn([safeAddress, tokenAddress])
|
return state.removeIn([safeAddress, tokenAddress])
|
||||||
},
|
},
|
||||||
[DISABLE_TOKEN]: (state: State, action: ActionType<typeof disableToken>): State => {
|
[DISABLE_TOKEN]: (state: State, action: ActionType<typeof disableToken>): State => {
|
||||||
|
|
|
@ -65,3 +65,9 @@ export const removeTokenFromStorage = (safeAddress: string, token: Token) => {
|
||||||
console.log('Error removing token in localstorage')
|
console.log('Error removing token in localstorage')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const removeFromActiveTokens = (safeAddress: string, tokenAddress: string) => {
|
||||||
|
const activeTokens = getActiveTokenAddresses(safeAddress)
|
||||||
|
const index = activeTokens.indexOf(tokenAddress)
|
||||||
|
setActiveTokenAddresses(safeAddress, activeTokens.delete(index))
|
||||||
|
}
|
|
@ -56,5 +56,4 @@ export const generateColumns = () => {
|
||||||
return List([assetRow, balanceRow, actions])
|
return List([assetRow, balanceRow, actions])
|
||||||
}
|
}
|
||||||
|
|
||||||
export const filterByZero = (data: Array<BalanceRow>, hideZero: boolean): Array<BalanceRow> =>
|
export const filterByZero = (data: Array<BalanceRow>, hideZero: boolean): Array<BalanceRow> => data.filter((row: BalanceRow) => (hideZero ? row[buildOrderFieldFrom(BALANCE_TABLE_BALANCE_ID)] !== 0 : true))
|
||||||
data.filter((row: BalanceRow) => (hideZero ? row[buildOrderFieldFrom(BALANCE_TABLE_BALANCE_ID)] !== 0 : true))
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ import { sleep } from '~/utils/timer'
|
||||||
import { type Safe } from '~/routes/safe/store/model/safe'
|
import { type Safe } from '~/routes/safe/store/model/safe'
|
||||||
import { getStandardTokenContract } from '~/logic/tokens/store/actions/fetchTokens'
|
import { getStandardTokenContract } from '~/logic/tokens/store/actions/fetchTokens'
|
||||||
import { type Token } from '~/logic/tokens/store/model/token'
|
import { type Token } from '~/logic/tokens/store/model/token'
|
||||||
import { isEther } from '~/utils/tokens'
|
import { isEther } from '~/logic/tokens/utils/tokens'
|
||||||
import { EMPTY_DATA } from '~/logic/wallets/ethTransactions'
|
import { EMPTY_DATA } from '~/logic/wallets/ethTransactions'
|
||||||
import { toNative } from '~/logic/wallets/tokens'
|
import { toNative } from '~/logic/wallets/tokens'
|
||||||
import { createTransaction, getSafeEthereumInstance } from '~/logic/safe/safeFrontendOperations'
|
import { createTransaction, getSafeEthereumInstance } from '~/logic/safe/safeFrontendOperations'
|
||||||
|
|
|
@ -7,7 +7,7 @@ import { type Token } from '~/logic/tokens/store/model/token'
|
||||||
import { TOKEN_REDUCER_ID } from '~/logic/tokens/store/reducer/tokens'
|
import { TOKEN_REDUCER_ID } from '~/logic/tokens/store/reducer/tokens'
|
||||||
import { addEtherTo, addTknTo } from '~/test/utils/tokenMovements'
|
import { addEtherTo, addTknTo } from '~/test/utils/tokenMovements'
|
||||||
import { dispatchTknBalance } from '~/test/utils/transactions/moveTokens.helper'
|
import { dispatchTknBalance } from '~/test/utils/transactions/moveTokens.helper'
|
||||||
import { ETH_ADDRESS } from '~/utils/tokens'
|
import { ETH_ADDRESS } from '~/logic/tokens/utils/tokenHelpers'
|
||||||
|
|
||||||
describe('Safe - redux balance property', () => {
|
describe('Safe - redux balance property', () => {
|
||||||
let store
|
let store
|
||||||
|
|
Loading…
Reference in New Issue