All files / app/address AddressFormModal.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  23x 23x 23x   23x 23x 23x 23x 23x 23x   23x   23x                                     23x 15x 15x 15x 15x 15x 15x 15x 15x 15x                                                                   23x 1x 1x   5x                                 6x 6x 6x 6x             23x 27x 27x 27x 27x 27x                                       23x  
import { Country, FormField } from '@bigcommerce/checkout-sdk';
import { withFormik, FormikProps } from 'formik';
import React, { FunctionComponent } from 'react';
import { lazy } from 'yup';
 
import { preventDefault } from '../common/dom';
import { withLanguage, TranslatedString, WithLanguageProps } from '../locale';
import { Button, ButtonVariant } from '../ui/button';
import { Form } from '../ui/form';
import { LoadingOverlay } from '../ui/loading';
import { Modal, ModalHeader } from '../ui/modal';
 
import getAddressFormFieldsValidationSchema from './getAddressFormFieldsValidationSchema';
import { AddressFormValues } from './mapAddressToFormValues';
import AddressForm from './AddressForm';
 
export interface AddressFormModalProps extends AddressFormProps {
    isOpen: boolean;
    onAfterOpen?(): void;
}
 
export interface AddressFormProps {
    countries?: Country[];
    countriesWithAutocomplete: string[];
    googleMapsApiKey?: string;
    isLoading: boolean;
    shouldShowSaveAddress?: boolean;
    defaultCountryCode?: string;
    getFields(countryCode?: string): FormField[];
    onSaveAddress(address: AddressFormValues): void;
    onRequestClose?(): void;
}
 
const SaveAddress: FunctionComponent<AddressFormProps & WithLanguageProps & FormikProps<AddressFormValues>> = ({
    googleMapsApiKey,
    getFields,
    countriesWithAutocomplete,
    countries,
    values,
    setFieldValue,
    isLoading,
    onRequestClose,
}) => (
    <Form autoComplete="on">
        <LoadingOverlay isLoading={ isLoading }>
            <AddressForm
                countries={ countries }
                countriesWithAutocomplete={ countriesWithAutocomplete }
                countryCode={ values.countryCode }
                formFields={ getFields(values.countryCode) }
                googleMapsApiKey={ googleMapsApiKey }
                setFieldValue={ setFieldValue }
                shouldShowSaveAddress={ false }
            />
            <div className="form-actions">
                <a
                    className="button optimizedCheckout-buttonSecondary"
                    href="#"
                    onClick={ preventDefault(onRequestClose) }
                >
                    <TranslatedString id="common.cancel_action" />
                </a>
 
                <Button
                    disabled={ isLoading }
                    id="checkout-save-address"
                    type="submit"
                    variant={ ButtonVariant.Primary }
                >
                    <TranslatedString id="address.save_address_action" />
                </Button>
            </div>
        </LoadingOverlay>
    </Form>
);
 
const SaveAddressForm = withLanguage(withFormik<AddressFormProps & WithLanguageProps, AddressFormValues>({
    handleSubmit: (values, { props: { onSaveAddress } }) => {
        onSaveAddress(values);
    },
    mapPropsToValues: ({ defaultCountryCode = '' }) => ({
        firstName: '',
        lastName: '',
        address1: '',
        address2: '',
        customFields: {},
        country: '',
        countryCode: defaultCountryCode,
        stateOrProvince: '',
        stateOrProvinceCode: '',
        postalCode: '',
        phone: '',
        city: '',
        company: '',
        shouldSaveAddress: false,
    }),
    validationSchema: ({
        language,
        getFields,
    }: AddressFormProps & WithLanguageProps) => (
        lazy<Partial<AddressFormValues>>(values => getAddressFormFieldsValidationSchema({
            language,
            formFields: getFields(values && values.countryCode),
        }))
    ),
})(SaveAddress));
 
const AddressFormModal: FunctionComponent<AddressFormModalProps> = ({
    isOpen,
    onAfterOpen,
    onRequestClose,
    ...addressFormProps
}) => (
    <Modal
        additionalModalClassName="modal--medium"
        header={
            <ModalHeader>
                <TranslatedString id="address.add_address_heading" />
            </ModalHeader>
        }
        isOpen={ isOpen }
        onAfterOpen={ onAfterOpen }
        onRequestClose={ onRequestClose }
        shouldShowCloseButton={ true }
    >
        <SaveAddressForm
            { ...addressFormProps }
            onRequestClose={ onRequestClose }
        />
    </Modal>
);
 
export default AddressFormModal;