import React, { FC } from 'react' import { Button, CheckboxControl, Notice, Snackbar, } from '@wordpress/components' import { createInterpolateElement } from '@wordpress/element' import { Badge, Text } from '@wordpress/ui' import HsCodeSelector from './HsCodeSelector' import { OrderItem } from '../model/models' import { useI18n } from '@wordpress/react-i18n' import apiFetch from '@wordpress/api-fetch' export declare interface CustomsInformationHsCodesProps { itemsMissingHsCode: Array onCancel: () => void onSave: () => void } const CustomsInformationHsCodes: FC = ({ itemsMissingHsCode, onCancel, onSave, }) => { const [inProgress, setInProgress] = React.useState(false) const [applyToAll, setApplyToAll] = React.useState(false) const [updateFailed, setUpdateFailed] = React.useState(false) const [hsCodeForAll, setHsCodeForAll] = React.useState< string | null | undefined >() const [updatedItems, setUpdatedItems] = React.useState>(itemsMissingHsCode) const { __ } = useI18n() const onSelectHsCode = ( hsCode: string | null | undefined, item?: OrderItem ) => { setUpdatedItems(prevItems => prevItems.map(prevItem => { if (item) { return prevItem.product_id === item.product_id ? { ...prevItem, hs_code: hsCode ?? undefined, } : prevItem } else { return itemsMissingHsCode.find( i => i.product_id === prevItem.product_id ) ? { ...prevItem, hs_code: hsCode ?? undefined, } : prevItem } }) ) } const saveHsCodes = async () => { setInProgress(true) setUpdateFailed(false) await apiFetch({ path: '/posten-bring-checkout/hscodes', method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify( updatedItems.map(item => ({ productId: item.product_id, hsCode: item.hs_code, })) ), }) .then(() => { onSave() }) .catch(() => setUpdateFailed(true)) .finally(() => setInProgress(false)) } // Stacked item images position calculation const imageSize = 55 const maxSpread = 30 const xStep = itemsMissingHsCode.length > 1 ? maxSpread / (itemsMissingHsCode.length - 1) : 0 const yStep = itemsMissingHsCode.length > 1 ? 24 / (itemsMissingHsCode.length - 1) : 0 const stackWidth = imageSize + maxSpread const stackHeight = imageSize + 24 return (
{createInterpolateElement( __( 'For the fastest and most cost-effective shipping, shipments to Northern Norway are transported by rail through Sweden. Due to new EU regulations, these shipments now require customs information. Read more about new customs regulations for Norwegian goods in transit (NVIT)', 'posten-bring-checkout' ), { link: ( ), } )} {itemsMissingHsCode.length > 1 && (
{ setApplyToAll(checked) setHsCodeForAll(null) }} checked={applyToAll} label={__( 'All items have the same HS code', 'posten-bring-checkout' )} />
)}
{applyToAll && (
{updatedItems .filter(item => itemsMissingHsCode.find( i => item.product_id === i.product_id ) ) .slice(0, 4) .map((item, index) => ( ))}
{__('All items', 'posten-bring-checkout')} { setHsCodeForAll(hsCode) onSelectHsCode(hsCode, undefined) }} />
)} {!applyToAll && updatedItems .filter(item => itemsMissingHsCode.find(i => item.product_id === i.product_id) ) .map(item => (
{item.name}
{item.name} onSelectHsCode(hsCode, item)} />
))}
{updateFailed && ( setUpdateFailed(false)} > {__( 'An error occurred while saving HS codes. Please try again', 'posten-bring-checkout' )} )}
{__( 'HS code data is provided by Tolletaten (Norwegian customs)', 'posten-bring-checkout' )}
) } export default CustomsInformationHsCodes