diff --git a/src/index.js b/src/index.js index 364987d1..a6fd93bf 100644 --- a/src/index.js +++ b/src/index.js @@ -10,12 +10,9 @@ import PageFrame from '~/components/layout/PageFrame' import { history, store } from '~/store' import theme from '~/theme/mui' import AppRoutes from '~/routes' -import fetchSafes from '~/routes/safe/store/actions/fetchSafes' import './index.scss' -store.dispatch(fetchSafes()) - const Root = () => ( diff --git a/src/routes/safe/store/actions/fetchSafes.js b/src/routes/safe/store/actions/fetchSafes.js deleted file mode 100644 index 45633a22..00000000 --- a/src/routes/safe/store/actions/fetchSafes.js +++ /dev/null @@ -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> => { - 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) => { - const storedSafes = load(SAFES_KEY) - - const safes = storedSafes ? await buildSafesFrom(storedSafes) : Map() - - return dispatch(updateSafes(safes)) -} diff --git a/src/routes/safe/store/actions/index.js b/src/routes/safe/store/actions/index.js deleted file mode 100644 index 0eb4a1c9..00000000 --- a/src/routes/safe/store/actions/index.js +++ /dev/null @@ -1,3 +0,0 @@ -// @flow -export * from './addSafe' -export { default as addSafe } from './addSafe' diff --git a/src/routes/safe/store/reducer/safe.js b/src/routes/safe/store/reducer/safe.js index 41832b32..d5c08adb 100644 --- a/src/routes/safe/store/reducer/safe.js +++ b/src/routes/safe/store/reducer/safe.js @@ -1,33 +1,61 @@ // @flow import { Map } from 'immutable' 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 { saveSafes, setOwners } from '~/utils/localStorage' -import updateSafes, { UPDATE_SAFES } from '~/routes/safe/store/actions/updateSafes' +import { saveSafes, setOwners, load, SAFES_KEY } from '~/utils/localStorage' import updateSafe, { UPDATE_SAFE } from '~/routes/safe/store/actions/updateSafe' export const SAFE_REDUCER_ID = 'safes' export type State = Map -/* -type Action = { - key: string, - payload: T, -}; +export const buildSafe = (storedSafe: SafeProps) => { + const owners = buildOwnersFrom( + storedSafe.owners.map((owner: OwnerProps) => owner.address), + storedSafe.owners.map((owner: OwnerProps) => owner.name), + ) -type AddSafeType = Action + const safe: SafeProps = { + address: storedSafe.address, + name: storedSafe.name, + threshold: storedSafe.threshold, + owners, + } + + return makeSafe(safe) +} + +const buildSafesFrom = (loadedSafes: Object): Promise> => { + 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({ [UPDATE_SAFE]: (state: State, action: ActionType): State => state.update(action.payload.get('address'), prevSafe => (prevSafe.equals(action.payload) ? prevSafe : action.payload)), - [UPDATE_SAFES]: (state: State, action: ActionType): State => - action.payload, [ADD_SAFE]: (state: State, action: ActionType): State => { const safe: Safe = makeSafe(action.payload) setOwners(safe.get('address'), safe.get('owners')) diff --git a/src/store/index.js b/src/store/index.js index 5078794a..4688fc21 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -4,7 +4,7 @@ import { routerMiddleware, routerReducer } from 'react-router-redux' import { combineReducers, createStore, applyMiddleware, compose, type Reducer, type Store } 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 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 transactions, { type State as TransactionsState, TRANSACTIONS_REDUCER_ID } from '~/routes/safe/store/reducer/transactions' @@ -32,13 +32,11 @@ const reducers: Reducer = combineReducers({ [TRANSACTIONS_REDUCER_ID]: transactions, }) -/* const initialState = { - [TRANSACTIONS_REDUCER_ID]: transactionsInitialState(), + [SAFE_REDUCER_ID]: safesInitialState(), } -*/ -export const store: Store = createStore(reducers, {}, finalCreateStore) +export const store: Store = createStore(reducers, initialState, finalCreateStore) export const aNewStore = (localState?: Object): Store => createStore(reducers, localState, finalCreateStore)