diff --git a/src/app/(dashboard)/services/components/delete-service-dialog.tsx b/src/app/(dashboard)/services/components/delete-service-dialog.tsx new file mode 100644 index 0000000..5ddc95e --- /dev/null +++ b/src/app/(dashboard)/services/components/delete-service-dialog.tsx @@ -0,0 +1,76 @@ +"use client"; + +import { useTransition } from "react"; +import { Loader2, Trash2 } from "lucide-react"; +import { toast } from "sonner"; + +import { Button } from "@/components/ui/button"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from "@/components/ui/dialog"; +import { deleteServiceAction } from "@/lib/appwrite/service-actions"; + +export function DeleteServiceDialog({ + open, + onOpenChange, + id, + name, +}: { + open: boolean; + onOpenChange: (v: boolean) => void; + id: string | null; + name: string; +}) { + const [isPending, startTransition] = useTransition(); + + const handleDelete = () => { + if (!id) return; + startTransition(async () => { + const fd = new FormData(); + fd.set("id", id); + const result = await deleteServiceAction(fd); + if (result.ok) { + toast.success("Hizmet silindi."); + onOpenChange(false); + } else { + toast.error(result.error ?? "Silme başarısız."); + } + }); + }; + + return ( + + + + Hizmeti sil + + {name} kalıcı olarak silinecek. Bu işlem geri alınamaz. + + + + + + + + + ); +} diff --git a/src/app/(dashboard)/services/components/service-form-sheet.tsx b/src/app/(dashboard)/services/components/service-form-sheet.tsx new file mode 100644 index 0000000..c234238 --- /dev/null +++ b/src/app/(dashboard)/services/components/service-form-sheet.tsx @@ -0,0 +1,204 @@ +"use client"; + +import { useActionState, useEffect } 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 { Switch } from "@/components/ui/switch"; +import { Textarea } from "@/components/ui/textarea"; +import { + createServiceAction, + updateServiceAction, +} from "@/lib/appwrite/service-actions"; +import { initialServiceState } from "@/lib/appwrite/service-types"; +import type { CustomerOption, ServiceRow } from "./types"; + +type Props = { + open: boolean; + onOpenChange: (v: boolean) => void; + service?: ServiceRow | null; + customers: CustomerOption[]; + defaultCustomerId?: string; +}; + +export function ServiceFormSheet({ + open, + onOpenChange, + service, + customers, + defaultCustomerId, +}: Props) { + const isEdit = Boolean(service); + const action = isEdit ? updateServiceAction : createServiceAction; + const [state, formAction, isPending] = useActionState(action, initialServiceState); + + useEffect(() => { + if (state.ok) { + toast.success(isEdit ? "Hizmet güncellendi." : "Hizmet eklendi."); + onOpenChange(false); + } else if (state.error) { + toast.error(state.error); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [state]); + + return ( + + + + {isEdit ? "Hizmeti düzenle" : "Yeni hizmet"} + + {customers.length === 0 + ? "Hizmet eklemek için önce en az bir müşteri tanımlamalısınız." + : "Müşteriye sunduğunuz hizmeti tanımlayın."} + + + +
+ {isEdit && service && } + +
+
+ + + {state.fieldErrors?.customerId && ( +

{state.fieldErrors.customerId}

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

{state.fieldErrors.name}

+ )} +
+ +
+ +