save whole tokens object in a reducer wip, fix flow type annotation in tokens reducer

This commit is contained in:
Mikhail Mikheev 2019-03-27 20:19:38 +04:00
parent 633f8738b6
commit 718e84aea3
7 changed files with 29 additions and 29 deletions

View File

@ -1,7 +1,7 @@
// @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/tokensStorage' import { setActiveTokens, getActiveTokenAddresses, setToken } from '~/logic/tokens/utils/tokensStorage'
import type { Dispatch as ReduxDispatch } from 'redux' import type { Dispatch as ReduxDispatch } from 'redux'
import { type GlobalState } from '~/store/index' import { type GlobalState } from '~/store/index'
@ -12,7 +12,7 @@ type AddTokenProps = {
token: Token, token: Token,
} }
const addToken = createAction( export const addToken = createAction(
ADD_TOKEN, ADD_TOKEN,
(safeAddress: string, token: Token): AddTokenProps => ({ (safeAddress: string, token: Token): AddTokenProps => ({
safeAddress, safeAddress,
@ -25,7 +25,7 @@ const saveToken = (safeAddress: string, token: Token) => async (dispatch: ReduxD
const tokenAddress = token.get('address') const tokenAddress = token.get('address')
const activeTokens = await getActiveTokenAddresses(safeAddress) const activeTokens = await getActiveTokenAddresses(safeAddress)
await setActiveTokenAddresses(safeAddress, activeTokens.push(tokenAddress)) await setActiveTokens(safeAddress, activeTokens.push(tokenAddress))
setToken(safeAddress, token) setToken(safeAddress, token)
} }

View File

@ -5,19 +5,19 @@ 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 { ensureOnceAsync } from '~/utils/singleton' import { ensureOnceAsync } from '~/utils/singleton'
import { type GlobalState } from '~/store/index' 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' import { calculateActiveErc20TokensFrom } from '~/logic/tokens/utils/tokenHelpers'
export const ADD_TOKENS = 'ADD_TOKENS' export const ADD_TOKENS = 'ADD_TOKENS'
const setTokensOnce = ensureOnceAsync(setActiveTokenAddresses) const setTokensOnce = ensureOnceAsync(setActiveTokens)
type TokenProps = { type TokenProps = {
safeAddress: string, safeAddress: string,
tokens: Map<string, Token>, tokens: Map<string, Token>,
} }
const addTokens = createAction( export const addTokens = createAction(
ADD_TOKENS, ADD_TOKENS,
(safeAddress: string, tokens: Map<string, Token>): TokenProps => ({ (safeAddress: string, tokens: Map<string, Token>): TokenProps => ({
safeAddress, safeAddress,

View File

@ -7,7 +7,7 @@ import { removeFromActiveTokens } from '~/logic/tokens/utils/tokensStorage'
export const DISABLE_TOKEN = 'DISABLE_TOKEN' 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, safeAddress,
tokenAddress, tokenAddress,
})) }))

View File

@ -3,11 +3,11 @@ import { createAction } from 'redux-actions'
import type { Dispatch as ReduxDispatch } from 'redux' import type { Dispatch as ReduxDispatch } from 'redux'
import { type GlobalState } from '~/store/index' import { type GlobalState } from '~/store/index'
import { type Token } from '~/logic/tokens/store/model/token' 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' 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, safeAddress,
tokenAddress, tokenAddress,
})) }))
@ -16,8 +16,8 @@ const setTokenEnabled = (safeAddress: string, token: Token) => async (dispatch:
const { address } = token const { address } = token
dispatch(enableToken(safeAddress, address)) dispatch(enableToken(safeAddress, address))
const activeTokens = await getActiveTokenAddresses(safeAddress) const activeTokens = await getActiveTokens(safeAddress)
await setActiveTokenAddresses(safeAddress, activeTokens.push(address)) await setActiveTokens(safeAddress, activeTokens.push(token))
} }
export default setTokenEnabled export default setTokenEnabled

View File

