All files / app/shipping ShippingAddress.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  6x 6x 6x   6x   6x 6x 6x                                             6x                                   71x   1278x   71x   71x 5x           71x 13x 1x     13x     71x 5x 5x   5x 4x             4x                       1x 1x   1x           1x                             66x                                   6x  
import { Address, CheckoutSelectors, Consignment, Country, CustomerAddress, FormField, ShippingInitializeOptions, ShippingRequestOptions } from '@bigcommerce/checkout-sdk';
import { memoizeOne } from '@bigcommerce/memoize';
import { noop } from 'lodash';
import React, { memo, useCallback, useContext, FunctionComponent } from 'react';
 
import { FormContext } from '../ui/form';
 
import RemoteShippingAddress from './RemoteShippingAddress';
import ShippingAddressForm from './ShippingAddressForm';
import StaticAddressEditable from './StaticAddressEditable';
 
export interface ShippingAddressProps {
    addresses: CustomerAddress[];
    consignments: Consignment[];
    countries?: Country[];
    countriesWithAutocomplete: string[];
    formFields: FormField[];
    googleMapsApiKey?: string;
    isLoading: boolean;
    isShippingStepPending: boolean;
    methodId?: string;
    shippingAddress?: Address;
    shouldShowSaveAddress?: boolean;
    hasRequestedShippingOptions: boolean;
    deinitialize(options: ShippingRequestOptions): Promise<CheckoutSelectors>;
    initialize(options: ShippingInitializeOptions): Promise<CheckoutSelectors>;
    onAddressSelect(address: Address): void;
    onFieldChange(name: string, value: string): void;
    onUnhandledError?(error: Error): void;
    onUseNewAddress(): void;
}
 
const ShippingAddress: FunctionComponent<ShippingAddressProps> = props => {
    const {
        methodId,
        formFields,
        countries,
        countriesWithAutocomplete,
        consignments,
        googleMapsApiKey,
        onAddressSelect,
        onFieldChange,
        onUseNewAddress,
        initialize,
        deinitialize,
        isLoading,
        shippingAddress,
        hasRequestedShippingOptions,
        addresses,
        shouldShowSaveAddress,
        onUnhandledError = noop,
        isShippingStepPending,
    } = props;
 
    const { setSubmitted } = useContext(FormContext);
 
    const initializeShipping = useCallback(memoizeOne((defaultOptions: ShippingInitializeOptions) => (
        (options?: ShippingInitializeOptions) => initialize({
            ...defaultOptions,
            ...options,
        })
    )), []);
 
    const handleFieldChange: (fieldName: string, value: string) => void = (fieldName, value) => {
        if (hasRequestedShippingOptions) {
            setSubmitted(true);
        }
 
        onFieldChange(fieldName, value);
    };
 
    if (methodId) {
        const containerId = 'addressWidget';
        let options: ShippingInitializeOptions = {};
 
        if (methodId === 'amazon') {
            options = {
                amazon: {
                    container: containerId,
                    onError: onUnhandledError,
                },
            };
 
            return (
                <RemoteShippingAddress
                    containerId={ containerId }
                    deinitialize={ deinitialize }
                    formFields={ formFields }
                    initialize={ initializeShipping(options) }
                    methodId={ methodId }
                    onFieldChange={ onFieldChange }
                />
            );
        }
 
        Eif (methodId === 'amazonpay' && shippingAddress) {
            const editAddressButtonId = 'edit-ship-button';
 
            options = {
                amazonpay: {
                    editAddressButtonId,
                },
            };
 
            return (
                <StaticAddressEditable
                    address={ shippingAddress }
                    buttonId={ editAddressButtonId }
                    deinitialize={ deinitialize }
                    formFields={ formFields }
                    initialize={ initializeShipping(options) }
                    isLoading={ isShippingStepPending }
                    methodId={ methodId }
                    onFieldChange={ onFieldChange }
                />
            );
        }
    }
 
    return (
        <ShippingAddressForm
            address={ shippingAddress }
            addresses={ addresses }
            consignments={ consignments }
            countries={ countries }
            countriesWithAutocomplete={ countriesWithAutocomplete }
            formFields={ formFields }
            googleMapsApiKey={ googleMapsApiKey }
            isLoading={ isLoading }
            onAddressSelect={ onAddressSelect }
            onFieldChange={ handleFieldChange }
            onUseNewAddress={ onUseNewAddress }
            shouldShowSaveAddress={ shouldShowSaveAddress }
        />
    );
};
 
export default memo(ShippingAddress);