-
Notifications
You must be signed in to change notification settings - Fork 295
/
VotingRegistrationStepsEnterPinCode.tsx
200 lines (187 loc) · 6.59 KB
/
VotingRegistrationStepsEnterPinCode.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// @flow
import React, { Component } from 'react';
import { defineMessages, intlShape, FormattedHTMLMessage } from 'react-intl';
import { observer } from 'mobx-react';
import vjf from 'mobx-react-form/lib/validators/VJF';
import PinCode from '../../widgets/forms/PinCode';
import ReactToolboxMobxForm from '../../../utils/ReactToolboxMobxForm';
import {
isValidPinCode,
isValidRepeatPinCode,
} from '../../../utils/validations';
import { FORM_VALIDATION_DEBOUNCE_WAIT } from '../../../config/timingConfig';
import {
VOTING_REGISTRATION_PIN_CODE_LENGTH,
NEXT_VOTING_FUND_NUMBER,
} from '../../../config/votingConfig';
import styles from './VotingRegistrationStepsEnterPinCode.scss';
import VotingRegistrationDialog from './widgets/VotingRegistrationDialog';
const messages = defineMessages({
description: {
id: 'voting.votingRegistration.enterPinCode.step.description',
defaultMessage:
'!!!Please enter a PIN for your Fund{nextVotingFundNumber} voting registration. The PIN you set here, and the QR code which you will get in the next step, will be required for you to vote using the Catalyst Voting app on your smartphone.',
description:
'Description on the voting registration "enter pin code" step.',
},
reminder: {
id: 'voting.votingRegistration.enterPinCode.step.reminder',
defaultMessage:
'!!!<span>It is important to remember your PIN.</span> If you forget your PIN, you will not be able to use this registration for voting, and you will need to repeat the registration process.',
description: 'Reminder on the voting registration "enter pin code" step.',
},
enterPinCodeLabel: {
id: 'voting.votingRegistration.enterPinCode.step.enterPinCodeLabel',
defaultMessage: '!!!Enter PIN',
description:
'Label for pin code input on the voting registration "enter pin code" step.',
},
repeatPinCodeLabel: {
id: 'voting.votingRegistration.enterPinCode.step.repeatPinCodeLabel',
defaultMessage: '!!!Repeat PIN',
description:
'Label for repeat pin code on the voting registration "enter pin code" step.',
},
invalidPinCode: {
id: 'voting.votingRegistration.enterPinCode.step.errors.invalidPinCode',
defaultMessage: '!!!Invalid PIN',
description: 'Error message shown when repeat pin code is invalid.',
},
invalidRepeatPinCode: {
id:
'voting.votingRegistration.enterPinCode.step.errors.invalidRepeatPinCode',
defaultMessage: '!!!PIN doesn’t match',
description: 'Error message shown when repeat pin code is invalid.',
},
continueButtonLabel: {
id: 'voting.votingRegistration.enterPinCode.step.continueButtonLabel',
defaultMessage: '!!!Continue',
description:
'Label for continue button on the voting registration "enter pin code" step.',
},
});
type Props = {
onClose: Function,
stepsList: Array<string>,
activeStep: number,
onSetPinCode: Function,
};
@observer
export default class VotingRegistrationStepsEnterPinCode extends Component<Props> {
static contextTypes = {
intl: intlShape.isRequired,
};
form = new ReactToolboxMobxForm(
{
fields: {
pinCode: {
type: 'password',
value: [],
validators: [
({ field, form }) => {
const value = field.value ? field.value.join('') : '';
const repeatPinCodeField = form.$('repeatPinCode');
const isRepeatPinCodeFieldSet =
value.length === VOTING_REGISTRATION_PIN_CODE_LENGTH;
repeatPinCodeField.validate({
showErrors: isRepeatPinCodeFieldSet,
});
return [
isValidPinCode(value, VOTING_REGISTRATION_PIN_CODE_LENGTH),
this.context.intl.formatMessage(messages.invalidPinCode),
];
},
],
},
repeatPinCode: {
type: 'password',
value: [],
validators: [
({ field, form }) => {
const value = field.value ? field.value.join('') : '';
const pinCode = form.$('pinCode').value
? form.$('pinCode').value.join('')
: '';
return [
isValidRepeatPinCode(pinCode, value),
this.context.intl.formatMessage(messages.invalidRepeatPinCode),
];
},
],
},
},
},
{
plugins: { vjf: vjf() },
options: {
validateOnChange: true,
validationDebounceWait: FORM_VALIDATION_DEBOUNCE_WAIT,
},
}
);
submit = () => {
this.form.submit({
onSuccess: (form) => {
const { pinCode } = form.values();
this.props.onSetPinCode(pinCode.join(''));
},
});
};
render() {
const { form } = this;
const { intl } = this.context;
const { onClose, stepsList, activeStep } = this.props;
const buttonLabel = intl.formatMessage(messages.continueButtonLabel);
const enterPinCodeLabel = intl.formatMessage(messages.enterPinCodeLabel);
const repeatPinCodeLabel = intl.formatMessage(messages.repeatPinCodeLabel);
const pinCodeField = form.$('pinCode');
const repeatPinCodeField = form.$('repeatPinCode');
const pinCodeFieldProps = pinCodeField.bind();
const repeatPinCodeFieldProps = repeatPinCodeField.bind();
const actions = [
{
label: buttonLabel,
onClick: this.submit,
disabled: !form.isValid,
primary: true,
},
];
return (
<VotingRegistrationDialog
onClose={() => {
onClose(true);
}}
stepsList={stepsList}
activeStep={activeStep}
actions={actions}
containerClassName={styles.component}
>
<p className={styles.description}>
<FormattedHTMLMessage
{...messages.description}
values={{ nextVotingFundNumber: NEXT_VOTING_FUND_NUMBER }}
/>
</p>
<div className={styles.pinCode}>
<PinCode
{...pinCodeFieldProps}
label={enterPinCodeLabel}
autoFocus
onChange={(...args) => pinCodeFieldProps.onChange(...args)}
/>
<PinCode
{...repeatPinCodeFieldProps}
label={repeatPinCodeLabel}
onChange={(...args) => repeatPinCodeFieldProps.onChange(...args)}
autoFocus={pinCodeField.isValid && !repeatPinCodeField.isValid}
disabled={!pinCodeField.isValid}
error={repeatPinCodeField.error}
/>
</div>
<p className={styles.reminder}>
<FormattedHTMLMessage {...messages.reminder} />
</p>
</VotingRegistrationDialog>
);
}
}