MyCrypto/common/reducers/transactions.ts
William O'Beirne db6b737cad Show Recent Txs on Check Tx Page (#1147)
* Save transactions to local storage.

* Checksum more things + reset hash on network change.

* Fix IHexTransaction type, grab from from tx object directly.

* Refactor storage of recent transactions to use redux storage and loading.

* Refactor types to a transactions types file.

* Initial crack at recent transactions tab on account

* Punctuation.

* Transaction Status responsive behavior.

* Refactor transaction helper function out to remove circular dependency.

* Fix typings

* Collapse subtabs to select list when too small.

* s/wallet/address

* Type select onChange

* Get fields from current state if web3 tx
2018-03-14 15:10:14 -05:00

78 lines
1.8 KiB
TypeScript

import {
FetchTransactionDataAction,
SetTransactionDataAction,
AddRecentTransactionAction,
TransactionsAction,
TypeKeys
} from 'actions/transactions';
import { SavedTransaction, TransactionState } from 'types/transactions';
export interface State {
txData: { [txhash: string]: TransactionState };
recent: SavedTransaction[];
}
export const INITIAL_STATE: State = {
txData: {},
recent: []
};
function fetchTxData(state: State, action: FetchTransactionDataAction): State {
return {
...state,
txData: {
...state.txData,
[action.payload]: {
data: null,
receipt: null,
error: null,
isLoading: true
}
}
};
}
function setTxData(state: State, action: SetTransactionDataAction): State {
return {
...state,
txData: {
...state.txData,
[action.payload.txhash]: {
data: action.payload.data,
receipt: action.payload.receipt,
error: action.payload.error,
isLoading: false
}
}
};
}
function resetTxData(state: State): State {
return {
...state,
txData: INITIAL_STATE.txData
};
}
function addRecentTx(state: State, action: AddRecentTransactionAction): State {
return {
...state,
recent: [action.payload, ...state.recent].slice(0, 50)
};
}
export function transactions(state: State = INITIAL_STATE, action: TransactionsAction): State {
switch (action.type) {
case TypeKeys.TRANSACTIONS_FETCH_TRANSACTION_DATA:
return fetchTxData(state, action);
case TypeKeys.TRANSACTIONS_SET_TRANSACTION_DATA:
return setTxData(state, action);
case TypeKeys.TRANSACTIONS_RESET_TRANSACTION_DATA:
return resetTxData(state);
case TypeKeys.TRANSACTIONS_ADD_RECENT_TRANSACTION:
return addRecentTx(state, action);
default:
return state;
}
}