mirror of
https://github.com/status-im/safe-react.git
synced 2025-02-20 05:28:08 +00:00
fix loading tokens from storage
This commit is contained in:
parent
81fbb3b662
commit
cdaa6c8223
@ -6,7 +6,9 @@ import ReactDOM from 'react-dom'
|
|||||||
import Root from '~/components/Root'
|
import Root from '~/components/Root'
|
||||||
import { store } from '~/store'
|
import { store } from '~/store'
|
||||||
import loadSafesFromStorage from '~/routes/safe/store/actions/loadSafesFromStorage'
|
import loadSafesFromStorage from '~/routes/safe/store/actions/loadSafesFromStorage'
|
||||||
|
import loadActiveTokens from '~/logic/tokens/store/actions/loadActiveTokens'
|
||||||
|
|
||||||
|
store.dispatch(loadActiveTokens())
|
||||||
store.dispatch(loadSafesFromStorage())
|
store.dispatch(loadSafesFromStorage())
|
||||||
|
|
||||||
ReactDOM.render(<Root />, document.getElementById('root'))
|
ReactDOM.render(<Root />, document.getElementById('root'))
|
||||||
|
@ -38,7 +38,7 @@ const fetchTokenList = async () => {
|
|||||||
return axios.get(url, errMsg)
|
return axios.get(url, errMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const fetchTokens = (safeAddress: string) => async (dispatch: ReduxDispatch<GlobalState>) => {
|
export const fetchTokens = () => async (dispatch: ReduxDispatch<GlobalState>) => {
|
||||||
try {
|
try {
|
||||||
const {
|
const {
|
||||||
data: { results: tokenList },
|
data: { results: tokenList },
|
||||||
@ -48,7 +48,7 @@ export const fetchTokens = (safeAddress: string) => async (dispatch: ReduxDispat
|
|||||||
tokenList.forEach((token: TokenProps) => map.set(token.address, makeToken(token)))
|
tokenList.forEach((token: TokenProps) => map.set(token.address, makeToken(token)))
|
||||||
})
|
})
|
||||||
|
|
||||||
dispatch(saveTokens(safeAddress, tokensMap))
|
dispatch(saveTokens(tokensMap))
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
console.log('Error fetching token list ' + err)
|
console.log('Error fetching token list ' + err)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
// @flow
|
// @flow
|
||||||
import type { Dispatch as ReduxDispatch } from 'redux'
|
import type { Dispatch as ReduxDispatch } from 'redux'
|
||||||
import { List, Map } from 'immutable'
|
import { Map, List } from 'immutable'
|
||||||
import { type TokenProps, type Token, makeToken } from '~/logic/tokens/store/model/token'
|
import { type TokenProps, type Token, makeToken } from '~/logic/tokens/store/model/token'
|
||||||
import { type GlobalState } from '~/store/index'
|
import { type GlobalState } from '~/store/index'
|
||||||
import { getActiveTokens } from '~/logic/tokens/utils/tokensStorage'
|
import { getActiveTokens } from '~/logic/tokens/utils/tokensStorage'
|
||||||
@ -8,13 +8,13 @@ import saveTokens from './saveTokens'
|
|||||||
|
|
||||||
const loadActiveTokens = () => async (dispatch: ReduxDispatch<GlobalState>) => {
|
const loadActiveTokens = () => async (dispatch: ReduxDispatch<GlobalState>) => {
|
||||||
try {
|
try {
|
||||||
const tokens: List<TokenProps> = await getActiveTokens()
|
const tokens: Map<string, TokenProps> = await getActiveTokens()
|
||||||
|
|
||||||
const tokenRecords: Map<string, Token> = Map().withMutations((map) => {
|
const tokenRecordsList: List<Token> = List(
|
||||||
tokens.forEach(token => map.set(token.address, makeToken(token)))
|
Object.values(tokens).map(token => makeToken(token)),
|
||||||
})
|
)
|
||||||
|
|
||||||
dispatch(saveTokens(tokenRecords))
|
dispatch(saveTokens(tokenRecordsList))
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
console.error('Error while loading active tokens from storage:', err)
|
console.error('Error while loading active tokens from storage:', err)
|
||||||
|
@ -1,24 +1,17 @@
|
|||||||
// @flow
|
// @flow
|
||||||
import { Map, List } from 'immutable'
|
import { Map } 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 { ensureOnceAsync } from '~/utils/singleton'
|
|
||||||
import { type GlobalState } from '~/store/index'
|
|
||||||
import { setActiveTokens } from '~/logic/tokens/utils/tokensStorage'
|
|
||||||
import { calculateActiveErc20TokensFrom } from '~/logic/tokens/utils/tokenHelpers'
|
|
||||||
|
|
||||||
export const ADD_TOKENS = 'ADD_TOKENS'
|
export const ADD_TOKENS = 'ADD_TOKENS'
|
||||||
|
|
||||||
type TokenProps = {
|
type TokenProps = {
|
||||||
safeAddress: string,
|
|
||||||
tokens: Map<string, Token>,
|
tokens: Map<string, Token>,
|
||||||
}
|
}
|
||||||
|
|
||||||
const addTokens = createAction<string, *, *>(
|
const addTokens = createAction<string, *, *>(
|
||||||
ADD_TOKENS,
|
ADD_TOKENS,
|
||||||
(safeAddress: string, tokens: Map<string, Token>): TokenProps => ({
|
(tokens: Map<string, Token>): TokenProps => ({
|
||||||
safeAddress,
|
|
||||||
tokens,
|
tokens,
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// @flow
|
// @flow
|
||||||
import { List } from 'immutable'
|
import { List, Map } from 'immutable'
|
||||||
import { type Token, type TokenProps } from '~/logic/tokens/store/model/token'
|
import { type Token, type TokenProps } from '~/logic/tokens/store/model/token'
|
||||||
import { loadFromStorage, saveToStorage } from '~/utils/storage'
|
import { loadFromStorage, saveToStorage } from '~/utils/storage'
|
||||||
|
|
||||||
@ -10,7 +10,7 @@ export const CUSTOM_TOKENS_KEY = 'CUSTOM_TOKENS'
|
|||||||
// to avoid iterating a large amount of data of tokens from the backend
|
// to avoid iterating a large amount of data of tokens from the backend
|
||||||
// Custom tokens should be saved too unless they're deleted (marking them as inactive doesn't count)
|
// Custom tokens should be saved too unless they're deleted (marking them as inactive doesn't count)
|
||||||
|
|
||||||
export const setActiveTokens = async (tokens: List<TokenProps>) => {
|
export const setActiveTokens = async (tokens: Map<string, Token>) => {
|
||||||
try {
|
try {
|
||||||
await saveToStorage(ACTIVE_TOKENS_KEY, tokens.toJS())
|
await saveToStorage(ACTIVE_TOKENS_KEY, tokens.toJS())
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -19,10 +19,10 @@ export const setActiveTokens = async (tokens: List<TokenProps>) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getActiveTokens = async (): Promise<List<TokenProps>> => {
|
export const getActiveTokens = async (): Promise<Map<string, TokenProps>> => {
|
||||||
const data = await loadFromStorage(ACTIVE_TOKENS_KEY)
|
const data = await loadFromStorage(ACTIVE_TOKENS_KEY)
|
||||||
|
|
||||||
return data ? List(data) : List()
|
return data || Map({})
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getCustomTokens = async (): Promise<List<TokenProps>> => {
|
export const getCustomTokens = async (): Promise<List<TokenProps>> => {
|
||||||
|
@ -4,7 +4,11 @@ import { UPDATE_SAFE } from '~/routes/safe/store/actions/updateSafe'
|
|||||||
import type { Store, AnyAction } from 'redux'
|
import type { Store, AnyAction } from 'redux'
|
||||||
import { type GlobalState } from '~/store/'
|
import { type GlobalState } from '~/store/'
|
||||||
import { saveSafes, setOwners } from '~/logic/safe/utils'
|
import { saveSafes, setOwners } from '~/logic/safe/utils'
|
||||||
import { safesMapSelector } from '~/routes/safeList/store/selectors/index'
|
import { safesMapSelector } from '~/routes/safeList/store/selectors'
|
||||||
|
import { getActiveTokensAddressesForAllSafes } from '~/routes/safe/store/selectors'
|
||||||
|
import { tokensSelector } from '~/logic/tokens/store/selectors'
|
||||||
|
import type { Token } from '~/logic/tokens/store/model/token'
|
||||||
|
import { setActiveTokens } from '~/logic/tokens/utils/tokensStorage'
|
||||||
|
|
||||||
const watchedActions = [ADD_SAFE, UPDATE_SAFE]
|
const watchedActions = [ADD_SAFE, UPDATE_SAFE]
|
||||||
|
|
||||||
@ -16,6 +20,22 @@ const safeStorageMware = (store: Store<GlobalState>) => (next: Function) => asyn
|
|||||||
const safes = safesMapSelector(state)
|
const safes = safesMapSelector(state)
|
||||||
saveSafes(safes.toJSON())
|
saveSafes(safes.toJSON())
|
||||||
|
|
||||||
|
// recalculate active tokens
|
||||||
|
if (action.payload.activeTokens) {
|
||||||
|
const tokens = tokensSelector(state)
|
||||||
|
const activeTokenAddresses = getActiveTokensAddressesForAllSafes(state)
|
||||||
|
|
||||||
|
const activeTokens = tokens.withMutations((map) => {
|
||||||
|
map.forEach((token: Token) => {
|
||||||
|
if (!activeTokenAddresses.has(token.address)) {
|
||||||
|
map.remove(token.address)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
setActiveTokens(activeTokens)
|
||||||
|
}
|
||||||
|
|
||||||
if (action.type === ADD_SAFE) {
|
if (action.type === ADD_SAFE) {
|
||||||
const { safe } = action.payload
|
const { safe } = action.payload
|
||||||
setOwners(safe.address, safe.owners)
|
setOwners(safe.address, safe.owners)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user