All files / app/customer EmailLoginForm.tsx

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 199 200 201 202 203 204 205 206 207 208 209 210 211  3x 3x 3x   3x 3x 3x 3x 3x 3x   3x 3x                                 3x 20x 20x 20x 20x 20x 20x 20x 20x   20x     20x 12x 7x 3x     4x     5x 2x     3x     20x               20x 12x 1x     11x 4x       4x 1x     3x                           7x                                                       20x 12x 9x     3x   3x                       20x 14x 1x     13x 4x   2x                             11x 4x     7x               20x                                           3x   12x 12x     1x 1x   4x    
import { SignInEmail } from '@bigcommerce/checkout-sdk';
import { withFormik, FormikProps } from 'formik';
import { noop } from 'lodash';
import React, { memo, useMemo, FunctionComponent } from 'react';
 
import { withLanguage, TranslatedHtml, TranslatedLink, TranslatedString, WithLanguageProps } from '../locale';
import { Alert, AlertType } from '../ui/alert';
import { Button, ButtonVariant } from '../ui/button';
import { Form } from '../ui/form';
import { LoadingSpinner } from '../ui/loading';
import { Modal, ModalHeader } from '../ui/modal';
 
import getEmailValidationSchema from './getEmailValidationSchema';
import EmailField from './EmailField';
 
export interface EmailLoginFormProps {
    email?: string;
    isOpen: boolean;
    isSendingEmail?: boolean;
    emailHasBeenRequested?: boolean;
    sentEmail?: SignInEmail;
    sentEmailError?: any;
    onRequestClose?(): void;
    onSendLoginEmail?(values: EmailLoginFormValues): void;
}
 
export interface EmailLoginFormValues {
    email: string;
}
 
const EmailLoginForm: FunctionComponent<EmailLoginFormProps & WithLanguageProps & FormikProps<EmailLoginFormValues>> = ({
    email,
    isOpen,
    isSendingEmail = false,
    emailHasBeenRequested,
    onRequestClose = noop,
    sentEmailError,
    sentEmail,
    submitForm,
    values: {
        email: formEmail,
    },
}) => {
    const modalHeaderStringId = useMemo(() => {
        if (emailHasBeenRequested) {
            if (sentEmailError) {
                return 'common.error_heading';
            }
 
            return 'login_email.sent_header';
        }
 
        if (email) {
            return 'login_email.header_with_email';
        }
 
        return 'login_email.header';
    }, [emailHasBeenRequested, sentEmailError, email]);
 
    const okButton = useMemo(() => (
        <div className="modal-footer">
            <Button onClick={ onRequestClose }>
                <TranslatedString id="common.ok_action" />
            </Button>
        </div>
    ), [onRequestClose]);
 
    const footer = useMemo(() => {
        if (sentEmailError && sentEmailError.status === 429) {
            return okButton;
        }
 
        if (emailHasBeenRequested && !sentEmailError) {
            Iif (isSendingEmail) {
                return <LoadingSpinner isLoading />;
            }
 
            if (sentEmail && sentEmail.sent_email === 'reset_password') {
                return okButton;
            }
 
            return (
                <p>
                    <TranslatedLink
                        id="login_email.resend_link"
                        onClick={ submitForm }
                    />
                    <TranslatedLink
                        id="login_email.use_password_link"
                        onClick={ onRequestClose }
                    />
                </p>
            );
        }
 
        return (
            <div className="modal-footer">
                <Button
                    className="optimizedCheckout-buttonSecondary"
                    onClick={ onRequestClose }
                    type="button"
                >
                    <TranslatedString id="common.go_back" />
                </Button>
                <Button
                    isLoading={ isSendingEmail }
                    type="submit"
                    variant={ ButtonVariant.Primary }
                >
                    <TranslatedString id="login_email.send" />
                </Button>
            </div>
        );
    }, [
        sentEmailError,
        emailHasBeenRequested,
        okButton,
        submitForm,
        isSendingEmail,
        onRequestClose,
        sentEmail,
    ]);
 
    const error = useMemo(() => {
        if (!sentEmailError) {
            return null;
        }
 
        const { status } = sentEmailError;
 
        return (
            <Alert type={ AlertType.Error }>
                { status === 429 ?
                    <TranslatedString id="login_email.error_temporary_disabled" /> :
                    <TranslatedString id={ status === 404 ?
                        'login_email.error_not_found' :
                        'login_email.error_server' }
                    /> }
            </Alert>
        );
    }, [sentEmailError]);
 
    const form = useMemo(() => {
        if (sentEmailError && sentEmailError.status === 429) {
            return null;
        }
 
        if (emailHasBeenRequested && sentEmail && !sentEmailError) {
            const { expiry, sent_email } = sentEmail;
 
            return (
                <p>
                    <TranslatedHtml
                        data={ {
                            email: formEmail,
                            minutes: Math.round(expiry / 60),
                        } }
                        id={ sent_email === 'sign_in' ?
                            'login_email.sent_text' :
                            'customer.reset_password_before_login_error' }
                    />
                </p>
            );
        }
 
        if (emailHasBeenRequested && !sentEmail) {
            return <EmailField />;
        }
 
        return (<>
            <p>
                <TranslatedString id="login_email.text" />
            </p>
            <EmailField />
        </>);
    }, [sentEmailError, emailHasBeenRequested, sentEmail, formEmail]);
 
    return (
        <Modal
            additionalBodyClassName="modal--withText"
            additionalModalClassName="modal--medium"
            header={
                <ModalHeader>
                    <TranslatedString id={ modalHeaderStringId } />
                </ModalHeader>
            }
            isOpen={ isOpen }
            onRequestClose={ onRequestClose }
            shouldShowCloseButton={ true }
        >
            <Form>
                <LoadingSpinner isLoading={ isSendingEmail && !email } />
                { error }
                { form }
                { footer }
            </Form>
        </Modal>);
};
 
export default withLanguage(withFormik<EmailLoginFormProps & WithLanguageProps, EmailLoginFormValues>({
    mapPropsToValues: ({
        email = '',
    }) => ({
        email,
    }),
    handleSubmit: (values, { props: { onSendLoginEmail = noop } }) => {
        onSendLoginEmail(values);
    },
    validationSchema: ({ language }: WithLanguageProps) => getEmailValidationSchema({ language }),
})(memo(EmailLoginForm)));