From f4ca495ea000527ca65b3084b7ea2b2ac5816267 Mon Sep 17 00:00:00 2001 From: apanizo Date: Tue, 22 May 2018 11:34:56 +0200 Subject: [PATCH] WA-238 Adding fetchTransactions action --- .../Transactions/test/transactions.test.js | 3 +- .../component/Transactions/transactions.js | 27 ++------------ .../safe/store/actions/fetchTransactions.js | 36 +++++++++++++++++++ src/routes/safe/store/reducer/transactions.js | 2 +- 4 files changed, 42 insertions(+), 26 deletions(-) create mode 100644 src/routes/safe/store/actions/fetchTransactions.js diff --git a/src/routes/safe/component/Transactions/test/transactions.test.js b/src/routes/safe/component/Transactions/test/transactions.test.js index 23e8fea4..029814bd 100644 --- a/src/routes/safe/component/Transactions/test/transactions.test.js +++ b/src/routes/safe/component/Transactions/test/transactions.test.js @@ -1,10 +1,11 @@ // @flow import { List, Map } from 'immutable' -import { createTransaction, loadSafeTransactions } from '~/routes/safe/component/Transactions/transactions' +import { createTransaction } from '~/routes/safe/component/Transactions/transactions' import { type Transaction } from '~/routes/safe/store/model/transaction' import { SafeFactory } from '~/routes/safe/store/test/builder/safe.builder' import { type Safe } from '~/routes/safe/store/model/safe' import { type Owner } from '~/routes/safe/store/model/owner' +import { loadSafeTransactions } from '~/routes/safe/store/actions/fetchTransactions' import { testSizeOfSafesWith, testSizeOfTransactions, testTransactionFrom } from './transactionsHelper' describe('Transactions Suite', () => { diff --git a/src/routes/safe/component/Transactions/transactions.js b/src/routes/safe/component/Transactions/transactions.js index 1bf5de41..d40ee7a6 100644 --- a/src/routes/safe/component/Transactions/transactions.js +++ b/src/routes/safe/component/Transactions/transactions.js @@ -1,8 +1,8 @@ // @flow -import { List, Map } from 'immutable' -import { type Owner, makeOwner } from '~/routes/safe/store/model/owner' +import { List } from 'immutable' +import { type Owner } from '~/routes/safe/store/model/owner' import { load, TX_KEY } from '~/utils/localStorage' -import { type Confirmation, type ConfirmationProps, makeConfirmation } from '~/routes/safe/store/model/confirmation' +import { type Confirmation, makeConfirmation } from '~/routes/safe/store/model/confirmation' import { makeTransaction, type Transaction, type TransactionProps } from '~/routes/safe/store/model/transaction' const buildConfirmationsFrom = (owners: List, creator: string): List => { @@ -52,24 +52,3 @@ export const createTransaction = ( localStorage.setItem(TX_KEY, JSON.stringify(safeTransactions)) } - -export const loadSafeTransactions = () => { - const safes = load(TX_KEY) || {} - - return Map().withMutations((map: Map>) => - Object.keys(safes).map((safe: string) => { - const safeTxs = safes[safe] - const safeTxsRecord = safeTxs.map((tx: TransactionProps) => { - const { confirmations } = tx - const txRecord = makeTransaction({ - ...tx, - confirmations: List(confirmations.map((conf: ConfirmationProps) => - makeConfirmation({ ...conf, owner: makeOwner(conf.owner) }))), - }) - - return txRecord - }) - - return map.set(safe, List(safeTxsRecord)) - })) -} diff --git a/src/routes/safe/store/actions/fetchTransactions.js b/src/routes/safe/store/actions/fetchTransactions.js new file mode 100644 index 00000000..11f33e13 --- /dev/null +++ b/src/routes/safe/store/actions/fetchTransactions.js @@ -0,0 +1,36 @@ +// @flow +import { List, Map } from 'immutable' +import type { Dispatch as ReduxDispatch } from 'redux' +import { type GlobalState } from '~/store/index' +import { makeOwner } from '~/routes/safe/store/model/owner' +import { makeTransaction, type Transaction, type TransactionProps } from '~/routes/safe/store/model/transaction' +import { load, TX_KEY } from '~/utils/localStorage' +import { type Confirmation, type ConfirmationProps, makeConfirmation } from '~/routes/safe/store/model/confirmation' +import addTransactions from './addTransactions' + +export const loadSafeTransactions = () => { + const safes = load(TX_KEY) || {} + + return Map().withMutations((map: Map>) => + Object.keys(safes).map((safe: string) => { + const safeTxs = safes[safe] + const safeTxsRecord = safeTxs.map((tx: TransactionProps) => { + const { confirmations } = tx + const txRecord = makeTransaction({ + ...tx, + confirmations: List(confirmations.map((conf: ConfirmationProps) => + makeConfirmation({ ...conf, owner: makeOwner(conf.owner) }))), + }) + + return txRecord + }) + + return map.set(safe, List(safeTxsRecord)) + })) +} + +export default () => async (dispatch: ReduxDispatch) => { + const transactions: Map> = await loadSafeTransactions() + + return dispatch(addTransactions(transactions)) +} diff --git a/src/routes/safe/store/reducer/transactions.js b/src/routes/safe/store/reducer/transactions.js index 81961a95..905eb764 100644 --- a/src/routes/safe/store/reducer/transactions.js +++ b/src/routes/safe/store/reducer/transactions.js @@ -3,7 +3,7 @@ import { List, Map } from 'immutable' import { handleActions, type ActionType } from 'redux-actions' import addTransactions, { ADD_TRANSACTIONS } from '~/routes/safe/store/actions/addTransactions' import { type Transaction } from '~/routes/safe/store/model/transaction' -import { loadSafeTransactions } from '~/routes/safe/component/Transactions/transactions' +import { loadSafeTransactions } from '~/routes/safe/store/actions/fetchTransactions' export const TRANSACTIONS_REDUCER_ID = 'transactions'