diff --git a/src/app/(dashboard)/software/components/assignment-form-sheet.tsx b/src/app/(dashboard)/software/components/assignment-form-sheet.tsx new file mode 100644 index 0000000..644a22e --- /dev/null +++ b/src/app/(dashboard)/software/components/assignment-form-sheet.tsx @@ -0,0 +1,236 @@ +"use client"; + +import { useActionState, useEffect, useState } from "react"; +import { Loader2, Save } from "lucide-react"; +import { toast } from "sonner"; + +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select"; +import { + Sheet, + SheetContent, + SheetDescription, + SheetFooter, + SheetHeader, + SheetTitle, +} from "@/components/ui/sheet"; +import { Textarea } from "@/components/ui/textarea"; +import { + createAssignmentAction, + updateAssignmentAction, +} from "@/lib/appwrite/software-actions"; +import { initialSoftwareState } from "@/lib/appwrite/software-types"; +import type { AssignmentRow, CustomerOption, SoftwareOption } from "./types"; + +type Props = { + open: boolean; + onOpenChange: (v: boolean) => void; + assignment?: AssignmentRow | null; + customers: CustomerOption[]; + softwareOptions: SoftwareOption[]; +}; + +function isoToInputDate(iso: string): string { + if (!iso) return ""; + return iso.slice(0, 10); +} + +export function AssignmentFormSheet({ + open, + onOpenChange, + assignment, + customers, + softwareOptions, +}: Props) { + const isEdit = Boolean(assignment); + const action = isEdit ? updateAssignmentAction : createAssignmentAction; + const [state, formAction, isPending] = useActionState(action, initialSoftwareState); + const [selectedSoftware, setSelectedSoftware] = useState(assignment?.softwareId ?? ""); + + useEffect(() => { + setSelectedSoftware(assignment?.softwareId ?? ""); + }, [assignment]); + + useEffect(() => { + if (state.ok) { + toast.success(isEdit ? "Atama güncellendi." : "Atama oluşturuldu."); + onOpenChange(false); + } else if (state.error) { + toast.error(state.error); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [state]); + + const defaultFee = + softwareOptions.find((s) => s.id === selectedSoftware)?.defaultFee ?? ""; + + const blocked = customers.length === 0 || softwareOptions.length === 0; + + return ( + + + + + {isEdit ? "Atamayı düzenle" : "Müşteriye yazılım ata"} + + + {blocked + ? "Atama yapmak için en az bir müşteri ve bir yazılım gerekli." + : "Yazılımı bir müşteriye atayın; varsayılan ücret özelleştirilebilir."} + + + +
+ {isEdit && assignment && } + +
+
+ + + {state.fieldErrors?.customerId && ( +

{state.fieldErrors.customerId}

+ )} +
+ +
+ + + {state.fieldErrors?.softwareId && ( +

{state.fieldErrors.softwareId}

+ )} +
+ +
+
+ + +
+
+ + +
+
+ +
+
+ + +
+
+ + +
+
+ +
+ +