1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-15 21:08:57 +00:00
trezor-wallet/src/actions/SessionStorageActions.js

79 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2018-05-25 09:20:39 +00:00
/* @flow */
import * as storageUtils from 'utils/storage';
import { findToken } from 'reducers/utils';
2018-05-25 09:20:39 +00:00
2018-12-03 18:28:18 +00:00
import type { State as EthereumSendFormState } from 'reducers/SendFormEthereumReducer';
import type { State as RippleSendFormState } from 'reducers/SendFormRippleReducer';
2019-03-04 12:33:02 +00:00
import type { ThunkAction, PayloadAction, GetState, Dispatch } from 'flowtype';
2018-05-25 09:20:39 +00:00
const TYPE: 'session' = 'session';
const { STORAGE_PATH } = storageUtils;
const KEY_TX_DRAFT: string = `${STORAGE_PATH}txdraft`;
2018-05-25 09:20:39 +00:00
const getTxDraftKey = (getState: GetState): string => {
const { pathname } = getState().router.location;
return `${KEY_TX_DRAFT}${pathname}`;
};
2018-07-30 10:52:13 +00:00
2019-03-04 12:33:02 +00:00
export const saveDraftTransaction = (): ThunkAction => (
dispatch: Dispatch,
getState: GetState
): void => {
2018-11-29 20:06:35 +00:00
const state = getState().sendFormEthereum;
2018-09-22 16:47:31 +00:00
if (state.untouched) return;
const key = getTxDraftKey(getState);
storageUtils.set(TYPE, key, JSON.stringify(state));
2018-07-30 10:52:13 +00:00
};
2018-05-25 09:20:39 +00:00
2019-03-04 12:33:02 +00:00
export const loadEthereumDraftTransaction = (): PayloadAction<?EthereumSendFormState> => (
dispatch: Dispatch,
getState: GetState
): ?EthereumSendFormState => {
const key = getTxDraftKey(getState);
const value: ?string = storageUtils.get(TYPE, key);
if (!value) return null;
2018-12-03 18:28:18 +00:00
const state: ?EthereumSendFormState = JSON.parse(value);
if (!state) return null;
// decide if draft is valid and should be returned
// ignore this draft if has any error
if (Object.keys(state.errors).length > 0) {
storageUtils.remove(TYPE, key);
return null;
2018-05-25 09:20:39 +00:00
}
2018-11-21 15:24:22 +00:00
// check if selected currency is token and make sure that this token is added into account
if (state.currency !== state.networkSymbol) {
const { account, tokens } = getState().selectedAccount;
if (!account) return null;
const token = findToken(tokens, account.descriptor, state.currency, account.deviceState);
2018-11-21 15:24:22 +00:00
if (!token) {
storageUtils.remove(TYPE, key);
return null;
}
}
return state;
2018-07-30 10:52:13 +00:00
};
2018-05-25 09:20:39 +00:00
2019-03-04 12:33:02 +00:00
export const loadRippleDraftTransaction = (): PayloadAction<?RippleSendFormState> => (
dispatch: Dispatch,
getState: GetState
): ?RippleSendFormState => {
2018-12-03 18:28:18 +00:00
const key = getTxDraftKey(getState);
const value: ?string = storageUtils.get(TYPE, key);
if (!value) return null;
const state: ?RippleSendFormState = JSON.parse(value);
if (!state) return null;
// decide if draft is valid and should be returned
// ignore this draft if has any error
if (Object.keys(state.errors).length > 0) {
storageUtils.remove(TYPE, key);
return null;
}
return state;
};
2018-07-30 10:52:13 +00:00
export const clear = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
const key = getTxDraftKey(getState);
storageUtils.remove(TYPE, key);
};