1
0
mirror of https://github.com/trezor/trezor-wallet synced 2025-01-07 06:40:54 +00:00
trezor-wallet/src/js/actions/ModalActions.js

119 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-12-13 11:01:37 +00:00
/* @flow */
'use strict';
import TrezorConnect, { UI, UI_EVENT } from 'trezor-connect';
2018-04-11 10:13:38 +00:00
import * as MODAL from './constants/modal';
2018-02-20 09:30:36 +00:00
import * as CONNECT from './constants/TrezorConnect';
2017-12-13 11:01:37 +00:00
2018-05-02 09:01:08 +00:00
import type { ThunkAction, AsyncAction, Action, GetState, Dispatch, TrezorDevice } from '../flowtype';
import type { State } from '../reducers/ModalReducer';
2017-12-13 11:01:37 +00:00
2018-04-16 21:19:50 +00:00
export type ModalAction = {
type: typeof MODAL.CLOSE
} | {
type: typeof MODAL.REMEMBER,
2018-05-02 09:01:08 +00:00
device: TrezorDevice
2018-04-16 21:19:50 +00:00
};
export const onPinSubmit = (value: string): Action => {
TrezorConnect.uiResponse({ type: UI.RECEIVE_PIN, payload: value });
2017-12-13 11:01:37 +00:00
return {
type: MODAL.CLOSE
2017-12-13 11:01:37 +00:00
}
}
2018-04-16 21:19:50 +00:00
export const onPassphraseSubmit = (passphrase: string): AsyncAction => {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const resp = await TrezorConnect.uiResponse({
2018-02-20 09:30:36 +00:00
type: UI.RECEIVE_PASSPHRASE,
payload: {
2018-02-20 09:30:36 +00:00
value: passphrase,
save: true
}
});
dispatch({
type: MODAL.CLOSE
2018-02-20 09:30:36 +00:00
});
2017-12-13 11:01:37 +00:00
}
}
2018-04-23 10:20:15 +00:00
// export const askForRemember = (device: TrezorDevice): Action => {
// return {
// type: MODAL.REMEMBER,
// device
// }
// }
2017-12-13 11:01:37 +00:00
2018-04-23 10:20:15 +00:00
export const onRememberDevice = (device: TrezorDevice): Action => {
2017-12-13 11:01:37 +00:00
return {
2018-02-20 09:30:36 +00:00
type: CONNECT.REMEMBER,
device
2017-12-13 11:01:37 +00:00
}
}
2018-04-23 10:20:15 +00:00
export const onForgetDevice = (device: TrezorDevice): Action => {
2017-12-13 11:01:37 +00:00
return {
2018-02-20 09:30:36 +00:00
type: CONNECT.FORGET,
device,
2017-12-13 11:01:37 +00:00
}
}
2018-04-23 10:20:15 +00:00
export const onForgetSingleDevice = (device: TrezorDevice): Action => {
2017-12-13 11:01:37 +00:00
return {
2018-02-20 09:30:36 +00:00
type: CONNECT.FORGET_SINGLE,
device,
2017-12-13 11:01:37 +00:00
}
}
2018-04-16 21:19:50 +00:00
export const onCancel = (): Action => {
2017-12-13 11:01:37 +00:00
return {
type: MODAL.CLOSE
2017-12-13 11:01:37 +00:00
}
}
2018-05-02 09:01:08 +00:00
export const onDuplicateDevice = (device: TrezorDevice): ThunkAction => {
2018-04-16 21:19:50 +00:00
return (dispatch: Dispatch, getState: GetState): void => {
2017-12-13 11:01:37 +00:00
2018-02-20 09:30:36 +00:00
dispatch( onCancel() );
2017-12-13 11:01:37 +00:00
2018-02-20 09:30:36 +00:00
dispatch({
type: CONNECT.DUPLICATE,
device
});
2017-12-13 11:01:37 +00:00
}
2018-04-16 21:19:50 +00:00
}
2018-05-02 09:01:08 +00:00
export const onRememberRequest = (prevState: State): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => {
const state: State = getState().modal;
// handle case where forget modal is already opened
// TODO: 2 modals at once (two devices disconnected in the same time)
if (prevState.opened && prevState.windowType === CONNECT.REMEMBER_REQUEST) {
// forget current (new)
if (state.opened) {
dispatch({
type: CONNECT.FORGET,
device: state.device
});
}
// forget previous (old)
dispatch({
type: CONNECT.FORGET,
device: prevState.device
});
}
}
}
2018-04-16 21:19:50 +00:00
export default {
onPinSubmit,
onPassphraseSubmit,
2018-04-23 10:20:15 +00:00
// askForRemember,
2018-04-16 21:19:50 +00:00
onRememberDevice,
onForgetDevice,
onForgetSingleDevice,
onCancel,
onDuplicateDevice
2017-12-13 11:01:37 +00:00
}