1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-12-04 06:08:10 +00:00
trezor-wallet/src/reducers/PendingTxReducer.js

52 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-03-08 16:10:53 +00:00
/* @flow */
import * as CONNECT from 'actions/constants/TrezorConnect';
2018-08-14 13:11:52 +00:00
import * as PENDING from 'actions/constants/pendingTx';
2018-03-08 16:10:53 +00:00
import type { Action, Transaction } from 'flowtype';
2018-04-23 10:20:15 +00:00
export type State = Array<Transaction>;
2018-04-23 10:20:15 +00:00
const initialState: State = [];
2018-03-08 16:10:53 +00:00
const add = (state: State, payload: Transaction): State => {
2018-07-30 10:52:13 +00:00
const newState = [...state];
2018-09-12 11:25:32 +00:00
newState.push(payload);
2018-03-08 16:10:53 +00:00
return newState;
2018-07-30 10:52:13 +00:00
};
2018-03-08 16:10:53 +00:00
2019-03-04 12:33:02 +00:00
const removeByDeviceState = (state: State, deviceState: ?string): State =>
state.filter(tx => tx.deviceState !== deviceState);
const removeByHash = (state: State, hash: string): State => state.filter(tx => tx.hash !== hash);
2018-03-08 16:10:53 +00:00
2019-03-04 12:33:02 +00:00
const reject = (state: State, hash: string): State =>
state.map(tx => {
if (tx.hash === hash && !tx.rejected) {
return { ...tx, rejected: true };
}
return tx;
});
2018-05-29 08:48:28 +00:00
2018-04-23 10:20:15 +00:00
export default function pending(state: State = initialState, action: Action): State {
2018-03-08 16:10:53 +00:00
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);
2018-07-30 10:52:13 +00:00
case PENDING.TX_RESOLVED:
return removeByHash(state, action.hash);
2018-09-26 16:46:49 +00:00
case PENDING.TX_REJECTED:
return reject(state, action.hash);
2018-03-08 16:10:53 +00:00
2018-07-30 10:52:13 +00:00
case PENDING.FROM_STORAGE:
2018-03-08 16:10:53 +00:00
return action.payload;
default:
return state;
}
2019-03-04 12:33:02 +00:00
}