1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-28 03:08:30 +00:00

save and clear SessionStorage called from SendformActions

This commit is contained in:
Szymon Lesisz 2018-05-29 10:47:48 +02:00
parent 4d43a4acc4
commit b41e6bacc0
5 changed files with 52 additions and 31 deletions

View File

@ -42,9 +42,9 @@ export const updateSelectedValues = (prevState: State, action: Action): AsyncAct
// reset form to default // reset form to default
if (action.type === SEND.TX_COMPLETE) { if (action.type === SEND.TX_COMPLETE) {
dispatch( SendFormActions.init() ); // dispatch( SendFormActions.init() );
// linear action // linear action
SessionStorageActions.clear(location.pathname); // SessionStorageActions.clear(location.pathname);
} }
// handle devices state change (from trezor-connect events or location change) // handle devices state change (from trezor-connect events or location change)
@ -57,14 +57,6 @@ export const updateSelectedValues = (prevState: State, action: Action): AsyncAct
if (locationChange) { if (locationChange) {
if (prevLocation) {
// save form data to session storage
// TODO: move to state.sendForm on change event
if (prevLocation.state.send) {
SessionStorageActions.save(prevState.router.location.pathname, state.sendForm);
}
}
// dispose current account view // dispose current account view
dispatch( dispose() ); dispatch( dispose() );
} }
@ -101,7 +93,7 @@ export const updateSelectedValues = (prevState: State, action: Action): AsyncAct
// initialize SendFormReducer // initialize SendFormReducer
if (location.state.send && getState().sendForm.currency === "") { if (location.state.send && getState().sendForm.currency === "") {
dispatch( SendFormActions.init( SessionStorageActions.load(location.pathname) ) ); dispatch( SendFormActions.init() );
} }
} }
} }

View File

