All files / app/address mapAddressToFormValues.ts

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    26x           26x 240x   2044x 2044x 411x 141x     411x   22x   411x   411x   411x     1633x 1633x     1633x           240x         240x 129x     240x 129x     240x       411x 405x     6x       6x       405x       405x       405x       1633x    
import { Address, AddressKey, FormField } from '@bigcommerce/checkout-sdk';
 
import { DynamicFormFieldType } from '../ui/form';
 
export type AddressFormValues = Pick<Address, Exclude<AddressKey, 'customFields'>> & {
    customFields: { [id: string]: any };
};
 
export default function mapAddressToFormValues(fields: FormField[], address?: Address): AddressFormValues {
    const values = ({
        ...fields.reduce(
            (addressFormValues, { name, custom, fieldType, default: defaultValue }) => {
                if (custom) {
                    if (!addressFormValues.customFields) {
                        addressFormValues.customFields = {};
                    }
 
                    const field = address &&
                        address.customFields &&
                        address.customFields.find(({ fieldId }) => fieldId === name);
 
                    const fieldValue = (field && field.fieldValue);
 
                    addressFormValues.customFields[name] = getValue(fieldType, fieldValue, defaultValue);
 
                    return addressFormValues;
                }
 
                Eif (isSystemAddressFieldName(name)) {
                    addressFormValues[name] = (address && address[name]) || '';
                }
 
                return addressFormValues;
            },
            {} as AddressFormValues
        ),
    });
 
    values.shouldSaveAddress = address && address.shouldSaveAddress !== undefined ?
        address.shouldSaveAddress :
        true;
 
    // Manually backfill stateOrProvince to avoid Formik warning (uncontrolled to controlled input)
    if (values.stateOrProvince === undefined) {
        values.stateOrProvince = '';
    }
 
    if (values.stateOrProvinceCode === undefined) {
        values.stateOrProvinceCode = '';
    }
 
    return values;
}
 
function getValue(fieldType?: string, fieldValue?: string | string[] | number, defaultValue?: string): string | string[] | number | Date | undefined {
    if (fieldValue === undefined || fieldValue === null) {
        return getDefaultValue(fieldType, defaultValue);
    }
 
    Iif (fieldType === DynamicFormFieldType.date && typeof fieldValue === 'string') {
        return fieldValue ? new Date(fieldValue) : undefined;
    }
 
    return fieldValue;
}
 
function getDefaultValue(fieldType?: string, defaultValue?: string): string | string[] | Date {
    Iif (defaultValue && fieldType === DynamicFormFieldType.date) {
        return new Date(defaultValue);
    }
 
    Iif (fieldType === DynamicFormFieldType.checkbox) {
        return [];
    }
 
    return defaultValue || '';
}
 
function isSystemAddressFieldName(fieldName: string): fieldName is Exclude<keyof Address, 'customFields' | 'shouldSaveAddress'> {
    return fieldName !== 'customFields' && fieldName !== 'shouldSaveAddress';
}