import clsx from 'clsx'; import { useEffect, useMemo, useState } from 'react'; import { useAppSelector } from '@akinon/next/redux/hooks'; import { RootState } from '@theme/redux/store'; import { useSetAddressesMutation } from '@akinon/next/data/client/checkout'; import { CheckoutAddressType } from '@akinon/next/types'; import { Checkbox, Icon, Modal } from '@theme/components'; import { partAttrs } from '@akinon/pz-theme/src/utils/part-styles'; import AddressBox from './address-box'; import { useAddAddressMutation } from '@akinon/next/data/client/address'; import { AddressForm } from '@theme/views/account/address-form'; import { useLocalization } from '@akinon/next/hooks'; import PluginModule, { Component } from '@akinon/next/components/plugin-module'; import { Trans } from '@akinon/next/components/trans'; const Addresses = () => { const { t } = useLocalization(); const [isModalOpen, setIsModalOpen] = useState(false); const addressList = useAppSelector( (state: RootState) => state.checkout.addressList ); const { shipping_address, billing_address, billing_and_shipping_same, delivery_option } = useAppSelector((state: RootState) => state.checkout.preOrder) ?? {}; const [isBillingAddressSame, setIsBillingAddressSame] = useState( billing_and_shipping_same ); const addressTypes: CheckoutAddressType[] = useMemo( () => [ { label: 'Delivery', text: t('checkout.address.delivery'), value: shipping_address?.pk, requestParam: 'shippingAddressPk' }, { label: 'Billing', text: t('checkout.address.billing'), value: billing_address?.pk, requestParam: 'billingAddressPk' } ], [shipping_address?.pk, billing_address?.pk, t] ); const [setAddresses] = useSetAddressesMutation(); const [addAddress] = useAddAddressMutation(); const handleSameBillingAddressChange = () => { setIsBillingAddressSame((prevValue) => { if (!prevValue) { setAddresses({ shippingAddressPk: shipping_address?.pk, billingAddressPk: shipping_address?.pk }); } return !prevValue; }); }; const setSelectAddresses = (pk: number) => { const addresses = { shippingAddressPk: pk, billingAddressPk: pk }; setAddresses(addresses); }; useEffect(() => { setIsBillingAddressSame(billing_and_shipping_same); }, [billing_and_shipping_same]); const onSubmit = (data) => { addAddress({ ...data, invalidateTag: 'Checkout' }) .unwrap() .then((addAddressResponse) => { if (addressList.length === 0) { setSelectAddresses(addAddressResponse.pk); } }) .catch(); setIsModalOpen(false); }; const AddressType = (props) => { return props.title; }; return (