wip commit: removing side effects from tokens reducer to actions

This commit is contained in:
Mikhail Mikheev 2019-03-22 19:27:18 +04:00
parent 5c8dcb1d8f
commit c62271bcb8
11 changed files with 57 additions and 32 deletions

View File

@ -1,6 +1,13 @@
// @flow
import { createAction } from 'redux-actions'
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'
@ -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

View File

@ -1,10 +1,17 @@
// @flow
import { Map } from 'immutable'
import { Map, List } from 'immutable'
import { createAction } from 'redux-actions'
import type { Dispatch as ReduxDispatch } from 'redux'
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'
const setTokensOnce = ensureOnce(setActiveTokenAddresses)
type TokenProps = {
safeAddress: string,
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

View File

@ -11,8 +11,6 @@ const enableToken = createAction(ENABLE_TOKEN, (safeAddress: string, token: Toke
const setTokenEnabled = (safeAddress: string, token: Token) => (dispatch: ReduxDispatch<GlobalState>) => {
dispatch(enableToken(safeAddress, token))
}
export default setTokenEnabled

View File

@ -9,7 +9,7 @@ import { type GlobalState } from '~/store/index'
import { makeToken, type Token, type TokenProps } from '~/logic/tokens/store/model/token'
import { ensureOnce } from '~/utils/singleton'
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 addTokens from './addTokens'
import { getRelayUrl } from '~/config/index'

View File

@ -1,6 +1,9 @@
// @flow
import { createAction } from 'redux-actions'
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'
@ -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

View File

@ -1,5 +1,5 @@
// @flow
import { List, Map } from 'immutable'
import { Map } from 'immutable'
import { handleActions, type ActionType } from 'redux-actions'
import addToken, { ADD_TOKEN } from '~/logic/tokens/store/actions/addToken'
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 {
setActiveTokenAddresses,
getActiveTokenAddresses,
setToken,
removeTokenFromStorage,
} from '~/logic/tokens/utils/activeTokensStorage'
import { ensureOnce } from '~/utils/singleton'
import { calculateActiveErc20TokensFrom } from '~/utils/tokens'
export const TOKEN_REDUCER_ID = 'tokens'
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(
{
[ADD_TOKENS]: (state: State, action: ActionType<typeof addTokens>): State => {
const { safeAddress, tokens } = action.payload
const activeAddresses: List<Token> = calculateActiveErc20TokensFrom(tokens.toList())
setTokensOnce(safeAddress, activeAddresses)
return state.update(safeAddress, (prevSafe: Map<string, Token>) => {
if (!prevSafe) {
return tokens
@ -48,17 +33,12 @@ export default handleActions(
const { safeAddress, token } = action.payload
const tokenAddress = token.get('address')
const activeTokens = getActiveTokenAddresses(safeAddress)
setActiveTokenAddresses(safeAddress, activeTokens.push(tokenAddress))
setToken(safeAddress, token)
return state.setIn([safeAddress, tokenAddress], token)
},
[REMOVE_TOKEN]: (state: State, action: ActionType<typeof removeToken>): State => {
const { safeAddress, token } = action.payload
const tokenAddress = token.get('address')
removeFromActiveTokens(safeAddress, tokenAddress)
removeTokenFromStorage(safeAddress, token)
return state.removeIn([safeAddress, tokenAddress])
},
[DISABLE_TOKEN]: (state: State, action: ActionType<typeof disableToken>): State => {

View File

@ -65,3 +65,9 @@ export const removeTokenFromStorage = (safeAddress: string, token: Token) => {
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))
}

View File

@ -56,5 +56,4 @@ export const generateColumns = () => {
return List([assetRow, balanceRow, actions])
}
export const filterByZero = (data: Array<BalanceRow>, hideZero: boolean): Array<BalanceRow> =>
data.filter((row: BalanceRow) => (hideZero ? row[buildOrderFieldFrom(BALANCE_TABLE_BALANCE_ID)] !== 0 : true))
export const filterByZero = (data: Array<BalanceRow>, hideZero: boolean): Array<BalanceRow> => data.filter((row: BalanceRow) => (hideZero ? row[buildOrderFieldFrom(BALANCE_TABLE_BALANCE_ID)] !== 0 : true))

View File

@ -7,7 +7,7 @@ import { sleep } from '~/utils/timer'
import { type Safe } from '~/routes/safe/store/model/safe'
import { getStandardTokenContract } from '~/logic/tokens/store/actions/fetchTokens'
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 { toNative } from '~/logic/wallets/tokens'
import { createTransaction, getSafeEthereumInstance } from '~/logic/safe/safeFrontendOperations'

View File

@ -7,7 +7,7 @@ import { type Token } from '~/logic/tokens/store/model/token'
import { TOKEN_REDUCER_ID } from '~/logic/tokens/store/reducer/tokens'
import { addEtherTo, addTknTo } from '~/test/utils/tokenMovements'
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', () => {
let store