import { CheckboxField } from '@components/common/form/CheckboxField.js'; import { Form } from '@components/common/form/Form.js'; import { InputField } from '@components/common/form/InputField.js'; import { NumberField } from '@components/common/form/NumberField.js'; import { Button } from '@components/common/ui/Button.js'; import { toast } from '@components/common/ui/Sonner.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import axios from 'axios'; import React from 'react'; import { useForm } from 'react-hook-form'; export interface PackageData { uuid: string; name: string; length: number; width: number; height: number; weight: { value: number; unit: string }; isDefault: boolean; updateApi: string; deleteApi: string; } export interface PackageFormProps { /** POST to create, PATCH to update. */ formMethod: 'POST' | 'PATCH'; saveApi: string; /** Receives the raw created/updated `package` row from the API response. */ onSuccess: (row?: Record) => void; reload?: () => void; pkg?: PackageData; dimensionUnit?: string; weightUnit?: string; } /** * Create/edit form for a package (parcel size). Height may be 0 — a flat * envelope. `weight` is the TARE (the empty package's own weight) and is * optional; it is added to the shipping weight for quotes and labels. * Submits via axios so business errors from the API (duplicate name, * default-swap rules) surface as toasts. */ export function PackageForm({ formMethod, saveApi, onSuccess, reload, pkg, dimensionUnit, weightUnit }: PackageFormProps) { const form = useForm(); const [saving, setSaving] = React.useState(false); const onSubmit = form.handleSubmit(async (values) => { setSaving(true); try { const response = await axios({ method: formMethod, url: saveApi, data: values, validateStatus: () => true }); if (response.data?.error) { toast.error(response.data.error.message); } else { toast.success( formMethod === 'POST' ? _('Package created') : _('Package saved') ); onSuccess(response.data?.data); if (reload) reload(); } } catch (e) { toast.error((e as Error).message); } finally { setSaving(false); } }); return (
{ /* we submit via axios — Form's submit path isn't used */ }} form={form} >

{formMethod === 'POST' ? _('Create a new package') : _('Edit package')}

); }