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/js/actions/ModalActions.js

119 lines
2.9 KiB

7 years ago
/* @flow */
'use strict';
import TrezorConnect, { UI, UI_EVENT } from 'trezor-connect';
import * as MODAL from './constants/modal';
6 years ago
import * as CONNECT from './constants/TrezorConnect';
7 years ago
import type { ThunkAction, AsyncAction, Action, GetState, Dispatch, TrezorDevice } from '~/flowtype';
6 years ago
import type { State } from '../reducers/ModalReducer';
7 years ago
export type ModalAction = {
type: typeof MODAL.CLOSE
} | {
type: typeof MODAL.REMEMBER,
6 years ago
device: TrezorDevice
};
export const onPinSubmit = (value: string): Action => {
TrezorConnect.uiResponse({ type: UI.RECEIVE_PIN, payload: value });
7 years ago
return {
type: MODAL.CLOSE
7 years ago
}
}
export const onPassphraseSubmit = (passphrase: string): AsyncAction => {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const resp = await TrezorConnect.uiResponse({
6 years ago
type: UI.RECEIVE_PASSPHRASE,
payload: {
6 years ago
value: passphrase,
save: true
}
});
dispatch({
type: MODAL.CLOSE
6 years ago
});
7 years ago
}
}
6 years ago
// export const askForRemember = (device: TrezorDevice): Action => {
// return {
// type: MODAL.REMEMBER,
// device
// }
// }
7 years ago
6 years ago
export const onRememberDevice = (device: TrezorDevice): Action => {
7 years ago
return {
6 years ago
type: CONNECT.REMEMBER,
device
7 years ago
}
}
6 years ago
export const onForgetDevice = (device: TrezorDevice): Action => {
7 years ago
return {
6 years ago
type: CONNECT.FORGET,
device,
7 years ago
}
}
6 years ago
export const onForgetSingleDevice = (device: TrezorDevice): Action => {
7 years ago
return {
6 years ago
type: CONNECT.FORGET_SINGLE,
device,
7 years ago
}
}
export const onCancel = (): Action => {
7 years ago
return {
type: MODAL.CLOSE
7 years ago
}
}
6 years ago
export const onDuplicateDevice = (device: TrezorDevice): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => {
7 years ago
6 years ago
dispatch( onCancel() );
7 years ago
6 years ago
dispatch({
type: CONNECT.DUPLICATE,
device
});
7 years ago
}
}
6 years ago
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
});
}
}
}
export default {
onPinSubmit,
onPassphraseSubmit,
6 years ago
// askForRemember,
onRememberDevice,
onForgetDevice,
onForgetSingleDevice,
onCancel,
onDuplicateDevice
7 years ago
}