From 477221d415590d915c21f79d2465c0b22cf1b662 Mon Sep 17 00:00:00 2001 From: Mikhail Mikheev Date: Fri, 29 Mar 2019 20:36:11 +0400 Subject: [PATCH] remove side effects from safes reducer wip --- src/routes/safe/store/actions/addSafe.js | 41 +++++++++++++++++------- src/routes/safe/store/reducer/safe.js | 2 +- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/routes/safe/store/actions/addSafe.js b/src/routes/safe/store/actions/addSafe.js index 5d0bed6f..2adf7986 100644 --- a/src/routes/safe/store/actions/addSafe.js +++ b/src/routes/safe/store/actions/addSafe.js @@ -1,7 +1,8 @@ // @flow import { List } from 'immutable' import { createAction } from 'redux-actions' -import { type SafeProps } from '~/routes/safe/store/model/safe' +import { type Safe, makeSafe } from '~/routes/safe/store/model/safe' +import { saveSafes, setOwners } from '~/utils/localStorage' import { makeOwner, type Owner } from '~/routes/safe/store/model/owner' export const ADD_SAFE = 'ADD_SAFE' @@ -12,18 +13,34 @@ export const buildOwnersFrom = (names: Array, addresses: Array) return List(owners) } -const addSafe = createAction( - ADD_SAFE, - (name: string, address: string, threshold: number, ownersName: string[], ownersAddress: string[]): SafeProps => { - const owners: List = buildOwnersFrom(ownersName, ownersAddress) +type ActionReturn = { + safe: Safe, +} - return { - address, - name, - threshold, - owners, - } - }, +const addSafe = createAction( + ADD_SAFE, + (safe: Safe): ActionReturn => ({ + safe, + }), ) +const saveSafe = ( + name: string, + address: string, + threshold: number, + ownersName: string[], + ownersAddress: string[], +) => async (dispatch: ReduxDispatch) => { + const owners: List = buildOwnersFrom(ownersName, ownersAddress) + + const safe: Safe = makeSafe({ + name, + address, + threshold, + owners, + }) + setOwners(safe.address, safe.owners) + saveSafes(safes.toJSON()) +} + export default addSafe diff --git a/src/routes/safe/store/reducer/safe.js b/src/routes/safe/store/reducer/safe.js index 2281e451..1eb15f16 100644 --- a/src/routes/safe/store/reducer/safe.js +++ b/src/routes/safe/store/reducer/safe.js @@ -73,7 +73,7 @@ export default handleActions( const safes = state.set(action.payload.address, safe) saveSafes(safes.toJSON()) - return safes + return state.set(action.payload.address, safe) }, }, Map(),