"use client"; import { useActionState, useEffect } from "react"; import { useRouter } from "next/navigation"; 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 { createInvoiceAction, updateInvoiceAction, } from "@/lib/appwrite/invoice-actions"; import { initialInvoiceState } from "@/lib/appwrite/invoice-types"; import type { Customer, InvoiceRow } from "./types"; type Props = { open: boolean; onOpenChange: (v: boolean) => void; invoice?: InvoiceRow | null; customers: Customer[]; }; function isoToDate(iso: string): string { if (!iso) return ""; return iso.slice(0, 10); } export function InvoiceFormSheet({ open, onOpenChange, invoice, customers }: Props) { const isEdit = Boolean(invoice); const action = isEdit ? updateInvoiceAction : createInvoiceAction; const [state, formAction, isPending] = useActionState(action, initialInvoiceState); const router = useRouter(); useEffect(() => { if (state.ok) { toast.success(isEdit ? "Fatura güncellendi." : "Fatura oluşturuldu."); onOpenChange(false); if (!isEdit && state.invoiceId) { router.push(`/invoices/${state.invoiceId}`); } } else if (state.error) { toast.error(state.error); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [state]); const today = new Date().toISOString().slice(0, 10); const inThirty = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000) .toISOString() .slice(0, 10); return ( {isEdit ? "Faturayı düzenle" : "Yeni fatura"} {isEdit ? "Fatura bilgilerini güncelleyin. Kalem eklemek için fatura detayına gidin." : "Faturayı oluşturun, ardından detay sayfasında kalemleri ekleyin. Numara otomatik üretilir."}
{isEdit && invoice && }
{state.fieldErrors?.customerId && (

{state.fieldErrors.customerId}

)}

“Ödendi” seçildiğinde finans modülüne otomatik gelir kaydı düşer. Durum geri alınırsa kayıt silinir.