import { AddressSummary } from '@components/common/customer/address/AddressSummary.js'; import { CheckboxField } from '@components/common/form/CheckboxField.js'; import { Form } from '@components/common/form/Form.js'; import { Button } from '@components/common/ui/Button.js'; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from '@components/common/ui/Dialog.js'; import { Item, ItemActions, ItemContent } from '@components/common/ui/Item.js'; import { toast } from '@components/common/ui/Sonner.js'; import CustomerAddressForm from '@components/frontStore/customer/address/addressForm/Index.js'; import { serverAddressErrors } from '@components/frontStore/customer/address/addressFormLogic.js'; import { ExtendedCustomerAddress, useCustomer, useCustomerDispatch } from '@components/frontStore/customer/CustomerContext.jsx'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import React from 'react'; import { useForm } from 'react-hook-form'; const Address: React.FC<{ address: ExtendedCustomerAddress; }> = ({ address }) => { const { updateAddress, deleteAddress } = useCustomerDispatch(); const [dialogOpen, setDialogOpen] = React.useState(false); // Own form instance so field-targeted server errors can be set on the inputs. const form = useForm({ shouldUnregister: true, shouldFocusError: false }); const classes = address.isDefault ? 'border-2 border-primary' : ''; return (
{/* `render` makes the Button THE trigger. A Button as a child would nest } /> {_('Edit Address')}
{ try { await updateAddress(address.uuid as string, data); setDialogOpen(false); toast.success(_('Address has been updated successfully!')); } catch (error) { const { applied } = serverAddressErrors( error, '', (name, err) => form.setError(name, err) ); toast.error( applied > 0 ? _('Please check the highlighted fields') : error instanceof Error ? error.message : String(error) ); } }} >
); }; export function MyAddresses({ title }: { title?: string }) { const { customer } = useCustomer(); const { addAddress } = useCustomerDispatch(); const [dialogOpen, setDialogOpen] = React.useState(false); const form = useForm({ shouldUnregister: true, shouldFocusError: false }); if (!customer) { return null; } return (
{title && (

{_('Address Book')}

)} {customer.addresses.length === 0 && (
{_('You have no addresses saved')}
)}
{customer.addresses.map((address) => (
))}
{_('Add new address')} } /> {_('Add new address')}
{ try { await addAddress(data as ExtendedCustomerAddress); setDialogOpen(false); toast.success(_('Address has been saved successfully!')); } catch (error) { const { applied } = serverAddressErrors( error, '', (name, err) => form.setError(name, err) ); toast.error( applied > 0 ? _('Please check the highlighted fields') : error instanceof Error ? error.message : String(error) ); } }} >
); }