load default safe on initial load wip

This commit is contained in:
Mikhail Mikheev 2019-09-19 13:30:13 +04:00
parent 05059dd8aa
commit 99fd7db46a
5 changed files with 55 additions and 35 deletions

View File

@ -7,6 +7,7 @@ import Root from '~/components/Root'
import { store } from '~/store'
import loadSafesFromStorage from '~/routes/safe/store/actions/loadSafesFromStorage'
import loadActiveTokens from '~/logic/tokens/store/actions/loadActiveTokens'
import loadDefaultSafe from '~/routes/safe/store/actions/loadDefaultSafe'
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line
@ -16,5 +17,6 @@ if (process.env.NODE_ENV !== 'production') {
store.dispatch(loadActiveTokens())
store.dispatch(loadSafesFromStorage())
store.dispatch(loadDefaultSafe())
ReactDOM.render(<Root />, document.getElementById('root'))

View File

@ -1,6 +1,8 @@
// @flow
import React from 'react'
import { connect } from 'react-redux'
import { Switch, Redirect, Route } from 'react-router-dom'
import { defaultSafeSelector } from '~/routes/safeList/store/selectors'
import Welcome from './welcome/container'
import {
SAFELIST_ADDRESS,
@ -23,9 +25,25 @@ const Load = React.lazy(() => import('./load/container/Load'))
const SAFE_ADDRESS = `${SAFELIST_ADDRESS}/:${SAFE_PARAM_ADDRESS}`
const Routes = () => (
type RoutesProps = {
defaultSafe?: string,
}
const Routes = ({ defaultSafe }: RoutesProps) => (
<Switch>
<Redirect exact from="/" to={WELCOME_ADDRESS} />
<Route
exact
path="/"
render={() => {
if (typeof defaultSafe === 'undefined') {
return 'Loading...'
}
if (defaultSafe) {
return <Redirect to={`${SAFELIST_ADDRESS}/${defaultSafe}`} />
}
return <Redirect to={WELCOME_ADDRESS} />
}}
/>
<Route exact path={WELCOME_ADDRESS} component={Welcome} />
<Route exact path={OPEN_ADDRESS} component={Open} />
<Route exact path={SAFELIST_ADDRESS} component={SafeList} />
@ -35,4 +53,8 @@ const Routes = () => (
</Switch>
)
export default Routes
export default connect<Object, Object, ?Function, ?Object>(
// $FlowFixMe
(state) => ({ defaultSafe: defaultSafeSelector(state) }),
null,
)(Routes)

View File

@ -0,0 +1,20 @@
// @flow
import type { Dispatch as ReduxDispatch } from 'redux'
import { type GlobalState } from '~/store/index'
import { getDefaultSafe } from '~/logic/safe/utils'
import setDefaultSafe from './setDefaultSafe'
const loadDefaultSafe = () => async (dispatch: ReduxDispatch<GlobalState>) => {
try {
const defaultSafe: ?string = await getDefaultSafe()
if (defaultSafe) {
dispatch(setDefaultSafe(defaultSafe))
}
} catch (err) {
// eslint-disable-next-line
console.error('Error while getting defautl safe from storage:', err)
}
}
export default loadDefaultSafe

View File

@ -2,11 +2,9 @@
import { Map, List } from 'immutable'
import { handleActions, type ActionType } from 'redux-actions'
import { ADD_SAFE, buildOwnersFrom } from '~/routes/safe/store/actions/addSafe'
import SafeRecord, { type Safe, type SafeProps } from '~/routes/safe/store/models/safe'
import SafeRecord, { type SafeProps } from '~/routes/safe/store/models/safe'
import TokenBalance from '~/routes/safe/store/models/tokenBalance'
import { makeOwner, type OwnerProps } from '~/routes/safe/store/models/owner'
import { loadFromStorage } from '~/utils/storage'
import { SAFES_KEY } from '~/logic/safe/utils'
import { UPDATE_SAFE } from '~/routes/safe/store/actions/updateSafe'
import { ACTIVATE_TOKEN_FOR_ALL_SAFES } from '~/routes/safe/store/actions/activateTokenForAllSafes'
import { REMOVE_SAFE } from '~/routes/safe/store/actions/removeSafe'
@ -18,7 +16,7 @@ import { SET_DEFAULT_SAFE } from '~/routes/safe/store/actions/setDefaultSafe'
export const SAFE_REDUCER_ID = 'safes'
type SafeReducerState = Map<string, *>
export type SafeReducerState = Map<string, *>
export const buildSafe = (storedSafe: SafeProps) => {
const names = storedSafe.owners.map((owner: OwnerProps) => owner.name)
@ -37,31 +35,6 @@ export const buildSafe = (storedSafe: SafeProps) => {
return safe
}
const buildSafesFrom = (loadedSafes: Object): Map<string, Safe> => {
const safes: Map<string, Safe> = Map()
const keys = Object.keys(loadedSafes)
try {
const safeRecords = keys.map((address: string) => buildSafe(loadedSafes[address]))
return safes.withMutations(async (map) => {
safeRecords.forEach((safe: SafeProps) => map.set(safe.address, safe))
})
} catch (err) {
// eslint-disable-next-line
console.log('Error while fetching safes information')
return Map()
}
}
export const safesInitialState = async (): Promise<State> => {
const storedSafes = await loadFromStorage(SAFES_KEY)
const safes = storedSafes ? buildSafesFrom(storedSafes) : Map()
return safes
}
export default handleActions<SafeReducerState, *>(
{
[UPDATE_SAFE]: (state: SafeReducerState, action: ActionType<Function>): SafeReducerState => {
@ -142,7 +115,7 @@ export default handleActions<SafeReducerState, *>(
},
Map({
// $FlowFixMe
defaultSafe: '',
defaultSafe: undefined,
safes: Map(),
}),
)

View File

@ -6,7 +6,7 @@ import {
} from 'redux'
import thunk from 'redux-thunk'
import provider, { PROVIDER_REDUCER_ID, type State as ProviderState } from '~/logic/wallets/store/reducer/provider'
import safe, { SAFE_REDUCER_ID, type State as SafeState } from '~/routes/safe/store/reducer/safe'
import safe, { SAFE_REDUCER_ID, type SafeReducerState as SafeState } from '~/routes/safe/store/reducer/safe'
import safeStorage from '~/routes/safe/store/middleware/safeStorage'
import tokens, { TOKEN_REDUCER_ID, type State as TokensState } from '~/logic/tokens/store/reducer/tokens'
import transactions, {
@ -37,6 +37,9 @@ const reducers: Reducer<GlobalState> = combineReducers({
[TRANSACTIONS_REDUCER_ID]: transactions,
})
export const store: Store<GlobalState> = createStore(reducers, finalCreateStore)
export const store: Store<GlobalState> = createStore(
reducers,
finalCreateStore,
)
export const aNewStore = (localState?: Object): Store<GlobalState> => createStore(reducers, localState, finalCreateStore)