All files / app/shipping ShippingAddressForm.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  7x   7x 7x 7x 7x                                     7x   49x 7x   98x 98x 98x 98x 98x 98x 98x 98x 98x 98x     98x     98x   98x 98x   98x                                                                 49x   4x 4x 4x   4x 48x     4x       4x     49x     13x   13x     49x             7x   7x  
import { Address, Consignment, Country, CustomerAddress, FormField } from '@bigcommerce/checkout-sdk';
import React, { Component, ReactNode } from 'react';
 
import {  isValidCustomerAddress, AddressForm, AddressSelect } from '../address';
import { connectFormik, ConnectFormikProps } from '../common/form';
import { Fieldset } from '../ui/form';
import { LoadingOverlay } from '../ui/loading';
 
import { SingleShippingFormValues } from './SingleShippingForm';
 
export interface ShippingAddressFormProps {
    addresses: CustomerAddress[];
    address?: Address;
    consignments: Consignment[];
    countries?: Country[];
    countriesWithAutocomplete: string[];
    googleMapsApiKey?: string;
    isLoading: boolean;
    formFields: FormField[];
    shouldShowSaveAddress?: boolean;
    onUseNewAddress(): void;
    onFieldChange(fieldName: string, value: string): void;
    onAddressSelect(address: Address): void;
}
 
const addressFieldName = 'shippingAddress';
 
class ShippingAddressForm extends Component<ShippingAddressFormProps & ConnectFormikProps<SingleShippingFormValues>> {
    render(): ReactNode {
        const {
            addresses,
            address: shippingAddress,
            onAddressSelect,
            onUseNewAddress,
            shouldShowSaveAddress,
            countries,
            countriesWithAutocomplete,
            formFields,
            isLoading,
            googleMapsApiKey,
            formik: {
                values: {
                    shippingAddress: formAddress,
                },
            },
        } = this.props;
 
        const hasAddresses = addresses && addresses.length > 0;
        const hasValidCustomerAddress = isValidCustomerAddress(shippingAddress, addresses, formFields);
 
        return (
            <Fieldset id="checkoutShippingAddress">
                { hasAddresses &&
                    <Fieldset id="shippingAddresses">
                        <LoadingOverlay isLoading={ isLoading }>
                            <AddressSelect
                                addresses={ addresses }
                                onSelectAddress={ onAddressSelect }
                                onUseNewAddress={ onUseNewAddress }
                                selectedAddress={ hasValidCustomerAddress ? shippingAddress : undefined }
                            />
                        </LoadingOverlay>
                    </Fieldset> }
 
                { !hasValidCustomerAddress &&
                    <LoadingOverlay isLoading={ isLoading } unmountContentWhenLoading>
                        <AddressForm
                            countries={ countries }
                            countriesWithAutocomplete={ countriesWithAutocomplete }
                            countryCode={ formAddress && formAddress.countryCode }
                            fieldName={ addressFieldName }
                            formFields={ formFields }
                            googleMapsApiKey={ googleMapsApiKey }
                            onAutocompleteToggle={ this.handleAutocompleteToggle }
                            onChange={ this.handleChange }
                            setFieldValue={ this.setFieldValue }
                            shouldShowSaveAddress={ shouldShowSaveAddress }
                        />
                    </LoadingOverlay> }
            </Fieldset>
        );
    }
 
    private setFieldValue: (fieldName: string, fieldValue: string) => void = (fieldName, fieldValue) => {
        const {
            formik: { setFieldValue },
            formFields,
        } = this.props;
 
        const customFormFieldNames = formFields
            .filter(field => field.custom)
            .map(field => field.name);
 
        const formFieldName = customFormFieldNames.includes(fieldName) ?
            `customFields.${fieldName}` :
            fieldName;
 
        setFieldValue(`${addressFieldName}.${formFieldName}`, fieldValue);
    };
 
    private handleChange: (fieldName: string, value: string) => void = (fieldName, value) => {
        const {
            onFieldChange,
        } = this.props;
 
        onFieldChange(fieldName, value);
    };
 
    private handleAutocompleteToggle: (state: { inputValue: string; isOpen: boolean }) => void = ({ isOpen, inputValue }) => {
        const { onFieldChange } = this.props;
 
        if (!isOpen) {
            onFieldChange('address1', inputValue);
        }
    };
}
 
export default connectFormik(ShippingAddressForm);