@ -4,6 +4,8 @@
import * as SEND from './constants/send'; import * as SEND from './constants/send';
import * as NOTIFICATION from './constants/notification'; import * as NOTIFICATION from './constants/notification';
import * as SessionStorageActions from './SessionStorageActions';
import { estimateGas, getGasPrice, pushTx } from './Web3Actions'; import { estimateGas, getGasPrice, pushTx } from './Web3Actions';
import EthereumjsUtil from 'ethereumjs-util'; import EthereumjsUtil from 'ethereumjs-util';
@ -150,7 +152,6 @@ export const calculateMaxAmount = (balance: BigNumber, gasPrice: string, gasLimi
} }
export const calculate = (prevProps: Props, props: Props) => { export const calculate = (prevProps: Props, props: Props) => {
const { const {
account, account,
tokens, tokens,
@ -236,7 +237,7 @@ export const getFeeLevels = (symbol: string, gasPrice: BigNumber | string, gasLi
// initialize component // initialize component
export const init = (stateFromStorage: ?State): ThunkAction => { export const init = (): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => { return (dispatch: Dispatch, getState: GetState): void => {
const { const {
@ -247,6 +248,7 @@ export const init = (stateFromStorage: ?State): ThunkAction => {
if (!account || !network || !web3) return; if (!account || !network || !web3) return;
const stateFromStorage = SessionStorageActions.load( getState().router.location.pathname );
if (stateFromStorage) { if (stateFromStorage) {
dispatch({ dispatch({
type: SEND.INIT, type: SEND.INIT,
@ -855,7 +857,7 @@ export const onSend = (): AsyncAction => {
const pendingNonce: number = stateUtils.getPendingNonce(pending); const pendingNonce: number = stateUtils.getPendingNonce(pending);
const nonce = pendingNonce >= account.nonce ? pendingNonce + 1 : account.nonce; const nonce = pendingNonce >= account.nonce ? pendingNonce + 1 : account.nonce;
console.warn("NONCEE", nonce, account.nonce, stateUtils.getPendingNonce(pending)) console.warn("NONCE", nonce, account.nonce, stateUtils.getPendingNonce(pending))
const txData = { const txData = {
address_n, address_n,
@ -930,6 +932,13 @@ export const onSend = (): AsyncAction => {
txData, txData,
}); });
// clear session storage
dispatch( SessionStorageActions.clear() );
// reset form
dispatch( init() );
dispatch({ dispatch({
type: NOTIFICATION.ADD, type: NOTIFICATION.ADD,
payload: { payload: {

View File

@ -2,18 +2,26 @@
'use strict'; 'use strict';
import type { State as SendFormState } from '../reducers/SendFormReducer'; import type { State as SendFormState } from '../reducers/SendFormReducer';
import type {
ThunkAction,
GetState,
Dispatch,
} from '~/flowtype';
const PREFIX: string = 'trezor:draft-tx:'; const PREFIX: string = 'trezor:draft-tx:';
export const save = (location: string, state: SendFormState): void => { export const save = (): ThunkAction => {
return (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;
if (!state.untouched) { if (!state.untouched) {
try { try {
window.sessionStorage.setItem(`${PREFIX}${location}`, JSON.stringify(state) ); window.sessionStorage.setItem(`${PREFIX}${location}`, JSON.stringify(state) );
} catch (error) { } catch (error) {
console.error("Saving sessionStorage error: " + error) console.error("Saving sessionStorage error: " + error)
}
} }
} }
} }
@ -24,7 +32,12 @@ export const load = (location: string): ?SendFormState => {
try { try {
const value: string = window.sessionStorage.getItem(`${PREFIX}${location}`); const value: string = window.sessionStorage.getItem(`${PREFIX}${location}`);
return JSON.parse(value); const state: ?SendFormState = JSON.parse(value);
if (state && state.address === '' && (state.amount === '' || state.amount === '0')) {
window.sessionStorage.removeItem(`${PREFIX}${location}`);
return;
}
return state;
} catch (error) { } catch (error) {
console.error("Loading sessionStorage error: " + error) console.error("Loading sessionStorage error: " + error)
} }
@ -32,12 +45,14 @@ export const load = (location: string): ?SendFormState => {
return; return;
} }
export const clear = (location: string) => { export const clear = (): ThunkAction => {
if (typeof window.localStorage === 'undefined') return; return (dispatch: Dispatch, getState: GetState): void => {
if (typeof window.localStorage === 'undefined') return;
try { const location = getState().router.location.pathname;
window.sessionStorage.removeItem(`${PREFIX}${location}`); try {
} catch (error) { window.sessionStorage.removeItem(`${PREFIX}${location}`);
console.error("Clearing sessionStorage error: " + error) } catch (error) {
console.error("Clearing sessionStorage error: " + error)
}
} }
} }

View File

@ -21,6 +21,8 @@ export default class SendContainer extends Component<Props> {
componentWillReceiveProps(newProps: Props) { componentWillReceiveProps(newProps: Props) {
calculate(this.props, newProps); calculate(this.props, newProps);
validation(newProps); validation(newProps);
this.props.saveSessionStorage();
} }
render() { render() {

View File

@ -6,6 +6,7 @@ import { bindActionCreators } from 'redux';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { default as SendFormActions } from '~/js/actions/SendFormActions'; import { default as SendFormActions } from '~/js/actions/SendFormActions';
import * as SessionStorageActions from '~/js/actions/SessionStorageActions';
import SendForm from './SendForm'; import SendForm from './SendForm';
import type { MapStateToProps, MapDispatchToProps } from 'react-redux'; import type { MapStateToProps, MapDispatchToProps } from 'react-redux';
@ -22,7 +23,8 @@ export type StateProps = BaseStateProps & {
} }
export type DispatchProps = BaseDispatchProps & { export type DispatchProps = BaseDispatchProps & {
sendFormActions: typeof SendFormActions sendFormActions: typeof SendFormActions,
saveSessionStorage: typeof SessionStorageActions.save
} }
export type Props = StateProps & BaseStateProps & DispatchProps & BaseDispatchProps; export type Props = StateProps & BaseStateProps & DispatchProps & BaseDispatchProps;
@ -42,6 +44,7 @@ const mapStateToProps: MapStateToProps<State, OwnProps, StateProps> = (state: St
const mapDispatchToProps: MapDispatchToProps<Dispatch, OwnProps, DispatchProps> = (dispatch: Dispatch): DispatchProps => { const mapDispatchToProps: MapDispatchToProps<Dispatch, OwnProps, DispatchProps> = (dispatch: Dispatch): DispatchProps => {
return { return {
sendFormActions: bindActionCreators(SendFormActions, dispatch), sendFormActions: bindActionCreators(SendFormActions, dispatch),
saveSessionStorage: bindActionCreators(SessionStorageActions.save, dispatch),
}; };
} }