2018-03-08 16:10:53 +00:00
|
|
|
/* @flow */
|
2018-10-08 10:17:31 +00:00
|
|
|
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
|
|
|
|
2018-12-19 16:44:19 +00:00
|
|
|
import type { Action, Transaction } from 'flowtype';
|
2018-04-23 10:20:15 +00:00
|
|
|
|
2018-12-19 16:44:19 +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
|
|
|
|
2018-12-19 16:44:19 +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);
|
2018-10-08 10:17:31 +00:00
|
|
|
|
2018-12-03 19:22:49 +00:00
|
|
|
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) {
|
2018-12-03 19:22:49 +00:00
|
|
|
case CONNECT.FORGET:
|
|
|
|
case CONNECT.FORGET_SINGLE:
|
|
|
|
case CONNECT.FORGET_SILENT:
|
|
|
|
case CONNECT.RECEIVE_WALLET_TYPE:
|
|
|
|
return removeByDeviceState(state, action.device.state);
|
2018-10-08 10:17:31 +00:00
|
|
|
|
2018-11-30 17:55:57 +00:00
|
|
|
case PENDING.ADD:
|
2018-12-03 19:22:49 +00:00
|
|
|
return add(state, action.payload);
|
2018-07-30 10:52:13 +00:00
|
|
|
case PENDING.TX_RESOLVED:
|
2018-12-03 19:22:49 +00:00
|
|
|
return removeByHash(state, action.hash);
|
2018-09-26 16:46:49 +00:00
|
|
|
case PENDING.TX_REJECTED:
|
2018-11-30 17:55:57 +00:00
|
|
|
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
|
|
|
}
|