From 718e84aea3cb84832ef1e0efbdc67bba78ad2168 Mon Sep 17 00:00:00 2001 From: Mikhail Mikheev Date: Wed, 27 Mar 2019 20:19:38 +0400 Subject: [PATCH] save whole tokens object in a reducer wip, fix flow type annotation in tokens reducer --- src/logic/tokens/store/actions/addToken.js | 6 ++--- src/logic/tokens/store/actions/addTokens.js | 6 ++--- .../tokens/store/actions/disableToken.js | 2 +- src/logic/tokens/store/actions/enableToken.js | 8 +++---- src/logic/tokens/store/actions/removeToken.js | 2 +- src/logic/tokens/store/reducer/tokens.js | 22 +++++++++---------- src/logic/tokens/utils/tokensStorage.js | 12 +++++----- 7 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/logic/tokens/store/actions/addToken.js b/src/logic/tokens/store/actions/addToken.js index a9d53bb4..c980a120 100644 --- a/src/logic/tokens/store/actions/addToken.js +++ b/src/logic/tokens/store/actions/addToken.js @@ -1,7 +1,7 @@ // @flow import { createAction } from 'redux-actions' import { type Token } from '~/logic/tokens/store/model/token' -import { setActiveTokenAddresses, getActiveTokenAddresses, setToken } from '~/logic/tokens/utils/tokensStorage' +import { setActiveTokens, getActiveTokenAddresses, setToken } from '~/logic/tokens/utils/tokensStorage' import type { Dispatch as ReduxDispatch } from 'redux' import { type GlobalState } from '~/store/index' @@ -12,7 +12,7 @@ type AddTokenProps = { token: Token, } -const addToken = createAction( +export const addToken = createAction( ADD_TOKEN, (safeAddress: string, token: Token): AddTokenProps => ({ safeAddress, @@ -25,7 +25,7 @@ const saveToken = (safeAddress: string, token: Token) => async (dispatch: ReduxD const tokenAddress = token.get('address') const activeTokens = await getActiveTokenAddresses(safeAddress) - await setActiveTokenAddresses(safeAddress, activeTokens.push(tokenAddress)) + await setActiveTokens(safeAddress, activeTokens.push(tokenAddress)) setToken(safeAddress, token) } diff --git a/src/logic/tokens/store/actions/addTokens.js b/src/logic/tokens/store/actions/addTokens.js index 6559d639..ac01b37f 100644 --- a/src/logic/tokens/store/actions/addTokens.js +++ b/src/logic/tokens/store/actions/addTokens.js @@ -5,19 +5,19 @@ import type { Dispatch as ReduxDispatch } from 'redux' import { type Token } from '~/logic/tokens/store/model/token' import { ensureOnceAsync } from '~/utils/singleton' import { type GlobalState } from '~/store/index' -import { setActiveTokenAddresses } from '~/logic/tokens/utils/tokensStorage' +import { setActiveTokens } from '~/logic/tokens/utils/tokensStorage' import { calculateActiveErc20TokensFrom } from '~/logic/tokens/utils/tokenHelpers' export const ADD_TOKENS = 'ADD_TOKENS' -const setTokensOnce = ensureOnceAsync(setActiveTokenAddresses) +const setTokensOnce = ensureOnceAsync(setActiveTokens) type TokenProps = { safeAddress: string, tokens: Map, } -const addTokens = createAction( +export const addTokens = createAction( ADD_TOKENS, (safeAddress: string, tokens: Map): TokenProps => ({ safeAddress, diff --git a/src/logic/tokens/store/actions/disableToken.js b/src/logic/tokens/store/actions/disableToken.js index 9e26f8f1..a531640d 100644 --- a/src/logic/tokens/store/actions/disableToken.js +++ b/src/logic/tokens/store/actions/disableToken.js @@ -7,7 +7,7 @@ import { removeFromActiveTokens } from '~/logic/tokens/utils/tokensStorage' export const DISABLE_TOKEN = 'DISABLE_TOKEN' -const disableToken = createAction(DISABLE_TOKEN, (safeAddress: string, tokenAddress: string) => ({ +export const disableToken = createAction(DISABLE_TOKEN, (safeAddress: string, tokenAddress: string) => ({ safeAddress, tokenAddress, })) diff --git a/src/logic/tokens/store/actions/enableToken.js b/src/logic/tokens/store/actions/enableToken.js index 75c0ae38..a8811380 100644 --- a/src/logic/tokens/store/actions/enableToken.js +++ b/src/logic/tokens/store/actions/enableToken.js @@ -3,11 +3,11 @@ import { createAction } from 'redux-actions' import type { Dispatch as ReduxDispatch } from 'redux' import { type GlobalState } from '~/store/index' import { type Token } from '~/logic/tokens/store/model/token' -import { setActiveTokenAddresses, getActiveTokenAddresses } from '~/logic/tokens/utils/tokensStorage' +import { setActiveTokens, getActiveTokens } from '~/logic/tokens/utils/tokensStorage' export const ENABLE_TOKEN = 'ENABLE_TOKEN' -const enableToken = createAction(ENABLE_TOKEN, (safeAddress: string, tokenAddress: string) => ({ +export const enableToken = createAction(ENABLE_TOKEN, (safeAddress: string, tokenAddress: string) => ({ safeAddress, tokenAddress, })) @@ -16,8 +16,8 @@ const setTokenEnabled = (safeAddress: string, token: Token) => async (dispatch: const { address } = token dispatch(enableToken(safeAddress, address)) - const activeTokens = await getActiveTokenAddresses(safeAddress) - await setActiveTokenAddresses(safeAddress, activeTokens.push(address)) + const activeTokens = await getActiveTokens(safeAddress) + await setActiveTokens(safeAddress, activeTokens.push(token)) } export default setTokenEnabled diff --git a/src/logic/tokens/store/actions/removeToken.js b/src/logic/tokens/store/actions/removeToken.js index 8398f3d7..960ac45b 100644 --- a/src/logic/tokens/store/actions/removeToken.js +++ b/src/logic/tokens/store/actions/removeToken.js @@ -12,7 +12,7 @@ type RemoveTokenProps = { token: Token, } -const removeToken = createAction( +export const removeToken = createAction( REMOVE_TOKEN, (safeAddress: string, token: Token): RemoveTokenProps => ({ safeAddress, diff --git a/src/logic/tokens/store/reducer/tokens.js b/src/logic/tokens/store/reducer/tokens.js index 178463c0..97c5fa5a 100644 --- a/src/logic/tokens/store/reducer/tokens.js +++ b/src/logic/tokens/store/reducer/tokens.js @@ -1,20 +1,20 @@ // @flow 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' -import addTokens, { ADD_TOKENS } from '~/logic/tokens/store/actions/addTokens' import { type Token } from '~/logic/tokens/store/model/token' -import disableToken, { DISABLE_TOKEN } from '~/logic/tokens/store/actions/disableToken' -import enableToken, { ENABLE_TOKEN } from '~/logic/tokens/store/actions/enableToken' +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/addTokens' +import { DISABLE_TOKEN } from '~/logic/tokens/store/actions/disableToken' +import { ENABLE_TOKEN } from '~/logic/tokens/store/actions/enableToken' export const TOKEN_REDUCER_ID = 'tokens' export type State = Map> -export default handleActions( +export default handleActions( { - [ADD_TOKENS]: (state: State, action: ActionType): State => { + [ADD_TOKENS]: (state: State, action: ActionType): State => { const { safeAddress, tokens } = action.payload return state.update(safeAddress, (prevSafe: Map) => { @@ -25,24 +25,24 @@ export default handleActions( return prevSafe.equals(tokens) ? prevSafe : tokens }) }, - [ADD_TOKEN]: (state: State, action: ActionType): State => { + [ADD_TOKEN]: (state: State, action: ActionType): State => { const { safeAddress, token } = action.payload const { address: tokenAddress } = token return state.setIn([safeAddress, tokenAddress], token) }, - [REMOVE_TOKEN]: (state: State, action: ActionType): State => { + [REMOVE_TOKEN]: (state: State, action: ActionType): State => { const { safeAddress, token } = action.payload const { address: tokenAddress } = token return state.removeIn([safeAddress, tokenAddress]) }, - [DISABLE_TOKEN]: (state: State, action: ActionType): State => { + [DISABLE_TOKEN]: (state: State, action: ActionType): State => { const { tokenAddress, safeAddress } = action.payload return state.setIn([safeAddress, tokenAddress, 'status'], false) }, - [ENABLE_TOKEN]: (state: State, action: ActionType): State => { + [ENABLE_TOKEN]: (state: State, action: ActionType): State => { const { tokenAddress, safeAddress } = action.payload return state.setIn([safeAddress, tokenAddress, 'status'], true) diff --git a/src/logic/tokens/utils/tokensStorage.js b/src/logic/tokens/utils/tokensStorage.js index babdde04..30f770ca 100644 --- a/src/logic/tokens/utils/tokensStorage.js +++ b/src/logic/tokens/utils/tokensStorage.js @@ -9,9 +9,9 @@ export const TOKENS_KEY = 'TOKENS' const getActiveTokensKey = (safeAddress: string) => `${ACTIVE_TOKENS_KEY}-${safeAddress}` const getTokensKey = (safeAddress: string) => `${TOKENS_KEY}-${safeAddress}` -export const setActiveTokenAddresses = async (safeAddress: string, tokens: List) => { +export const setActiveTokens = async (safeAddress: string, tokens: List) => { try { - const serializedState = JSON.stringify(tokens) + const serializedState = JSON.stringify(tokens.toJS()) const key = getActiveTokensKey(safeAddress) await ImmortalDB.set(key, serializedState) } catch (err) { @@ -20,7 +20,7 @@ export const setActiveTokenAddresses = async (safeAddress: string, tokens: List< } } -export const getActiveTokenAddresses = async (safeAddress: string): Promise> => { +export const getActiveTokens = async (safeAddress: string): Promise> => { const key = getActiveTokensKey(safeAddress) const data = await ImmortalDB.get(key) @@ -61,8 +61,8 @@ export const removeTokenFromStorage = async (safeAddress: string, token: Token) } } -export const removeFromActiveTokens = async (safeAddress: string, tokenAddress: string) => { +export const removeFromActiveTokens = async (safeAddress: string, token: Token) => { const activeTokens = await getActiveTokenAddresses(safeAddress) - const index = activeTokens.indexOf(tokenAddress) - await setActiveTokenAddresses(safeAddress, activeTokens.delete(index)) + const index = activeTokens.indexOf(token) + await setActiveTokens(safeAddress, activeTokens.delete(index)) }