#73 Adding Safes model to redux store when loading the app
This commit is contained in:
parent
984d54dfad
commit
c5e378be6e
|
@ -10,12 +10,9 @@ import PageFrame from '~/components/layout/PageFrame'
|
||||||
import { history, store } from '~/store'
|
import { history, store } from '~/store'
|
||||||
import theme from '~/theme/mui'
|
import theme from '~/theme/mui'
|
||||||
import AppRoutes from '~/routes'
|
import AppRoutes from '~/routes'
|
||||||
import fetchSafes from '~/routes/safe/store/actions/fetchSafes'
|
|
||||||
|
|
||||||
import './index.scss'
|
import './index.scss'
|
||||||
|
|
||||||
store.dispatch(fetchSafes())
|
|
||||||
|
|
||||||
const Root = () => (
|
const Root = () => (
|
||||||
<Provider store={store}>
|
<Provider store={store}>
|
||||||
<MuiThemeProvider theme={theme}>
|
<MuiThemeProvider theme={theme}>
|
||||||
|
|
|
@ -1,34 +0,0 @@
|
||||||
// @flow
|
|
||||||
import type { Dispatch as ReduxDispatch } from 'redux'
|
|
||||||
import { Map } from 'immutable'
|
|
||||||
import { type GlobalState } from '~/store/index'
|
|
||||||
import { load, SAFES_KEY } from '~/utils/localStorage'
|
|
||||||
import updateSafes from '~/routes/safe/store/actions/updateSafes'
|
|
||||||
import { buildSafe } from '~/routes/safe/store/actions/fetchSafe'
|
|
||||||
import { type Safe } from '~/routes/safe/store/model/safe'
|
|
||||||
|
|
||||||
const buildSafesFrom = async (loadedSafes: Object): Promise<Map<string, Safe>> => {
|
|
||||||
const safes = Map()
|
|
||||||
|
|
||||||
const keys = Object.keys(loadedSafes)
|
|
||||||
try {
|
|
||||||
const safeRecords = await Promise.all(keys.map((address: string) => buildSafe(loadedSafes[address])))
|
|
||||||
|
|
||||||
return safes.withMutations(async (map) => {
|
|
||||||
safeRecords.forEach((safe: Safe) => map.set(safe.get('address'), safe))
|
|
||||||
})
|
|
||||||
} catch (err) {
|
|
||||||
// eslint-disable-next-line
|
|
||||||
console.log("Error while fetching safes information")
|
|
||||||
|
|
||||||
return Map()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default () => async (dispatch: ReduxDispatch<GlobalState>) => {
|
|
||||||
const storedSafes = load(SAFES_KEY)
|
|
||||||
|
|
||||||
const safes = storedSafes ? await buildSafesFrom(storedSafes) : Map()
|
|
||||||
|
|
||||||
return dispatch(updateSafes(safes))
|
|
||||||
}
|
|
|
@ -1,3 +0,0 @@
|
||||||
// @flow
|
|
||||||
export * from './addSafe'
|
|
||||||
export { default as addSafe } from './addSafe'
|
|
|
@ -1,33 +1,61 @@
|
||||||
// @flow
|
// @flow
|
||||||
import { Map } from 'immutable'
|
import { Map } from 'immutable'
|
||||||
import { handleActions, type ActionType } from 'redux-actions'
|
import { handleActions, type ActionType } from 'redux-actions'
|
||||||
import addSafe, { ADD_SAFE } from '~/routes/safe/store/actions/addSafe'
|
import addSafe, { ADD_SAFE, buildOwnersFrom } from '~/routes/safe/store/actions/addSafe'
|
||||||
import { type Safe, makeSafe } from '~/routes/safe/store/model/safe'
|
import { type Safe, makeSafe } from '~/routes/safe/store/model/safe'
|
||||||
import { saveSafes, setOwners } from '~/utils/localStorage'
|
import { saveSafes, setOwners, load, SAFES_KEY } from '~/utils/localStorage'
|
||||||
import updateSafes, { UPDATE_SAFES } from '~/routes/safe/store/actions/updateSafes'
|
|
||||||
import updateSafe, { UPDATE_SAFE } from '~/routes/safe/store/actions/updateSafe'
|
import updateSafe, { UPDATE_SAFE } from '~/routes/safe/store/actions/updateSafe'
|
||||||
|
|
||||||
export const SAFE_REDUCER_ID = 'safes'
|
export const SAFE_REDUCER_ID = 'safes'
|
||||||
|
|
||||||
export type State = Map<string, Safe>
|
export type State = Map<string, Safe>
|
||||||
|
|
||||||
/*
|
export const buildSafe = (storedSafe: SafeProps) => {
|
||||||
type Action<T> = {
|
const owners = buildOwnersFrom(
|
||||||
key: string,
|
storedSafe.owners.map((owner: OwnerProps) => owner.address),
|
||||||
payload: T,
|
storedSafe.owners.map((owner: OwnerProps) => owner.name),
|
||||||
};
|
)
|
||||||
|
|
||||||
type AddSafeType = Action<SafeProps>
|
const safe: SafeProps = {
|
||||||
|
address: storedSafe.address,
|
||||||
|
name: storedSafe.name,
|
||||||
|
threshold: storedSafe.threshold,
|
||||||
|
owners,
|
||||||
|
}
|
||||||
|
|
||||||
|
return makeSafe(safe)
|
||||||
|
}
|
||||||
|
|
||||||
|
const buildSafesFrom = (loadedSafes: Object): Promise<Map<string, Safe>> => {
|
||||||
|
const safes = Map()
|
||||||
|
|
||||||
|
const keys = Object.keys(loadedSafes)
|
||||||
|
try {
|
||||||
|
const safeRecords = keys.map((address: string) => buildSafe(loadedSafes[address]))
|
||||||
|
|
||||||
|
return safes.withMutations(async (map) => {
|
||||||
|
safeRecords.forEach((safe: Safe) => map.set(safe.get('address'), safe))
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
// eslint-disable-next-line
|
||||||
|
console.log("Error while fetching safes information")
|
||||||
|
|
||||||
|
return Map()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const safesInitialState = (): State => {
|
||||||
|
const storedSafes = load(SAFES_KEY)
|
||||||
|
const safes = storedSafes ? buildSafesFrom(storedSafes) : Map()
|
||||||
|
|
||||||
|
return safes
|
||||||
|
}
|
||||||
|
|
||||||
action: AddSafeType
|
|
||||||
*/
|
|
||||||
|
|
||||||
export default handleActions({
|
export default handleActions({
|
||||||
[UPDATE_SAFE]: (state: State, action: ActionType<typeof updateSafe>): State =>
|
[UPDATE_SAFE]: (state: State, action: ActionType<typeof updateSafe>): State =>
|
||||||
state.update(action.payload.get('address'), prevSafe =>
|
state.update(action.payload.get('address'), prevSafe =>
|
||||||
(prevSafe.equals(action.payload) ? prevSafe : action.payload)),
|
(prevSafe.equals(action.payload) ? prevSafe : action.payload)),
|
||||||
[UPDATE_SAFES]: (state: State, action: ActionType<typeof updateSafes>): State =>
|
|
||||||
action.payload,
|
|
||||||
[ADD_SAFE]: (state: State, action: ActionType<typeof addSafe>): State => {
|
[ADD_SAFE]: (state: State, action: ActionType<typeof addSafe>): State => {
|
||||||
const safe: Safe = makeSafe(action.payload)
|
const safe: Safe = makeSafe(action.payload)
|
||||||
setOwners(safe.get('address'), safe.get('owners'))
|
setOwners(safe.get('address'), safe.get('owners'))
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { routerMiddleware, routerReducer } from 'react-router-redux'
|
||||||
import { combineReducers, createStore, applyMiddleware, compose, type Reducer, type Store } from 'redux'
|
import { combineReducers, createStore, applyMiddleware, compose, type Reducer, type Store } from 'redux'
|
||||||
import thunk from 'redux-thunk'
|
import thunk from 'redux-thunk'
|
||||||
import provider, { PROVIDER_REDUCER_ID, type State as ProviderState } from '~/logic/wallets/store/reducer/provider'
|
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 State as SafeState, safesInitialState } from '~/routes/safe/store/reducer/safe'
|
||||||
import tokens, { TOKEN_REDUCER_ID, type State as TokensState } from '~/routes/tokens/store/reducer/tokens'
|
import tokens, { TOKEN_REDUCER_ID, type State as TokensState } from '~/routes/tokens/store/reducer/tokens'
|
||||||
import transactions, { type State as TransactionsState, TRANSACTIONS_REDUCER_ID } from '~/routes/safe/store/reducer/transactions'
|
import transactions, { type State as TransactionsState, TRANSACTIONS_REDUCER_ID } from '~/routes/safe/store/reducer/transactions'
|
||||||
|
|
||||||
|
@ -32,13 +32,11 @@ const reducers: Reducer<GlobalState> = combineReducers({
|
||||||
[TRANSACTIONS_REDUCER_ID]: transactions,
|
[TRANSACTIONS_REDUCER_ID]: transactions,
|
||||||
})
|
})
|
||||||
|
|
||||||
/*
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
[TRANSACTIONS_REDUCER_ID]: transactionsInitialState(),
|
[SAFE_REDUCER_ID]: safesInitialState(),
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
export const store: Store<GlobalState> = createStore(reducers, {}, finalCreateStore)
|
export const store: Store<GlobalState> = createStore(reducers, initialState, finalCreateStore)
|
||||||
|
|
||||||
export const aNewStore = (localState?: Object): Store<GlobalState> =>
|
export const aNewStore = (localState?: Object): Store<GlobalState> =>
|
||||||
createStore(reducers, localState, finalCreateStore)
|
createStore(reducers, localState, finalCreateStore)
|
||||||
|
|
Loading…
Reference in New Issue