save whole tokens object in a reducer wip, fix flow type annotation in tokens reducer
This commit is contained in:
parent
633f8738b6
commit
718e84aea3
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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<string, Token>,
|
||||
}
|
||||
|
||||
const addTokens = createAction(
|
||||
export const addTokens = createAction(
|
||||
ADD_TOKENS,
|
||||
(safeAddress: string, tokens: Map<string, Token>): TokenProps => ({
|
||||
safeAddress,
|
||||
|
|
|
@ -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,
|
||||
}))
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -12,7 +12,7 @@ type RemoveTokenProps = {
|
|||
token: Token,
|
||||
}
|
||||
|
||||
const removeToken = createAction(
|
||||
export const removeToken = createAction(
|
||||
REMOVE_TOKEN,
|
||||
(safeAddress: string, token: Token): RemoveTokenProps => ({
|
||||
safeAddress,
|
||||
|
|
|
@ -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<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
|
||||
|
||||
return state.update(safeAddress, (prevSafe: Map<string, Token>) => {
|
||||
|
@ -25,24 +25,24 @@ export default handleActions(
|
|||
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 { address: 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 { address: tokenAddress } = token
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
return state.setIn([safeAddress, tokenAddress, 'status'], true)
|
||||
|
|
|
@ -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<string>) => {
|
||||
export const setActiveTokens = async (safeAddress: string, tokens: List<Token>) => {
|
||||
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<List<string>> => {
|
||||
export const getActiveTokens = async (safeAddress: string): Promise<List<Token>> => {
|
||||
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))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue