From e41cc22e3b607bd9e01a35fcaacd55048b5ca8b9 Mon Sep 17 00:00:00 2001 From: Vladimir Volek Date: Wed, 21 Nov 2018 19:19:14 +0100 Subject: [PATCH] Move values to redux --- src/actions/SignVerifyActions.js | 13 +++ src/actions/constants/signVerify.js | 2 + src/reducers/SignVerifyReducer.js | 35 +++++++- .../views/Account/SignVerify/Container.js | 2 +- .../Wallet/views/Account/SignVerify/index.js | 88 +++++++------------ 5 files changed, 79 insertions(+), 61 deletions(-) diff --git a/src/actions/SignVerifyActions.js b/src/actions/SignVerifyActions.js index 38feeb53..fbe561ec 100644 --- a/src/actions/SignVerifyActions.js +++ b/src/actions/SignVerifyActions.js @@ -111,6 +111,18 @@ const verify = ( } }; +const inputChange = (name, value): ThunkAction => (dispatch: Dispatch): void => { + dispatch({ + type: SIGN_VERIFY.INPUT_CHANGE, + name, + value, + }); + dispatch({ + type: SIGN_VERIFY.TOUCH, + name, + }); +}; + const clear = (): ThunkAction => (dispatch: Dispatch): void => { dispatch({ type: SIGN_VERIFY.CLEAR, @@ -121,4 +133,5 @@ export default { sign, verify, clear, + inputChange, }; \ No newline at end of file diff --git a/src/actions/constants/signVerify.js b/src/actions/constants/signVerify.js index 96b5d7ea..fdf622c7 100644 --- a/src/actions/constants/signVerify.js +++ b/src/actions/constants/signVerify.js @@ -1,3 +1,5 @@ /* @flow */ export const SIGN_SUCCESS: 'sign__verify__sign__success' = 'sign__verify__sign__success'; +export const INPUT_CHANGE: 'sign__verify__input__change' = 'sign__verify__input__change'; +export const TOUCH: 'sign__verify__input__touch' = 'sign__verify__input__touch'; export const CLEAR: 'sign__verify__sign__clear' = 'sign__verify__sign__clear'; \ No newline at end of file diff --git a/src/reducers/SignVerifyReducer.js b/src/reducers/SignVerifyReducer.js index 4ad04eef..cdbe1ee8 100644 --- a/src/reducers/SignVerifyReducer.js +++ b/src/reducers/SignVerifyReducer.js @@ -1,23 +1,50 @@ /* @flow */ import type { Action } from 'flowtype'; -import * as SIGN_VERIFY from '../actions/constants/signVerify'; +import * as SIGN_VERIFY from 'actions/constants/signVerify'; export type State = { - signature: string + signAddress: string, + signMessage: string, + signSignature: string, + verifyAddress: string, + verifyMessage: string, + verifySignature: string, + touched: Array } export const initialState: State = { - signature: '', + signAddress: '', + signMessage: '', + signSignature: '', + verifyAddress: '', + verifyMessage: '', + verifySignature: '', + touched: [], }; export default (state: State = initialState, action: Action): State => { switch (action.type) { case SIGN_VERIFY.SIGN_SUCCESS: return { - ...state, + ...initialState, signature: action.signature, }; + case SIGN_VERIFY.TOUCH: { + if (!state.touched.includes(action.name)) { + return { + ...state, + touched: [...state.touched, action.name], + }; + } + return state; + } + + case SIGN_VERIFY.INPUT_CHANGE: { + const changes = { [action.name]: action.value }; + return { ...state, ...changes }; + } + case SIGN_VERIFY.CLEAR: return { ...initialState, diff --git a/src/views/Wallet/views/Account/SignVerify/Container.js b/src/views/Wallet/views/Account/SignVerify/Container.js index 29281581..e4692801 100644 --- a/src/views/Wallet/views/Account/SignVerify/Container.js +++ b/src/views/Wallet/views/Account/SignVerify/Container.js @@ -25,7 +25,7 @@ export type Props = StateProps & DispatchProps; const mapStateToProps: MapStateToProps = (state: State): StateProps => ({ wallet: state.wallet, selectedAccount: state.selectedAccount, - signature: state.signVerifyReducer.signature, + signVerify: state.signVerifyReducer, }); const mapDispatchToProps: MapDispatchToProps = (dispatch: Dispatch): DispatchProps => ({ diff --git a/src/views/Wallet/views/Account/SignVerify/index.js b/src/views/Wallet/views/Account/SignVerify/index.js index 66dbc24e..2913fc9c 100644 --- a/src/views/Wallet/views/Account/SignVerify/index.js +++ b/src/views/Wallet/views/Account/SignVerify/index.js @@ -55,61 +55,37 @@ type State = { } class SignVerify extends Component { - constructor(props: Props) { - super(props); - this.state = { - signMessage: '', - verifyAddress: '', - verifyMessage: '', - verifySignature: '', - touched: [], - }; - } - - setTouchedInput = (inputName: string) => { - if (!this.state.touched.includes(inputName)) { - this.setState(prevState => ({ - touched: [...prevState.touched, inputName], - })); - } - } - - shouldValidate = (inputName: string) => this.state.touched.includes(inputName) - handleInputChange = (event: SyntheticInputEvent) => { - this.setTouchedInput(event.target.name); - this.setState({ [event.target.name]: event.target.value }); - } - - clearSign =() => { - this.setState({ - signMessage: '', - }); - this.props.signVerifyActions.clear(); - } - - clearVerify = () => { - this.setState({ - verifyAddress: '', - verifyMessage: '', - verifySignature: '', - }); + const touched = true; + this.props.signVerifyActions.inputChange( + event.target.name, + event.target.value, + touched, + ); } render() { const device = this.props.wallet.selectedDevice; const { - account, - discovery, - shouldRender, - notification, + account, discovery, shouldRender, notification, } = this.props.selectedAccount; const { type, title, message } = notification; if (!device || !account || !discovery || !shouldRender) return ; const { signVerifyActions, - signature, + signVerify, } = this.props; + + const { + signAddress, + signMessage, + signSignature, + verifyAddress, + verifyMessage, + verifySignature, + touched, + } = signVerify; + console.log(touched); return ( Sign & Verify @@ -130,7 +106,7 @@ class SignVerify extends Component {