step to select verification method

This commit is contained in:
Jannis R 2016-12-06 17:16:54 +01:00
parent 1672cbad7b
commit 1ac3421f33
No known key found for this signature in database
GPG Key ID: 0FE83946296A88A5
2 changed files with 105 additions and 33 deletions

View File

@ -20,6 +20,13 @@ import DoneIcon from 'material-ui/svg-icons/action/done-all';
import CancelIcon from 'material-ui/svg-icons/content/clear'; import CancelIcon from 'material-ui/svg-icons/content/clear';
import { Button, IdentityIcon, Modal } from '~/ui'; import { Button, IdentityIcon, Modal } from '~/ui';
import RadioButtons from '~/ui/Form/RadioButtons';
import { nullableProptype } from '~/util/proptypes';
const methods = {
sms: { label: 'SMS Verification', key: 0, value: 'sms' },
email: { label: 'E-mail Verification', key: 1, value: 'email' }
};
import { import {
LOADING, LOADING,
@ -39,23 +46,34 @@ import Done from './Done';
@observer @observer
export default class Verification extends Component { export default class Verification extends Component {
static propTypes = { static propTypes = {
store: PropTypes.any.isRequired, store: nullableProptype(PropTypes.object).isRequired,
account: PropTypes.string.isRequired, account: PropTypes.string.isRequired,
onSelectMethod: PropTypes.func.isRequired,
onClose: PropTypes.func.isRequired onClose: PropTypes.func.isRequired
} }
static phases = { // mapping (store steps -> steps) static phases = { // mapping (store steps -> steps)
[LOADING]: 0, [LOADING]: 1,
[QUERY_DATA]: 1, [QUERY_DATA]: 2,
[POSTING_REQUEST]: 2, [POSTED_REQUEST]: 2, [REQUESTING_CODE]: 2, [POSTING_REQUEST]: 3, [POSTED_REQUEST]: 3, [REQUESTING_CODE]: 3,
[QUERY_CODE]: 3, [QUERY_CODE]: 4,
[POSTING_CONFIRMATION]: 4, [POSTED_CONFIRMATION]: 4, [POSTING_CONFIRMATION]: 5, [POSTED_CONFIRMATION]: 5,
[DONE]: 5 [DONE]: 6
} }
state = {
method: 'sms'
};
render () { render () {
const phase = Verification.phases[this.props.store.step]; const { store } = this.props;
const { error, isStepValid } = this.props.store; let phase = 0; let error = false; let isStepValid = true;
if (store) {
phase = Verification.phases[store.step];
error = store.error;
isStepValid = store.isStepValid;
}
return ( return (
<Modal <Modal
@ -63,8 +81,8 @@ export default class Verification extends Component {
title='verify your account' title='verify your account'
visible visible
current={ phase } current={ phase }
steps={ ['Prepare', 'Enter Data', 'Request', 'Enter Code', 'Confirm', 'Done!'] } steps={ ['Method', 'Prepare', 'Enter Data', 'Request', 'Enter Code', 'Confirm', 'Done!'] }
waiting={ error ? [] : [ 0, 2, 4 ] } waiting={ error ? [] : [ 1, 3, 5 ] }
> >
{ this.renderStep(phase, error) } { this.renderStep(phase, error) }
</Modal> </Modal>
@ -85,7 +103,7 @@ export default class Verification extends Component {
return (<div>{ cancel }</div>); return (<div>{ cancel }</div>);
} }
if (phase === 5) { if (phase === 6) {
return ( return (
<div> <div>
{ cancel } { cancel }
@ -101,16 +119,23 @@ export default class Verification extends Component {
let action = () => {}; let action = () => {};
switch (phase) { switch (phase) {
case 1: case 0:
action = store.sendRequest; action = () => {
const { onSelectMethod } = this.props;
const { method } = this.state;
onSelectMethod(method);
};
break; break;
case 2: case 2:
action = store.queryCode; action = store.sendRequest;
break; break;
case 3: case 3:
action = store.sendConfirmation; action = store.queryCode;
break; break;
case 4: case 4:
action = store.sendConfirmation;
break;
case 5:
action = store.done; action = store.done;
break; break;
} }
@ -133,6 +158,19 @@ export default class Verification extends Component {
return (<p>{ error }</p>); return (<p>{ error }</p>);
} }
if (phase === 0) {
const { method } = this.state;
const values = Object.values(methods);
const value = values.findIndex((v) => v.value === method);
return (
<RadioButtons
value={ value < 0 ? 0 : value }
values={ values }
onChange={ this.selectMethod }
/>
);
}
const { const {
step, step,
fee, number, isNumberValid, isVerified, hasRequested, fee, number, isNumberValid, isVerified, hasRequested,
@ -141,13 +179,34 @@ export default class Verification extends Component {
} = this.props.store; } = this.props.store;
switch (phase) { switch (phase) {
case 0: case 1:
return ( return (
<p>Loading Verification.</p> <p>Loading Verification.</p>
); );
case 1: case 2:
const { setNumber, setConsentGiven } = this.props.store; const { method } = this.state;
const { setConsentGiven } = this.props.store;
const fields = []
if (method === 'sms') {
fields.push({
key: 'number',
label: 'phone number in international format',
hint: 'the SMS will be sent to this number',
error: this.props.store.isNumberValid ? null : 'invalid number',
onChange: this.props.store.setNumber
});
} else if (method === 'email') {
fields.push({
key: 'email',
label: 'email address',
hint: 'the code will be sent to this address',
error: this.props.store.isEmailValid ? null : 'invalid email',
onChange: this.props.store.setEmail
});
}
return ( return (
<GatherData <GatherData
fee={ fee } isNumberValid={ isNumberValid } fee={ fee } isNumberValid={ isNumberValid }
@ -156,12 +215,12 @@ export default class Verification extends Component {
/> />
); );
case 2: case 3:
return ( return (
<SendRequest step={ step } tx={ requestTx } /> <SendRequest step={ step } tx={ requestTx } />
); );
case 3: case 4:
return ( return (
<QueryCode <QueryCode
number={ number } fee={ fee } isCodeValid={ isCodeValid } number={ number } fee={ fee } isCodeValid={ isCodeValid }
@ -169,12 +228,12 @@ export default class Verification extends Component {
/> />
); );
case 4: case 5:
return ( return (
<SendConfirmation step={ step } tx={ confirmationTx } /> <SendConfirmation step={ step } tx={ confirmationTx } />
); );
case 5: case 6:
return ( return (
<Done /> <Done />
); );
@ -183,4 +242,8 @@ export default class Verification extends Component {
return null; return null;
} }
} }
selectMethod = (choice, i) => {
this.setState({ method: choice.value });
}
} }

View File

@ -32,7 +32,8 @@ import Header from './Header';
import Transactions from './Transactions'; import Transactions from './Transactions';
import { setVisibleAccounts } from '~/redux/providers/personalActions'; import { setVisibleAccounts } from '~/redux/providers/personalActions';
import VerificationStore from '~/modals/Verification/sms-store'; import SMSVerificationStore from '~/modals/Verification/sms-store';
import EmailVerificationStore from '~/modals/Verification/email-store';
import styles from './account.css'; import styles from './account.css';
@ -72,15 +73,6 @@ class Account extends Component {
if (prevAddress !== nextAddress) { if (prevAddress !== nextAddress) {
this.setVisibleAccounts(nextProps); this.setVisibleAccounts(nextProps);
} }
const { isTestnet } = nextProps;
if (typeof isTestnet === 'boolean' && !this.state.verificationStore) {
const { api } = this.context;
const { address } = nextProps.params;
this.setState({
verificationStore: new VerificationStore(api, address, isTestnet)
});
}
} }
componentWillUnmount () { componentWillUnmount () {
@ -230,6 +222,7 @@ class Account extends Component {
return ( return (
<Verification <Verification
store={ store } account={ address } store={ store } account={ address }
onSelectMethod={ this.selectVerificationMethod }
onClose={ this.onVerificationClose } onClose={ this.onVerificationClose }
/> />
); );
@ -303,6 +296,22 @@ class Account extends Component {
this.setState({ showVerificationDialog: true }); this.setState({ showVerificationDialog: true });
} }
selectVerificationMethod = (name) => {
const { isTestnet } = this.props;
if (typeof isTestnet !== 'boolean' || this.state.verificationStore) return;
const { api } = this.context;
const { address } = this.props.params;
let verificationStore = null;
if (name === 'sms') {
verificationStore = new SMSVerificationStore(api, address, isTestnet);
} else if (name === 'email') {
verificationStore = new EmailVerificationStore(api, address, isTestnet);
}
this.setState({ verificationStore });
}
onVerificationClose = () => { onVerificationClose = () => {
this.setState({ showVerificationDialog: false }); this.setState({ showVerificationDialog: false });
} }