1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-24 09:18:09 +00:00

updated SessionStorage actions

This commit is contained in:
Szymon Lesisz 2018-09-22 18:47:31 +02:00
parent 88d2a65340
commit 1846d936e2

View File

@ -4,47 +4,56 @@
import type { State as SendFormState } from 'reducers/SendFormReducer'; import type { State as SendFormState } from 'reducers/SendFormReducer';
import type { import type {
ThunkAction, ThunkAction,
PayloadAction,
GetState, GetState,
Dispatch, Dispatch,
} from 'flowtype'; } from 'flowtype';
const PREFIX: string = 'trezor:draft-tx:'; const TX_PREFIX: string = 'trezor:draft-tx:';
export const save = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => { export const saveDraftTransaction = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
if (typeof window.localStorage === 'undefined') return; if (typeof window.localStorage === 'undefined') return;
const location = getState().router.location.pathname;
const state = getState().sendForm; const state = getState().sendForm;
if (!state.untouched) { if (state.untouched) return;
const location = getState().router.location.pathname;
try { try {
window.sessionStorage.setItem(`${PREFIX}${location}`, JSON.stringify(state)); // save state as it is
// "loadDraftTransaction" will do the validation
window.sessionStorage.setItem(`${TX_PREFIX}${location}`, JSON.stringify(state));
} catch (error) { } catch (error) {
console.error(`Saving sessionStorage error: ${error}`); console.error(`Saving sessionStorage error: ${error}`);
} }
}
}; };
export const load = (location: string): ?SendFormState => { export const loadDraftTransaction = (): PayloadAction<?SendFormState> => (dispatch: Dispatch, getState: GetState): ?SendFormState => {
if (typeof window.localStorage === 'undefined') return; if (typeof window.localStorage === 'undefined') return null;
try { try {
const value: string = window.sessionStorage.getItem(`${PREFIX}${location}`); const location = getState().router.location.pathname;
const value: string = window.sessionStorage.getItem(`${TX_PREFIX}${location}`);
const state: ?SendFormState = JSON.parse(value); const state: ?SendFormState = JSON.parse(value);
if (state && state.address === '' && (state.amount === '' || state.amount === '0')) { if (state) {
window.sessionStorage.removeItem(`${PREFIX}${location}`); // decide if draft is valid and should be returned
return; // ignore this draft if has any error
if (Object.keys(state.errors).length > 0) {
window.sessionStorage.removeItem(`${TX_PREFIX}${location}`);
return null;
} }
return state; return state;
}
} catch (error) { } catch (error) {
console.error(`Loading sessionStorage error: ${error}`); console.error(`Loading sessionStorage error: ${error}`);
} }
return null;
}; };
export const clear = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => { export const clear = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
if (typeof window.localStorage === 'undefined') return; if (typeof window.localStorage === 'undefined') return;
const location = getState().router.location.pathname; const location = getState().router.location.pathname;
try { try {
window.sessionStorage.removeItem(`${PREFIX}${location}`); window.sessionStorage.removeItem(`${TX_PREFIX}${location}`);
} catch (error) { } catch (error) {
console.error(`Clearing sessionStorage error: ${error}`); console.error(`Clearing sessionStorage error: ${error}`);
} }