diff --git a/distribution_fe/app/(dashboard)/operations/deliveries/_components/delivery-task-fail-dialog.tsx b/distribution_fe/app/(dashboard)/operations/deliveries/_components/delivery-task-fail-dialog.tsx new file mode 100644 index 0000000..a9d0011 --- /dev/null +++ b/distribution_fe/app/(dashboard)/operations/deliveries/_components/delivery-task-fail-dialog.tsx @@ -0,0 +1,103 @@ +'use client' + +import { useEffect } from 'react' +import { useForm, zodResolver, z } from '@/lib/form' +import { FormField } from '@/components/shared/form-field' +import { Input } from '@/components/ui/input' +import { Button } from '@/components/ui/button' +import { + Dialog, + DialogContent, + DialogHeader, + DialogTitle, + DialogDescription, + DialogFooter, +} from '@/components/ui/dialog' +import { FieldGroup } from '@/components/ui/field' +import { toast } from '@/components/ui/toast' +import { deliveryTasks as deliveryTasksApi, getApiError } from '@/lib/api/index' +import type { DeliveryTaskResponse } from '@/types/api' + +const schema = z.object({ + note: z.string().min(1, 'Reason is required'), +}) + +type FormValues = z.infer + +interface DeliveryTaskFailDialogProps { + open: boolean + onClose: () => void + onSuccess: () => void + task: DeliveryTaskResponse | null +} + +export function DeliveryTaskFailDialog({ open, onClose, onSuccess, task }: DeliveryTaskFailDialogProps) { + const form = useForm({ + resolver: zodResolver(schema), + defaultValues: { note: '' }, + }) + + useEffect(() => { + if (open) { + form.reset({ note: '' }) + } + }, [open]) + + if (!task) return null + + async function onSubmit(values: FormValues) { + try { + await deliveryTasksApi.fail(task!.id, values.note) + toast.add({ title: 'Delivery task marked as failed', type: 'success' }) + onSuccess() + onClose() + } catch (err) { + toast.add({ title: 'Failed to update task', description: getApiError(err), type: 'error' }) + } + } + + return ( + !v && onClose()}> + + + Mark Delivery Failed + + Record why this delivery could not be completed. + + + +
+ + ( + + )} + /> + + + + + + +
+
+
+ ) +}