forked from adobe/aem-core-cif-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateAccount.js
148 lines (140 loc) · 5.54 KB
/
createAccount.js
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
/*******************************************************************************
*
* Copyright 2019 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*
******************************************************************************/
import React from 'react';
import { Form } from 'informed';
import { shape, string, func } from 'prop-types';
import { useTranslation } from 'react-i18next';
import Field from '../Field';
import TextInput from '../TextInput';
import Checkbox from '../Checkbox';
import Button from '../Button';
import combine from '../../utils/combineValidators';
import {
validateEmail,
isRequired,
validatePassword,
validateConfirmPassword,
hasLengthAtLeast
} from '../../utils/formValidators';
import mergeClasses from '../../utils/mergeClasses';
import defaultClasses from './createAccount.css';
import useCreateAccount from './useCreateAccount';
import LoadingIndicator from '../LoadingIndicator';
const CreateAccount = props => {
const { showAccountCreated, handleCancel } = props;
const [{ createAccountError, inProgress }, { createAccount }] = useCreateAccount({
showAccountCreated
});
const [t] = useTranslation('account');
if (inProgress) {
return <LoadingIndicator message="Creating account" />;
}
const handleCreateAccount = formValues => {
createAccount(formValues);
};
const errorMessage = createAccountError ? createAccountError : null;
const classes = mergeClasses(defaultClasses, props.classes);
return (
<Form className={classes.root} onSubmit={handleCreateAccount}>
<p className={classes.lead}>
{t(
'account:create-account-lead',
'Check out faster, use multiple addresses, track orders and more by creating an account!'
)}
</p>
<Field label={t('account:firstname', 'First Name')} required={true}>
<TextInput
field="customer.firstname"
autoComplete="given-name"
validate={isRequired}
validateOnBlur
aria-label="firstname"
/>
</Field>
<Field label={t('account:lastname', 'Last Name')} required={true}>
<TextInput
field="customer.lastname"
autoComplete="family-name"
validate={isRequired}
validateOnBlur
aria-label="lastname"
/>
</Field>
<Field label={t('account:email', 'Email address')} required={true}>
<TextInput
field="customer.email"
autoComplete="email"
validate={combine([isRequired, validateEmail])}
validateOnBlur
aria-label="email"
/>
</Field>
<Field label={t('account:password', 'Password')} required={true}>
<TextInput
field="password"
type="password"
autoComplete="new-password"
validate={combine([isRequired, [hasLengthAtLeast, 8], validatePassword])}
validateOnBlur
aria-label="password"
/>
</Field>
<Field label={t('account:confirm-password', 'Confirm Password')} required={true}>
<TextInput
field="confirm"
type="password"
validate={combine([isRequired, validateConfirmPassword])}
validateOnBlur
aria-label="confirm"
/>
</Field>
<div className={classes.subscribe}>
<Checkbox
field="subscribe"
label={t('account:subscribe-news', 'Subscribe to news and updates')}
aria-label="subscribe"
/>
</div>
<div className={classes.error}>{errorMessage}</div>
<div className={classes.actions}>
<Button disabled={inProgress} type="submit" priority="high" aria-label="submit">
{t('account:create-submit', 'Submit')}
</Button>
{handleCancel && (
<Button
disabled={inProgress}
type="button"
priority="normal"
aria-label="cancel"
onClick={handleCancel}>
{t('account:create-cancel', 'Cancel')}
</Button>
)}
</div>
</Form>
);
};
CreateAccount.propTypes = {
classes: shape({
actions: string,
error: string,
lead: string,
root: string,
subscribe: string
}),
showMyAccount: func.isRequired,
showAccountCreated: func.isRequired,
handleCancel: func
};
export default CreateAccount;