You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trezor-wallet/src/reducers/PendingTxReducer.js

52 lines
1.5 KiB

6 years ago
/* @flow */
import * as CONNECT from 'actions/constants/TrezorConnect';
import * as PENDING from 'actions/constants/pendingTx';
6 years ago
import type { Action, Transaction } from 'flowtype';
6 years ago
export type State = Array<Transaction>;
6 years ago
const initialState: State = [];
6 years ago
const add = (state: State, payload: Transaction): State => {
const newState = [...state];
6 years ago
newState.push(payload);
6 years ago
return newState;
};
6 years ago
5 years ago
const removeByDeviceState = (state: State, deviceState: ?string): State =>
state.filter(tx => tx.deviceState !== deviceState);
const removeByHash = (state: State, hash: string): State => state.filter(tx => tx.txid !== hash);
6 years ago
5 years ago
const reject = (state: State, hash: string): State =>
state.map(tx => {
if (tx.txid === hash && !tx.rejected) {
return Object.assign({}, { rejected: true }, tx);
5 years ago
}
return tx;
});
6 years ago
export default function pending(state: State = initialState, action: Action): State {
6 years ago
switch (action.type) {
case CONNECT.FORGET:
case CONNECT.FORGET_SINGLE:
case CONNECT.FORGET_SILENT:
case CONNECT.RECEIVE_WALLET_TYPE:
return removeByDeviceState(state, action.device.state);
case PENDING.ADD:
return add(state, action.payload);
case PENDING.TX_RESOLVED:
return removeByHash(state, action.hash);
6 years ago
case PENDING.TX_REJECTED:
return reject(state, action.hash);
6 years ago
case PENDING.FROM_STORAGE:
6 years ago
return action.payload;
default:
return state;
}
5 years ago
}