import { Button } from '@components/common/ui/Button.js'; import { Item, ItemContent, ItemDescription, ItemTitle } from '@components/common/ui/Item.js'; import { RadioGroup, RadioGroupItem } from '@components/common/ui/RadioGroup.js'; import { useCheckout, useCheckoutDispatch } from '@components/frontStore/checkout/CheckoutContext.js'; import CustomerAddressForm from '@components/frontStore/customer/address/addressForm/Index.js'; import { serverAddressErrors } from '@components/frontStore/customer/address/addressFormLogic.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import { Address, AddressGraphql } from '@evershop/evershop/types/address'; import React, { useEffect, useState } from 'react'; import { useWatch } from 'react-hook-form'; export function BillingAddress({ billingAddress, addBillingAddress, addingBillingAddress, noShippingRequired }: { billingAddress?: AddressGraphql; addBillingAddress?: (address: Address) => Promise; addingBillingAddress?: boolean; noShippingRequired: boolean; }) { const { form, checkoutData } = useCheckout(); const { updateCheckoutData } = useCheckoutDispatch(); const { getValues, trigger, formState: { disabled } } = form; const shippingAddress = useWatch({ control: form.control, name: 'shippingAddress' }); const billingAddressField = useWatch({ control: form.control, name: 'billingAddress' }); const [useSameAddress, setUseSameAddress] = useState(!noShippingRequired); // The billing form fills itself: `CustomerAddressForm` maps a stored // address (the GraphQL shape) to its field ids with `initialValuesFor`. // Writing `billingAddress` into the form here put that shape in raw — // `locality` and `country` as objects, `addressLine1`-style keys no field // reads — so a saved address came back as City "[object Object]" and an // empty Country, and saving it again failed with unknown address fields. useEffect(() => { if (useSameAddress && shippingAddress) { updateCheckoutData({ billingAddress: shippingAddress }); } }, [useSameAddress, checkoutData.shippingAddress]); useEffect(() => { if (!useSameAddress) { const billingAddress = { ...getValues('billingAddress') }; updateCheckoutData({ billingAddress }); } }, [billingAddressField]); const handleAddressOptionChange = (value: string) => { const isSameAddress = value === 'same'; if (isSameAddress === useSameAddress || disabled) { return; } setUseSameAddress(isSameAddress); if (!isSameAddress) { updateCheckoutData({ billingAddress: undefined }); } else if (checkoutData.shippingAddress) { updateCheckoutData({ billingAddress: checkoutData.shippingAddress }); } }; const handleGoToPayment = async () => { const isValid = await trigger('billingAddress'); if (isValid && addBillingAddress) { const billingAddressData = getValues('billingAddress'); try { await addBillingAddress(billingAddressData); } catch (error) { // Field-targeted errors from the server land on the inputs (spec § 3.8). serverAddressErrors(error, 'billingAddress', (name, err) => form.setError(name, err) ); throw error; } } }; return (
{_('Billing Address')} { handleAddressOptionChange(value as string); }} > {!noShippingRequired ? ( <> {/* Whole-box click selects each option (Base UI radios only react to pointer events on their [role="radio"] element, so the boxes carry click handlers). The guard skips the radio itself and, on the second box, the controls of the expanded billing-address form. */} { if ( (e.target as HTMLElement).closest('[role="radio"]') ) { return; } if (!useSameAddress) { handleAddressOptionChange('same'); } }} className="cursor-pointer" > { if ( (e.target as HTMLElement).closest( 'input, button, a, select, textarea, [role="radio"], [role="combobox"]' ) ) { return; } if (useSameAddress) { handleAddressOptionChange('different'); } }} className="cursor-pointer" > {/* ItemDescription clamps its text to two lines; a form inside it must not be clamped, or the address preview is cut short and the button below covers it (#1175). */} {!useSameAddress && (
{noShippingRequired && ( )}
)}
) : (
{/* No shipping: the billing address is the only address, so the one already saved on the cart is shown. */} {noShippingRequired && ( )}
)}
); }