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 { FileUploadEntity } from "@edr/types/freight"; import { useMutation } from "@tanstack/react-query"; import { api } from "@/services/api"; import { FileUploadSetting } from "@/types/fileUploadSettings"; export interface EditFileUploadSettingDialogProps { mode?: "create" | "edit"; setting?: FileUploadSetting; children: ReactNode; } 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 = useMutation( api.fileUploadSettings.create.mutationOptions(), ); const updateMutation = useMutation( api.fileUploadSettings.update.mutationOptions(), ); 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.