diff --git a/src/logic/tokens/store/actions/loadActiveTokens.js b/src/logic/tokens/store/actions/loadActiveTokens.js new file mode 100644 index 00000000..1e8abcbf --- /dev/null +++ b/src/logic/tokens/store/actions/loadActiveTokens.js @@ -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) => { + try { + const tokens: List = await getActiveTokens(safeAddress) + + if (tokens.size) { + const tokenRecords: Map = 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 diff --git a/src/routes/safe/container/actions.js b/src/routes/safe/container/actions.js index 5799093e..a1860317 100644 --- a/src/routes/safe/container/actions.js +++ b/src/routes/safe/container/actions.js @@ -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, } diff --git a/src/routes/safe/container/index.jsx b/src/routes/safe/container/index.jsx index d32d0ec1..dac25abc 100644 --- a/src/routes/safe/container/index.jsx +++ b/src/routes/safe/container/index.jsx @@ -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 { 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 { if (safe) { const safeAddress = safe.get('address') - fetchTokens(safeAddress) + loadActiveTokens(safeAddress) + // fetchTokenBalances(safeAddress) } } @@ -64,4 +70,7 @@ class SafeView extends React.PureComponent { } } -export default connect(selector, actions)(SafeView) +export default connect<*, *, *, *>( + selector, + actions, +)(SafeView) diff --git a/src/routes/safe/container/selector.js b/src/routes/safe/container/selector.js index 5e163366..e9c5e433 100644 --- a/src/routes/safe/container/selector.js +++ b/src/routes/safe/container/selector.js @@ -42,7 +42,7 @@ export const grantedSelector: Selector = crea }, ) -export default createStructuredSelector({ +export default createStructuredSelector({ safe: safeSelector, provider: providerNameSelector, tokens: orderedTokenListSelector, diff --git a/src/routes/safe/store/actions/loadSafesFromStorage.js b/src/routes/safe/store/actions/loadSafesFromStorage.js index 4453624d..1720b6b6 100644 --- a/src/routes/safe/store/actions/loadSafesFromStorage.js +++ b/src/routes/safe/store/actions/loadSafesFromStorage.js @@ -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) => { +const loadSafesFromStorage = () => async (dispatch: ReduxDispatch) => { try { const safes: ?{ [string]: SafeProps } = await loadFromStorage(SAFES_KEY) @@ -23,3 +23,5 @@ export default () => async (dispatch: ReduxDispatch) => { return Promise.resolve() } + +export default loadSafesFromStorage