"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { processPayment, markPaid } from "./actions"; import type { POStatus } from "@prisma/client"; export function PaymentActions({ poId, poStatus }: { poId: string; poStatus: POStatus }) { const router = useRouter(); const [ref, setRef] = useState(""); const [pending, setPending] = useState(false); const [error, setError] = useState(""); async function handleProcessPayment() { setPending(true); setError(""); const result = await processPayment({ poId }); if ("error" in result) { setError(result.error); setPending(false); } else { router.refresh(); } } async function handleMarkPaid(e: React.FormEvent) { e.preventDefault(); if (!ref.trim()) { setError("Payment reference is required."); return; } setPending(true); setError(""); const result = await markPaid({ poId, paymentRef: ref }); if ("error" in result) { setError(result.error); setPending(false); } else { router.refresh(); } } if (poStatus === "MGR_APPROVED") { return (
{error && {error}}
); } if (poStatus === "SENT_FOR_PAYMENT") { return (
setRef(e.target.value)} className="flex-1 rounded-lg border border-neutral-300 px-3 py-2 text-sm focus:border-primary-500 focus:outline-none focus:ring-2 focus:ring-primary-500/20" /> {error && {error}}
); } return null; }