load active tokens from localstorage when visiting safe view
This commit is contained in:
parent
6003d8db21
commit
3b27729d85
|
@ -0,0 +1,26 @@
|
|||
// @flow
|
||||
import type { Dispatch as ReduxDispatch } from 'redux'
|
||||
import { List, Map } from 'immutable'
|
||||
import { type TokenProps, type Token, makeToken } from '~/logic/tokens/store/model/token'
|
||||
import { type GlobalState } from '~/store/index'
|
||||
import { getActiveTokens } from '~/logic/tokens/utils/tokensStorage'
|
||||
import saveTokens from './saveTokens'
|
||||
|
||||
const loadActiveTokens = (safeAddress: string) => async (dispatch: ReduxDispatch<GlobalState>) => {
|
||||
try {
|
||||
const tokens: List<TokenProps> = await getActiveTokens(safeAddress)
|
||||
|
||||
if (tokens.size) {
|
||||
const tokenRecords: Map<string, Token> = Map().withMutations((map) => {
|
||||
tokens.forEach(token => map.set(token.address, makeToken(token)))
|
||||
})
|
||||
|
||||
dispatch(saveTokens(safeAddress, tokenRecords))
|
||||
}
|
||||
} catch (err) {
|
||||
// eslint-disable-next-line
|
||||
console.error('Error while getting safes from storage:', err)
|
||||
}
|
||||
}
|
||||
|
||||
export default loadActiveTokens
|
|
@ -1,13 +1,16 @@
|
|||
// @flow
|
||||
import fetchSafe from '~/routes/safe/store/actions/fetchSafe'
|
||||
import { fetchTokens } from '~/logic/tokens/store/actions/fetchTokens'
|
||||
import loadActiveTokens from '~/logic/tokens/store/actions/loadActiveTokens'
|
||||
|
||||
export type Actions = {
|
||||
fetchSafe: typeof fetchSafe,
|
||||
fetchTokens: typeof fetchTokens,
|
||||
loadActiveTokens: typeof loadActiveTokens,
|
||||
}
|
||||
|
||||
export default {
|
||||
fetchSafe,
|
||||
fetchTokens,
|
||||
loadActiveTokens,
|
||||
}
|
||||
|
|
|
@ -6,26 +6,31 @@ import Layout from '~/routes/safe/component/Layout'
|
|||
import selector, { type SelectorProps } from './selector'
|
||||
import actions, { type Actions } from './actions'
|
||||
|
||||
type Props = Actions & SelectorProps & {
|
||||
granted: boolean,
|
||||
}
|
||||
type Props = Actions &
|
||||
SelectorProps & {
|
||||
granted: boolean,
|
||||
}
|
||||
|
||||
const TIMEOUT = process.env.NODE_ENV === 'test' ? 1500 : 15000
|
||||
|
||||
class SafeView extends React.PureComponent<Props> {
|
||||
componentDidMount() {
|
||||
const { safeUrl, fetchTokens, fetchSafe } = this.props
|
||||
const {
|
||||
safeUrl, fetchTokenBalances, fetchSafe, loadActiveTokens,
|
||||
} = this.props
|
||||
|
||||
fetchSafe(safeUrl)
|
||||
fetchTokens(safeUrl)
|
||||
loadActiveTokens(safeUrl)
|
||||
// fetchTokens(safeUrl)
|
||||
|
||||
this.intervalId = setInterval(() => {
|
||||
fetchTokens(safeUrl)
|
||||
// fetchTokenBalances(safeUrl)
|
||||
fetchSafe(safeUrl)
|
||||
}, TIMEOUT)
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps) {
|
||||
const { safe, fetchTokens } = this.props
|
||||
const { safe, fetchTokenBalances, loadActiveTokens } = this.props
|
||||
|
||||
if (prevProps.safe) {
|
||||
return
|
||||
|
@ -33,7 +38,8 @@ class SafeView extends React.PureComponent<Props> {
|
|||
|
||||
if (safe) {
|
||||
const safeAddress = safe.get('address')
|
||||
fetchTokens(safeAddress)
|
||||
loadActiveTokens(safeAddress)
|
||||
// fetchTokenBalances(safeAddress)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -64,4 +70,7 @@ class SafeView extends React.PureComponent<Props> {
|
|||
}
|
||||
}
|
||||
|
||||
export default connect(selector, actions)(SafeView)
|
||||
export default connect<*, *, *, *>(
|
||||
selector,
|
||||
actions,
|
||||
)(SafeView)
|
||||
|
|
|
@ -42,7 +42,7 @@ export const grantedSelector: Selector<GlobalState, RouterProps, boolean> = crea
|
|||
},
|
||||
)
|
||||
|
||||
export default createStructuredSelector({
|
||||
export default createStructuredSelector<Object, *>({
|
||||
safe: safeSelector,
|
||||
provider: providerNameSelector,
|
||||
tokens: orderedTokenListSelector,
|
||||
|
|
|
@ -7,7 +7,7 @@ import { loadFromStorage } from '~/utils/storage'
|
|||
import { addSafe } from './addSafe'
|
||||
import { buildSafe } from '~/routes/safe/store/reducer/safe'
|
||||
|
||||
export default () => async (dispatch: ReduxDispatch<GlobalState>) => {
|
||||
const loadSafesFromStorage = () => async (dispatch: ReduxDispatch<GlobalState>) => {
|
||||
try {
|
||||
const safes: ?{ [string]: SafeProps } = await loadFromStorage(SAFES_KEY)
|
||||
|
||||
|
@ -23,3 +23,5 @@ export default () => async (dispatch: ReduxDispatch<GlobalState>) => {
|
|||
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
export default loadSafesFromStorage
|
||||
|
|
Loading…
Reference in New Issue