|
| 1 | +import { CheckboxField } from '@components/common/form/CheckboxField.js'; |
| 2 | +import { Form } from '@components/common/form/Form.js'; |
| 3 | +import { InputField } from '@components/common/form/InputField.js'; |
| 4 | +import { NumberField } from '@components/common/form/NumberField.js'; |
| 5 | +import { Button } from '@components/common/ui/Button.js'; |
| 6 | +import axios from 'axios'; |
| 7 | +import React from 'react'; |
| 8 | +import { useForm } from 'react-hook-form'; |
| 9 | +import { toast } from 'react-toastify'; |
| 10 | + |
| 11 | +export interface PackageData { |
| 12 | + uuid: string; |
| 13 | + name: string; |
| 14 | + length: number; |
| 15 | + width: number; |
| 16 | + height: number; |
| 17 | + weight: { value: number; unit: string }; |
| 18 | + isDefault: boolean; |
| 19 | + updateApi: string; |
| 20 | + deleteApi: string; |
| 21 | +} |
| 22 | + |
| 23 | +export interface PackageFormProps { |
| 24 | + /** POST to create, PATCH to update. */ |
| 25 | + formMethod: 'POST' | 'PATCH'; |
| 26 | + saveApi: string; |
| 27 | + /** Receives the raw created/updated `package` row from the API response. */ |
| 28 | + onSuccess: (row?: Record<string, unknown>) => void; |
| 29 | + reload?: () => void; |
| 30 | + pkg?: PackageData; |
| 31 | + dimensionUnit?: string; |
| 32 | + weightUnit?: string; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Create/edit form for a package (parcel size). Height may be 0 — a flat |
| 37 | + * envelope. `weight` is the TARE (the empty package's own weight) and is |
| 38 | + * optional; it is added to the shipping weight for quotes and labels. |
| 39 | + * Submits via axios so business errors from the API (duplicate name, |
| 40 | + * default-swap rules) surface as toasts. |
| 41 | + */ |
| 42 | +export function PackageForm({ |
| 43 | + formMethod, |
| 44 | + saveApi, |
| 45 | + onSuccess, |
| 46 | + reload, |
| 47 | + pkg, |
| 48 | + dimensionUnit, |
| 49 | + weightUnit |
| 50 | +}: PackageFormProps) { |
| 51 | + const form = useForm(); |
| 52 | + const [saving, setSaving] = React.useState(false); |
| 53 | + |
| 54 | + const onSubmit = form.handleSubmit(async (values) => { |
| 55 | + setSaving(true); |
| 56 | + try { |
| 57 | + const response = await axios({ |
| 58 | + method: formMethod, |
| 59 | + url: saveApi, |
| 60 | + data: values, |
| 61 | + validateStatus: () => true |
| 62 | + }); |
| 63 | + if (response.data?.error) { |
| 64 | + toast.error(response.data.error.message); |
| 65 | + } else { |
| 66 | + toast.success( |
| 67 | + formMethod === 'POST' ? 'Package created' : 'Package saved' |
| 68 | + ); |
| 69 | + onSuccess(response.data?.data); |
| 70 | + if (reload) reload(); |
| 71 | + } |
| 72 | + } catch (e) { |
| 73 | + toast.error((e as Error).message); |
| 74 | + } finally { |
| 75 | + setSaving(false); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + return ( |
| 80 | + <Form |
| 81 | + id="packageForm" |
| 82 | + method={formMethod} |
| 83 | + action={saveApi} |
| 84 | + submitBtn={false} |
| 85 | + onSuccess={() => { |
| 86 | + /* we submit via axios — Form's submit path isn't used */ |
| 87 | + }} |
| 88 | + form={form} |
| 89 | + > |
| 90 | + <div className="space-y-3"> |
| 91 | + <h3 className="text-lg font-semibold"> |
| 92 | + {formMethod === 'POST' ? 'Create a new package' : 'Edit package'} |
| 93 | + </h3> |
| 94 | + <InputField |
| 95 | + name="name" |
| 96 | + label="Name" |
| 97 | + placeholder="e.g. Small Box" |
| 98 | + required |
| 99 | + validation={{ required: 'Name is required' }} |
| 100 | + defaultValue={pkg?.name} |
| 101 | + /> |
| 102 | + <div className="grid grid-cols-3 gap-x-3"> |
| 103 | + <NumberField |
| 104 | + name="length" |
| 105 | + label="Length" |
| 106 | + unit={dimensionUnit} |
| 107 | + required |
| 108 | + validation={{ |
| 109 | + required: 'Length is required', |
| 110 | + min: { value: 0.01, message: 'Length must be greater than 0' } |
| 111 | + }} |
| 112 | + defaultValue={pkg?.length} |
| 113 | + /> |
| 114 | + <NumberField |
| 115 | + name="width" |
| 116 | + label="Width" |
| 117 | + unit={dimensionUnit} |
| 118 | + required |
| 119 | + validation={{ |
| 120 | + required: 'Width is required', |
| 121 | + min: { value: 0.01, message: 'Width must be greater than 0' } |
| 122 | + }} |
| 123 | + defaultValue={pkg?.width} |
| 124 | + /> |
| 125 | + <NumberField |
| 126 | + name="height" |
| 127 | + label="Height" |
| 128 | + unit={dimensionUnit} |
| 129 | + required |
| 130 | + validation={{ |
| 131 | + required: 'Height is required (0 for envelopes)', |
| 132 | + min: { value: 0, message: 'Height must be 0 or greater' } |
| 133 | + }} |
| 134 | + helperText="0 for flat envelopes" |
| 135 | + defaultValue={pkg?.height} |
| 136 | + /> |
| 137 | + </div> |
| 138 | + <NumberField |
| 139 | + name="weight" |
| 140 | + label="Empty package weight" |
| 141 | + unit={weightUnit} |
| 142 | + validation={{ |
| 143 | + min: { value: 0, message: 'Weight must be 0 or greater' } |
| 144 | + }} |
| 145 | + helperText="Optional. Added to the shipping weight for quotes and labels." |
| 146 | + defaultValue={pkg?.weight?.value ?? 0} |
| 147 | + /> |
| 148 | + <CheckboxField |
| 149 | + name="is_default" |
| 150 | + label="Default package" |
| 151 | + defaultValue={pkg?.isDefault === true} |
| 152 | + helperText="Preselected for new products. Exactly one package is the default." |
| 153 | + /> |
| 154 | + <div className="flex justify-end gap-2 pt-2"> |
| 155 | + <Button |
| 156 | + title="Save" |
| 157 | + variant="default" |
| 158 | + type="button" |
| 159 | + disabled={saving} |
| 160 | + onClick={onSubmit} |
| 161 | + > |
| 162 | + {saving ? 'Saving…' : 'Save'} |
| 163 | + </Button> |
| 164 | + </div> |
| 165 | + </div> |
| 166 | + </Form> |
| 167 | + ); |
| 168 | +} |
0 commit comments