From 2da7eab781abbef02be2fb7fdd209aca0f773de8 Mon Sep 17 00:00:00 2001 From: apanizo Date: Thu, 7 Jun 2018 22:25:32 +0200 Subject: [PATCH] WA-234 fetch safe info regularly in safe view --- src/routes/safe/container/actions.js | 6 +-- src/routes/safe/container/index.jsx | 9 +---- src/routes/safe/store/actions/fetchSafe.js | 42 +++++++++++++++++++++ src/routes/safe/store/actions/fetchSafes.js | 35 ++--------------- src/routes/safe/store/actions/updateSafe.js | 8 ++++ src/routes/safe/store/reducer/safe.js | 3 ++ 6 files changed, 61 insertions(+), 42 deletions(-) create mode 100644 src/routes/safe/store/actions/fetchSafe.js create mode 100644 src/routes/safe/store/actions/updateSafe.js diff --git a/src/routes/safe/container/actions.js b/src/routes/safe/container/actions.js index 4c0e607f..6ffe7123 100644 --- a/src/routes/safe/container/actions.js +++ b/src/routes/safe/container/actions.js @@ -1,13 +1,13 @@ // @flow +import fetchSafe from '~/routes/safe/store/actions/fetchSafe' import fetchBalance from '~/routes/safe/store/actions/fetchBalance' -import fetchDailyLimit from '~/routes/safe/store/actions/fetchDailyLimit' export type Actions = { + fetchSafe: typeof fetchSafe, fetchBalance: typeof fetchBalance, - fetchDailyLimit: typeof fetchDailyLimit, } export default { + fetchSafe, fetchBalance, - fetchDailyLimit, } diff --git a/src/routes/safe/container/index.jsx b/src/routes/safe/container/index.jsx index ea291ffc..d6579ead 100644 --- a/src/routes/safe/container/index.jsx +++ b/src/routes/safe/container/index.jsx @@ -14,17 +14,12 @@ type Props = Actions & SelectorProps & { class SafeView extends React.PureComponent { componentDidMount() { this.intervalId = setInterval(() => { - const { safe, fetchBalance } = this.props + const { safe, fetchSafe, fetchBalance } = this.props if (!safe) { return } - const safeAddress: string = safe.get('address') fetchBalance(safeAddress) + fetchSafe(safe) }, 1500) - - const { fetchDailyLimit, safe } = this.props - if (safe) { - fetchDailyLimit(safe.get('address')) - } } componentWillUnmount() { diff --git a/src/routes/safe/store/actions/fetchSafe.js b/src/routes/safe/store/actions/fetchSafe.js new file mode 100644 index 00000000..60b0cacf --- /dev/null +++ b/src/routes/safe/store/actions/fetchSafe.js @@ -0,0 +1,42 @@ +// @flow +import type { Dispatch as ReduxDispatch } from 'redux' +import { List } from 'immutable' +import { type GlobalState } from '~/store/index' +import { makeOwner } from '~/routes/safe/store/model/owner' +import { type SafeProps, type Safe, makeSafe } from '~/routes/safe/store/model/safe' +import { makeDailyLimit } from '~/routes/safe/store/model/dailyLimit' +import { getDailyLimitFrom } from '~/routes/safe/component/Withdrawn/withdrawn' +import { getGnosisSafeInstanceAt } from '~/wallets/safeContracts' +import updateSafe from '~/routes/safe/store/actions/updateSafe' + +const buildOwnersFrom = (safeOwners: string[], storedOwners: Object[]) => ( + safeOwners.map((ownerAddress: string) => { + const foundOwner = storedOwners.find(owner => owner.address === ownerAddress) + return makeOwner(foundOwner || { name: 'UNKNOWN', address: ownerAddress }) + }) +) + +export const buildSafe = async (storedSafe: Object) => { + const safeAddress = storedSafe.address + const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress) + + const dailyLimit = makeDailyLimit(await getDailyLimitFrom(safeAddress, 0)) + const threshold = Number(await gnosisSafe.getThreshold()) + const owners = List(buildOwnersFrom(await gnosisSafe.getOwners(), storedSafe.owners)) + + const safe: SafeProps = { + address: safeAddress, + dailyLimit, + name: storedSafe.name, + threshold, + owners, + } + + return makeSafe(safe) +} + +export default (safe: Safe) => async (dispatch: ReduxDispatch) => { + const safeRecord = await buildSafe(safe.toJSON()) + + return dispatch(updateSafe(safeRecord)) +} diff --git a/src/routes/safe/store/actions/fetchSafes.js b/src/routes/safe/store/actions/fetchSafes.js index b982ba92..196c1b40 100644 --- a/src/routes/safe/store/actions/fetchSafes.js +++ b/src/routes/safe/store/actions/fetchSafes.js @@ -1,40 +1,11 @@ // @flow import type { Dispatch as ReduxDispatch } from 'redux' -import { List, Map } from 'immutable' +import { Map } from 'immutable' import { type GlobalState } from '~/store/index' -import { makeOwner } from '~/routes/safe/store/model/owner' -import { type SafeProps, type Safe, makeSafe } from '~/routes/safe/store/model/safe' -import { makeDailyLimit } from '~/routes/safe/store/model/dailyLimit' -import { getDailyLimitFrom } from '~/routes/safe/component/Withdrawn/withdrawn' -import { getGnosisSafeInstanceAt } from '~/wallets/safeContracts' import { load, SAFES_KEY } from '~/utils/localStorage' import updateSafes from '~/routes/safe/store/actions/updateSafes' - -const buildOwnersFrom = (safeOwners: string[], storedOwners: Object[]) => ( - safeOwners.map((ownerAddress: string) => { - const foundOwner = storedOwners.find(owner => owner.address === ownerAddress) - return makeOwner(foundOwner || { name: 'UNKNOWN', address: ownerAddress }) - }) -) - -const buildSafe = async (storedSafe: Object) => { - const safeAddress = storedSafe.address - const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress) - - const dailyLimit = makeDailyLimit(await getDailyLimitFrom(safeAddress, 0)) - const threshold = Number(await gnosisSafe.getThreshold()) - const owners = List(buildOwnersFrom(await gnosisSafe.getOwners(), storedSafe.owners)) - - const safe: SafeProps = { - address: safeAddress, - dailyLimit, - name: storedSafe.name, - threshold, - owners, - } - - return makeSafe(safe) -} +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() diff --git a/src/routes/safe/store/actions/updateSafe.js b/src/routes/safe/store/actions/updateSafe.js new file mode 100644 index 00000000..89e30306 --- /dev/null +++ b/src/routes/safe/store/actions/updateSafe.js @@ -0,0 +1,8 @@ +// @flow +import { createAction } from 'redux-actions' + +export const UPDATE_SAFE = 'UPDATE_SAFE' + +const updateSafe = createAction(UPDATE_SAFE) + +export default updateSafe diff --git a/src/routes/safe/store/reducer/safe.js b/src/routes/safe/store/reducer/safe.js index d2a3dee0..bd585812 100644 --- a/src/routes/safe/store/reducer/safe.js +++ b/src/routes/safe/store/reducer/safe.js @@ -8,6 +8,7 @@ import { saveSafes } from '~/utils/localStorage' import { makeDailyLimit } from '~/routes/safe/store/model/dailyLimit' import updateThreshold, { UPDATE_THRESHOLD } from '~/routes/safe/store/actions/updateThreshold' import updateSafes, { UPDATE_SAFES } from '~/routes/safe/store/actions/updateSafes' +import updateSafe, { UPDATE_SAFE } from '~/routes/safe/store/actions/updateSafe' export const SAFE_REDUCER_ID = 'safes' @@ -25,6 +26,8 @@ action: AddSafeType */ export default handleActions({ + [UPDATE_SAFE]: (state: State, action: ActionType): State => + state.set(action.payload.get('address'), action.payload), [UPDATE_SAFES]: (state: State, action: ActionType): State => action.payload, [ADD_SAFE]: (state: State, action: ActionType): State => {