1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-12-29 02:18:06 +00:00
trezor-wallet/src/reducers/ModalReducer.js

97 lines
2.7 KiB
JavaScript
Raw Normal View History

2017-12-13 11:01:37 +00:00
/* @flow */
2018-07-30 10:52:13 +00:00
2017-12-13 11:01:37 +00:00
import { UI, DEVICE } from 'trezor-connect';
2018-08-14 13:11:52 +00:00
import * as RECEIVE from 'actions/constants/receive';
import * as MODAL from 'actions/constants/modal';
import * as CONNECT from 'actions/constants/TrezorConnect';
2017-12-13 11:01:37 +00:00
2018-08-14 12:56:47 +00:00
import type { Action, TrezorDevice } from 'flowtype';
2018-04-23 10:20:15 +00:00
2018-04-16 21:19:50 +00:00
export type State = {
context: typeof MODAL.CONTEXT_NONE;
2018-05-02 09:01:08 +00:00
} | {
context: typeof MODAL.CONTEXT_DEVICE,
2018-05-02 09:01:08 +00:00
device: TrezorDevice;
instances?: Array<TrezorDevice>;
windowType?: string;
} | {
context: typeof MODAL.CONTEXT_EXTERNAL_WALLET,
windowType?: string;
2017-12-13 11:01:37 +00:00
}
2018-04-16 21:19:50 +00:00
const initialState: State = {
context: MODAL.CONTEXT_NONE,
2017-12-13 11:01:37 +00:00
};
2018-04-23 10:20:15 +00:00
export default function modal(state: State = initialState, action: Action): State {
2017-12-13 11:01:37 +00:00
switch (action.type) {
2018-07-30 10:52:13 +00:00
case RECEIVE.REQUEST_UNVERIFIED:
case CONNECT.FORGET_REQUEST:
case CONNECT.TRY_TO_DUPLICATE:
2018-10-05 13:47:51 +00:00
case CONNECT.REQUEST_WALLET_TYPE:
return {
context: MODAL.CONTEXT_DEVICE,
2018-10-05 13:47:51 +00:00
device: action.device,
windowType: action.type,
};
2018-07-30 10:52:13 +00:00
case CONNECT.REMEMBER_REQUEST:
2018-04-23 10:20:15 +00:00
return {
context: MODAL.CONTEXT_DEVICE,
2018-04-23 10:20:15 +00:00
device: action.device,
instances: action.instances,
2018-07-30 10:52:13 +00:00
windowType: action.type,
2018-04-23 10:20:15 +00:00
};
2017-12-13 11:01:37 +00:00
2018-10-11 11:07:58 +00:00
// device connected
// close modal if modal context is not 'device'
case DEVICE.CONNECT:
case DEVICE.CONNECT_UNACQUIRED:
if (state.context !== MODAL.CONTEXT_DEVICE) {
return initialState;
}
return state;
// device with context assigned to modal was disconnected
// close modal
2018-07-30 10:52:13 +00:00
case DEVICE.DISCONNECT:
if (state.context === MODAL.CONTEXT_DEVICE && action.device.path === state.device.path) {
2018-07-30 10:52:13 +00:00
return initialState;
2017-12-13 11:01:37 +00:00
}
2018-02-20 09:30:36 +00:00
return state;
2017-12-13 11:01:37 +00:00
2018-07-30 10:52:13 +00:00
case UI.REQUEST_PIN:
case UI.INVALID_PIN:
case UI.REQUEST_PASSPHRASE:
2017-12-13 11:01:37 +00:00
return {
context: MODAL.CONTEXT_DEVICE,
2018-05-02 09:01:08 +00:00
device: action.payload.device,
2018-07-30 10:52:13 +00:00
windowType: action.type,
2018-02-20 09:30:36 +00:00
};
2018-07-30 10:52:13 +00:00
case UI.REQUEST_BUTTON:
2017-12-13 11:01:37 +00:00
return {
context: MODAL.CONTEXT_DEVICE,
2018-05-02 09:01:08 +00:00
device: action.payload.device,
2018-07-30 10:52:13 +00:00
windowType: action.payload.code,
};
case UI.CLOSE_UI_WINDOW:
case MODAL.CLOSE:
case CONNECT.FORGET:
case CONNECT.FORGET_SINGLE:
case CONNECT.REMEMBER:
2018-05-02 09:01:08 +00:00
return initialState;
2018-02-20 09:30:36 +00:00
case MODAL.OPEN_EXTERNAL_WALLET:
return {
context: MODAL.CONTEXT_EXTERNAL_WALLET,
windowType: action.id,
};
2017-12-13 11:01:37 +00:00
default:
return state;
}
}