1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-12-30 19:00:53 +00:00

Move values to redux

This commit is contained in:
Vladimir Volek 2018-11-21 19:19:14 +01:00
parent 93000adf9e
commit e41cc22e3b
5 changed files with 79 additions and 61 deletions

View File

@ -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 => { const clear = (): ThunkAction => (dispatch: Dispatch): void => {
dispatch({ dispatch({
type: SIGN_VERIFY.CLEAR, type: SIGN_VERIFY.CLEAR,
@ -121,4 +133,5 @@ export default {
sign, sign,
verify, verify,
clear, clear,
inputChange,
}; };

View File

@ -1,3 +1,5 @@
/* @flow */ /* @flow */
export const SIGN_SUCCESS: 'sign__verify__sign__success' = 'sign__verify__sign__success'; 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'; export const CLEAR: 'sign__verify__sign__clear' = 'sign__verify__sign__clear';

View File

@ -1,23 +1,50 @@
/* @flow */ /* @flow */
import type { Action } from 'flowtype'; import type { Action } from 'flowtype';
import * as SIGN_VERIFY from '../actions/constants/signVerify'; import * as SIGN_VERIFY from 'actions/constants/signVerify';
export type State = { export type State = {
signature: string signAddress: string,
signMessage: string,
signSignature: string,
verifyAddress: string,
verifyMessage: string,
verifySignature: string,
touched: Array<String>
} }
export const initialState: State = { export const initialState: State = {
signature: '', signAddress: '',
signMessage: '',
signSignature: '',
verifyAddress: '',
verifyMessage: '',
verifySignature: '',
touched: [],
}; };
export default (state: State = initialState, action: Action): State => { export default (state: State = initialState, action: Action): State => {
switch (action.type) { switch (action.type) {
case SIGN_VERIFY.SIGN_SUCCESS: case SIGN_VERIFY.SIGN_SUCCESS:
return { return {
...state, ...initialState,
signature: action.signature, 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: case SIGN_VERIFY.CLEAR:
return { return {
...initialState, ...initialState,

View File

@ -25,7 +25,7 @@ export type Props = StateProps & DispatchProps;
const mapStateToProps: MapStateToProps<State, OwnProps, StateProps> = (state: State): StateProps => ({ const mapStateToProps: MapStateToProps<State, OwnProps, StateProps> = (state: State): StateProps => ({
wallet: state.wallet, wallet: state.wallet,
selectedAccount: state.selectedAccount, selectedAccount: state.selectedAccount,
signature: state.signVerifyReducer.signature, signVerify: state.signVerifyReducer,
}); });
const mapDispatchToProps: MapDispatchToProps<Dispatch, OwnProps, DispatchProps> = (dispatch: Dispatch): DispatchProps => ({ const mapDispatchToProps: MapDispatchToProps<Dispatch, OwnProps, DispatchProps> = (dispatch: Dispatch): DispatchProps => ({

View File

@ -55,61 +55,37 @@ type State = {
} }
class SignVerify extends Component<Props, State> { class SignVerify extends Component<Props, State> {
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<Text>) => { handleInputChange = (event: SyntheticInputEvent<Text>) => {
this.setTouchedInput(event.target.name); const touched = true;
this.setState({ [event.target.name]: event.target.value }); this.props.signVerifyActions.inputChange(
} event.target.name,
event.target.value,
clearSign =() => { touched,
this.setState({ );
signMessage: '',
});
this.props.signVerifyActions.clear();
}
clearVerify = () => {
this.setState({
verifyAddress: '',
verifyMessage: '',
verifySignature: '',
});
} }
render() { render() {
const device = this.props.wallet.selectedDevice; const device = this.props.wallet.selectedDevice;
const { const {
account, account, discovery, shouldRender, notification,
discovery,
shouldRender,
notification,
} = this.props.selectedAccount; } = this.props.selectedAccount;
const { type, title, message } = notification; const { type, title, message } = notification;
if (!device || !account || !discovery || !shouldRender) return <Content type={type} title={title} message={message} isLoading />; if (!device || !account || !discovery || !shouldRender) return <Content type={type} title={title} message={message} isLoading />;
const { const {
signVerifyActions, signVerifyActions,
signature, signVerify,
} = this.props; } = this.props;
const {
signAddress,
signMessage,
signSignature,
verifyAddress,
verifyMessage,
verifySignature,
touched,
} = signVerify;
console.log(touched);
return ( return (
<Content> <Content>
<Title>Sign & Verify</Title> <Title>Sign & Verify</Title>
@ -130,7 +106,7 @@ class SignVerify extends Component<Props, State> {
<Textarea <Textarea
topLabel="Message" topLabel="Message"
name="signMessage" name="signMessage"
value={this.state.signMessage} value={signMessage}
onChange={this.handleInputChange} onChange={this.handleInputChange}
rows={4} rows={4}
maxRows={4} maxRows={4}
@ -141,7 +117,7 @@ class SignVerify extends Component<Props, State> {
<Textarea <Textarea
topLabel="Signature" topLabel="Signature"
name="signSignature" name="signSignature"
value={signature} value={signSignature}
rows={4} rows={4}
maxRows={4} maxRows={4}
maxLength="255" maxLength="255"
@ -150,12 +126,12 @@ class SignVerify extends Component<Props, State> {
</Row> </Row>
<RowButtons> <RowButtons>
<Button <Button
onClick={this.clearSign} onClick={this.props.signVerifyActions.clearSign}
isWhite isWhite
>Clear >Clear
</Button> </Button>
<StyledButton <StyledButton
onClick={() => signVerifyActions.sign(account.addressPath, this.state.signMessage)} onClick={() => signVerifyActions.sign(signAddress, signMessage)}
>Sign >Sign
</StyledButton> </StyledButton>
</RowButtons> </RowButtons>
@ -165,11 +141,11 @@ class SignVerify extends Component<Props, State> {
<Input <Input
topLabel="Address" topLabel="Address"
name="verifyAddress" name="verifyAddress"
value={this.state.verifyAddress} value={verifyAddress}
onChange={this.handleInputChange} onChange={this.handleInputChange}
type="text" type="text"
state={(this.shouldValidate('verifyAddress') && validateAddress(this.state.verifyAddress)) ? 'error' : null} state={(touched.includes('verifyAddress') && validateAddress(verifyAddress)) ? 'error' : null}
bottomText={this.shouldValidate('verifyAddress') ? validateAddress(this.state.verifyAddress) : null} bottomText={touched.includes('verifyAddress') ? validateAddress(verifyAddress) : null}
isSmallText isSmallText
/> />
</Row> </Row>
@ -177,7 +153,7 @@ class SignVerify extends Component<Props, State> {
<Textarea <Textarea
topLabel="Message" topLabel="Message"
name="verifyMessage" name="verifyMessage"
value={this.state.verifyMessage} value={verifyMessage}
onChange={this.handleInputChange} onChange={this.handleInputChange}
rows={4} rows={4}
maxRows={4} maxRows={4}
@ -188,7 +164,7 @@ class SignVerify extends Component<Props, State> {
<Textarea <Textarea
topLabel="Signature" topLabel="Signature"
name="verifySignature" name="verifySignature"
value={this.state.verifySignature} value={verifySignature}
onChange={this.handleInputChange} onChange={this.handleInputChange}
rows={4} rows={4}
maxRows={4} maxRows={4}
@ -197,15 +173,15 @@ class SignVerify extends Component<Props, State> {
</Row> </Row>
<RowButtons> <RowButtons>
<Button <Button
onClick={this.clearVerify} onClick={signVerifyActions.clearSign}
isWhite isWhite
>Clear >Clear
</Button> </Button>
<StyledButton <StyledButton
onClick={() => signVerifyActions.verify( onClick={() => signVerifyActions.verify(
this.state.verifyAddress, verifyAddress,
this.state.verifyMessage, verifyMessage,
this.state.verifySignature, verifySignature,
)} )}
>Verify >Verify
</StyledButton> </StyledButton>