Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 2x 2x 87x 2x 8x 37x 37x 1x 1x 22x 22x | import { withFormik, FormikProps } from 'formik';
import { noop } from 'lodash';
import React, { memo, useCallback, FunctionComponent } from 'react';
import { object, string } from 'yup';
import { preventDefault } from '../common/dom';
import { withLanguage, TranslatedHtml, TranslatedLink, TranslatedString, WithLanguageProps } from '../locale';
import { Alert, AlertType } from '../ui/alert';
import { Button, ButtonVariant } from '../ui/button';
import { Fieldset, Form, Legend } from '../ui/form';
import getEmailValidationSchema from './getEmailValidationSchema';
import mapErrorMessage from './mapErrorMessage';
import CustomerViewType from './CustomerViewType';
import EmailField from './EmailField';
import PasswordField from './PasswordField';
export interface LoginFormProps {
canCancel?: boolean;
email?: string;
forgotPasswordUrl: string;
isSignInEmailEnabled?: boolean;
isSendingSignInEmail?: boolean;
isSigningIn?: boolean;
signInError?: Error;
signInEmailError?: Error;
viewType?: Omit<CustomerViewType, 'guest'>;
passwordlessLogin?: boolean;
shouldShowCreateAccountLink?: boolean;
onCancel?(): void;
onCreateAccount?(): void;
onChangeEmail?(email: string): void;
onSignIn(data: LoginFormValues): void;
onSendLoginEmail?(): void;
onContinueAsGuest?(): void;
}
export interface LoginFormValues {
email: string;
password: string;
}
const LoginForm: FunctionComponent<LoginFormProps & WithLanguageProps & FormikProps<LoginFormValues>> = ({
canCancel,
forgotPasswordUrl,
email,
isSignInEmailEnabled,
isSigningIn,
language,
onCancel = noop,
onChangeEmail,
onContinueAsGuest,
onCreateAccount = noop,
onSendLoginEmail = noop,
signInError,
shouldShowCreateAccountLink,
viewType = CustomerViewType.Login,
}) => {
const changeEmailLink = useCallback(() => {
Iif (!email) {
return null;
}
return (
<p className="optimizedCheckout-contentSecondary">
<TranslatedLink
data={ { email } }
id="customer.guest_could_login_change_email"
onClick={ onCancel }
testId="change-email"
/>
</p>
);
}, [email, onCancel]);
return (
<Form
className="checkout-form"
id="checkout-customer-returning"
testId="checkout-customer-returning"
>
<Fieldset legend={
<Legend hidden>
<TranslatedString id="customer.returning_customer_text" />
</Legend>
}
>
{ signInError && <Alert
testId="customer-login-error-message"
type={ AlertType.Error }
>
{ mapErrorMessage(signInError, key => language.translate(key)) }
</Alert> }
{ viewType === CustomerViewType.SuggestedLogin &&
<Alert type={ AlertType.Info }>
<TranslatedHtml
data={ { email } }
id="customer.guest_could_login"
/>
</Alert> }
{ viewType === CustomerViewType.Login && shouldShowCreateAccountLink && <p>
<TranslatedLink
id="customer.create_account_to_continue_text"
onClick={ onCreateAccount }
/>
</p> }
{ viewType === CustomerViewType.CancellableEnforcedLogin &&
<Alert type={ AlertType.Info }>
<TranslatedHtml
data={ { email } }
id="customer.guest_must_login"
/>
</Alert> }
{ viewType === CustomerViewType.EnforcedLogin &&
<Alert type={ AlertType.Error }>
<TranslatedLink
id="customer.guest_temporary_disabled"
onClick={ onCreateAccount }
/>
</Alert> }
{ (viewType === CustomerViewType.Login || viewType === CustomerViewType.EnforcedLogin) &&
<EmailField onChange={ onChangeEmail } /> }
<PasswordField forgotPasswordUrl={ isSignInEmailEnabled ? undefined : forgotPasswordUrl } />
{ isSignInEmailEnabled && <p>
<TranslatedLink
id="login_email.link"
onClick={ onSendLoginEmail }
testId="customer-signin-link"
/>
</p> }
<div className="form-actions">
<Button
disabled={ isSigningIn }
id="checkout-customer-continue"
testId="customer-continue-button"
type="submit"
variant={ ButtonVariant.Primary }
>
<TranslatedString id="customer.sign_in_action" />
</Button>
{ viewType === CustomerViewType.SuggestedLogin && <a
className="button optimizedCheckout-buttonSecondary"
data-test="customer-guest-continue"
href="#"
id="checkout-guest-continue"
onClick={ preventDefault(onContinueAsGuest) }
>
<TranslatedString id="customer.continue_as_guest_action" />
</a> }
{ canCancel &&
viewType !== CustomerViewType.EnforcedLogin &&
viewType !== CustomerViewType.SuggestedLogin &&
<a
className="button optimizedCheckout-buttonSecondary"
data-test="customer-cancel-button"
href="#"
id="checkout-customer-cancel"
onClick={ preventDefault(onCancel) }
>
<TranslatedString id={ viewType === CustomerViewType.CancellableEnforcedLogin ?
'login_email.use_another_email' :
'common.cancel_action' }
/>
</a> }
</div>
{ viewType === CustomerViewType.SuggestedLogin && changeEmailLink() }
</Fieldset>
</Form>);
};
export default withLanguage(withFormik<LoginFormProps & WithLanguageProps, LoginFormValues>({
mapPropsToValues: ({
email = '',
}) => ({
email,
password: '',
}),
handleSubmit: (values, { props: { onSignIn } }) => {
onSignIn(values);
},
validationSchema: ({ language }: LoginFormProps & WithLanguageProps) =>
getEmailValidationSchema({ language }).concat(object({
password: string()
.required(language.translate('customer.password_required_error')),
})),
})(memo(LoginForm)));
|