/* eslint-disable no-case-declarations, @typescript-eslint/camelcase */ import React, {useEffect, useReducer, MouseEvent, FormEvent} from 'react'; import {useForm, FormContext} from 'react-hook-form'; import classNames from 'classnames'; import './ShippingAddressSection.scss'; import {Link} from 'display-library/Link'; import {Button} from 'display-library/Button'; import {Expandable} from 'display-library/Expandable'; import ConfirmAddressModal from './ConfirmAddressModal'; import ShippingAddressForm from './ShippingAddressForm'; import ShippingAddressList from './ShippingAddressList'; import LoadingOverlay from 'shared/components/loader/overlay'; import {initialState, reducer} from './ShippingAddressReducer'; import ShippingAddressListItem from './ShippingAddressListItem'; import { validateAddress, createAddress, getAddresses, editAddress } from 'checkout/APIUtils'; import { AddressAttributes, SectionProps, CurrentView, Address, Option } from 'checkout/ShippingAddressProps'; const ShippingAddressSection: React.FC = ({ cartId, accessToken, useOldStyles, onNewAddress, onAddressEdit, defaultCountry, onAddressChange, initialSelectionId, ...formProps }) => { const formHelpers: any = useForm(); const {states, countries} = formProps; const {setValue, getValues} = formHelpers; const [state, dispatch] = useReducer(reducer, initialState); const { addresses, currentView, loadingLabel, currentState, currentCountry, modalAddresses, currentSelectionId } = state; const currentSelection = addresses.find( (a: Address) => a.id == currentSelectionId ); useEffect(() => { if (addresses.length) return; async function getAndSetAddresses() { const newAddresses = await getAddresses(accessToken); if (newAddresses.length) { dispatch(['SetAddresses', newAddresses]); dispatch(['SetCurrentView', 'closed']); } else { dispatch(['SetCurrentView', 'new']); } if (initialSelectionId) { dispatch(['SetCurrentSelectionId', initialSelectionId]); } else { dispatch(['SetCurrentSelectionId', newAddresses[0].id]); } } getAndSetAddresses(); }, [accessToken, addresses.length, initialSelectionId]); useEffect(() => { if (currentState.value) return; if (states && states[currentCountry.value]) dispatch(['SetCurrentState', states[currentCountry.value][0]]); }, [currentCountry.value, currentState.value, states]); useEffect(() => { if (currentCountry.value) return; if (defaultCountry) return dispatch(['SetCurrentCountry', defaultCountry]); if (countries) return dispatch(['SetCurrentCountry', countries[0]]); }, [defaultCountry, countries, currentCountry.value]); function handleCancelClick(event: MouseEvent) { if (event) event.preventDefault(); dispatch(['SetCurrentView', 'closed']); } function populateCountry() { const country = countries.find( (c: Option) => c.value === currentSelection.attributes.country_code ); dispatch(['SetCurrentCountry', country]); } function populateState() { const {country_code, state} = currentSelection.attributes; const countryStates = states[country_code]; if (countryStates) { dispatch([ 'SetCurrentState', countryStates.find((s: Option) => s.label === state) ]); } } function populateForm() { [ 'address_line1', 'address_line2', 'postal_code', 'first_name', 'last_name', 'phone', 'city' ].map(a => setValue(a, currentSelection.attributes[a])); setTimeout(() => { populateCountry(); populateState(); }, 500); } useEffect(() => { if (currentView === 'edit') populateForm(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [currentView]); function openModal() { dispatch(['SetLoadingLabel', 'validating']); const address = getValues(); address.country_code = currentCountry.value; address.state = currentState.value; validateAddress(address, accessToken).then(validatedAddress => { dispatch(['ClearLoadingLabel']); dispatch([ 'SetModalAddresses', {validated: validatedAddress, nonValidated: address} ]); const newView = `modal-${currentView}` as CurrentView; dispatch(['SetCurrentView', newView]); }); } async function handleUpdateClick(event: MouseEvent | FormEvent) { if (event) event.preventDefault(); switch (currentView) { case 'new': case 'edit': formHelpers.triggerValidation().then(() => { const hasErrors = Object.keys(formHelpers.errors).length; if (formHelpers.formState.dirty && !hasErrors) openModal(); }); break; case 'list': dispatch(['SetLoadingLabel', 'saving']); onAddressChange(currentSelection.id).then(() => { dispatch(['SetCurrentView', 'closed']); dispatch(['ClearLoadingLabel']); }); break; } } function setAndPersistSelectionChange( newAddress: Address, newAddresses: Array
) { dispatch(['SetCurrentSelectionId', newAddress.id]); dispatch(['SetAddresses', newAddresses]); onAddressChange(newAddress.id); } async function handleModalConfirm(address: AddressAttributes) { if (currentView === 'modal-new') { createAddress(address, accessToken).then(newAddress => { setAndPersistSelectionChange(newAddress, [...addresses, newAddress]); onNewAddress(newAddress); }); } if (currentView === 'modal-edit') { const newAddress = await editAddress( address, currentSelection.id, accessToken ); const newAddresses = addresses; const index = addresses.map(a => a.id).indexOf(newAddress.id); newAddresses.splice(index, 1, newAddress); setAndPersistSelectionChange(newAddress, newAddresses); onAddressEdit(newAddress); } } const bottomButtons = (
); const openContent = ( <> {(() => { switch (currentView) { case 'new': case 'edit': return ( dispatch(['SetCurrentCountry', country]) } setCurrentState={state => dispatch(['SetCurrentState', state])} currentCountry={currentCountry} currentState={currentState} /> ); case 'list': return ( dispatch(['SetCurrentSelectionId', id]) } setCurrentView={(view: CurrentView) => dispatch(['SetCurrentView', view]) } /> ); } })()} {bottomButtons} ); const closedContent = currentSelection ? ( ) : ( dispatch(['SetCurrentView', 'new'])} name="Add a new address" theme="underline" /> ); return (
{(currentView === 'modal-new' || currentView === 'modal-edit') && ( dispatch(['SetCurrentView', 'closed'])} handleConfirm={handleModalConfirm} addresses={modalAddresses} /> )} dispatch(['SetCurrentView', 'list'])} editButtonText={useOldStyles ? 'Change' : undefined} hideEditButton={!addresses || !currentSelection} />
); }; export default ShippingAddressSection;