import { useState, type ReactNode } from "react"; import { Hash, Loader2 } from "lucide-react"; import { Dialog, DialogClose, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Button } from "@/components/ui/button"; import { Textarea } from "@/components/ui/textarea"; import type { FileUploadEntity, FileUploadSetting, } from "@/types/fileUploadSettings"; import { useCreateFileUploadSetting, useUpdateFileUploadSetting, } from "@/hooks/useFileUploadSettings"; export interface EditFileUploadSettingDialogProps { mode?: "create" | "edit"; setting?: FileUploadSetting; children: ReactNode; } const selectClass = "flex h-10 w-full rounded-md border border-slate-200 bg-white px-3 py-2 text-sm text-slate-700 shadow-xs outline-none transition hover:border-slate-300 focus:border-[#10B981]/50 focus:ring-2 focus:ring-[#10B981]/20"; const ENTITIES: FileUploadEntity[] = [ "customer", "booking", "consignment", "shipment", "invoice", "train", "other", ]; export default function EditFileUploadSettingDialog({ mode = "create", setting, children, }: EditFileUploadSettingDialogProps) { const isEdit = mode === "edit"; const [open, setOpen] = useState(false); const [code, setCode] = useState(setting?.code ?? ""); const [label, setLabel] = useState(setting?.label ?? ""); const [entity, setEntity] = useState( setting?.entity ?? "other", ); const [description, setDescription] = useState(setting?.description ?? ""); const [error, setError] = useState(null); const createMutation = useCreateFileUploadSetting(); const updateMutation = useUpdateFileUploadSetting(); const pending = createMutation.isPending || updateMutation.isPending; const reset = () => { setCode(setting?.code ?? ""); setLabel(setting?.label ?? ""); setEntity(setting?.entity ?? "other"); setDescription(setting?.description ?? ""); setError(null); }; const handleSubmit = () => { setError(null); if (!code.trim() || !label.trim()) { setError("Code and label are required."); return; } const payload = { code: code.trim(), label: label.trim(), entity, description: description.trim() || undefined, }; const onDone = () => { setOpen(false); if (!isEdit) reset(); }; const onError = (err: unknown) => { setError( err instanceof Error ? err.message : "Something went wrong. Try again.", ); }; if (isEdit && setting) { updateMutation.mutate( { id: setting.id, dto: payload }, { onSuccess: onDone, onError }, ); } else { createMutation.mutate(payload, { onSuccess: onDone, onError }); } }; return ( { setOpen(next); if (!next) reset(); }} > {children} {isEdit ? "Edit File Upload Setting" : "New File Upload Setting"} {isEdit ? "Update the metadata for this file upload group." : "Define a new file upload group that a form can reference by code."}
setCode(e.target.value)} placeholder="e.g. customer_registration" className="pl-10 font-mono" disabled={isEdit} />

{isEdit ? "Code is immutable after creation." : "Stable identifier used in code. Use snake_case."}

setLabel(e.target.value)} placeholder="e.g. Customer Registration" />

Domain the upload group applies to.

Manage fields from the "Fields" action on the list.