@ -12,7 +12,7 @@ type RemoveTokenProps = {
token: Token, token: Token,
} }
const removeToken = createAction( export const removeToken = createAction(
REMOVE_TOKEN, REMOVE_TOKEN,
(safeAddress: string, token: Token): RemoveTokenProps => ({ (safeAddress: string, token: Token): RemoveTokenProps => ({
safeAddress, safeAddress,

View File

@ -1,20 +1,20 @@
// @flow // @flow
import { 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 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 { type Token } from '~/logic/tokens/store/model/token'
import disableToken, { DISABLE_TOKEN } from '~/logic/tokens/store/actions/disableToken' import { ADD_TOKEN } from '~/logic/tokens/store/actions/addToken'
import enableToken, { ENABLE_TOKEN } from '~/logic/tokens/store/actions/enableToken' 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 const TOKEN_REDUCER_ID = 'tokens'
export type State = Map<string, Map<string, Token>> export type State = Map<string, Map<string, Token>>
export default handleActions( export default handleActions<State, *>(
{ {
[ADD_TOKENS]: (state: State, action: ActionType<typeof addTokens>): State => { [ADD_TOKENS]: (state: State, action: ActionType<Function>): State => {
const { safeAddress, tokens } = action.payload const { safeAddress, tokens } = action.payload
return state.update(safeAddress, (prevSafe: Map<string, Token>) => { return state.update(safeAddress, (prevSafe: Map<string, Token>) => {
@ -25,24 +25,24 @@ export default handleActions(
return prevSafe.equals(tokens) ? prevSafe : tokens return prevSafe.equals(tokens) ? prevSafe : tokens
}) })
}, },
[ADD_TOKEN]: (state: State, action: ActionType<typeof addToken>): State => { [ADD_TOKEN]: (state: State, action: ActionType<Function>): State => {
const { safeAddress, token } = action.payload const { safeAddress, token } = action.payload
const { address: tokenAddress } = token const { address: tokenAddress } = 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<Function>): State => {
const { safeAddress, token } = action.payload const { safeAddress, token } = action.payload
const { address: tokenAddress } = token const { address: tokenAddress } = 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<Function>): State => {
const { tokenAddress, safeAddress } = action.payload const { tokenAddress, safeAddress } = action.payload
return state.setIn([safeAddress, tokenAddress, 'status'], false) return state.setIn([safeAddress, tokenAddress, 'status'], false)
}, },
[ENABLE_TOKEN]: (state: State, action: ActionType<typeof enableToken>): State => { [ENABLE_TOKEN]: (state: State, action: ActionType<Function>): State => {
const { tokenAddress, safeAddress } = action.payload const { tokenAddress, safeAddress } = action.payload
return state.setIn([safeAddress, tokenAddress, 'status'], true) return state.setIn([safeAddress, tokenAddress, 'status'], true)

View File

@ -9,9 +9,9 @@ export const TOKENS_KEY = 'TOKENS'
const getActiveTokensKey = (safeAddress: string) => `${ACTIVE_TOKENS_KEY}-${safeAddress}` const getActiveTokensKey = (safeAddress: string) => `${ACTIVE_TOKENS_KEY}-${safeAddress}`
const getTokensKey = (safeAddress: string) => `${TOKENS_KEY}-${safeAddress}` const getTokensKey = (safeAddress: string) => `${TOKENS_KEY}-${safeAddress}`
export const setActiveTokenAddresses = async (safeAddress: string, tokens: List<string>) => { export const setActiveTokens = async (safeAddress: string, tokens: List<Token>) => {
try { try {
const serializedState = JSON.stringify(tokens) const serializedState = JSON.stringify(tokens.toJS())
const key = getActiveTokensKey(safeAddress) const key = getActiveTokensKey(safeAddress)
await ImmortalDB.set(key, serializedState) await ImmortalDB.set(key, serializedState)
} catch (err) { } catch (err) {
@ -20,7 +20,7 @@ export const setActiveTokenAddresses = async (safeAddress: string, tokens: List<
} }
} }
export const getActiveTokenAddresses = async (safeAddress: string): Promise<List<string>> => { export const getActiveTokens = async (safeAddress: string): Promise<List<Token>> => {
const key = getActiveTokensKey(safeAddress) const key = getActiveTokensKey(safeAddress)
const data = await ImmortalDB.get(key) 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 activeTokens = await getActiveTokenAddresses(safeAddress)
const index = activeTokens.indexOf(tokenAddress) const index = activeTokens.indexOf(token)
await setActiveTokenAddresses(safeAddress, activeTokens.delete(index)) await setActiveTokens(safeAddress, activeTokens.delete(index))
} }