"use client"; import { useState } from "react"; import { AdminDialog } from "@/components/ui/admin-dialog"; interface Props { open: boolean; /** Saves the PO as a draft. Resolves `{ ok: true }` on success. */ onSaveDraft: () => Promise<{ ok: boolean; error?: string }>; /** Leave the page without saving. */ onDiscard: () => void; /** Cancel and stay on the page. */ onStay: () => void; saveLabel?: string; } export function UnsavedChangesDialog({ open, onSaveDraft, onDiscard, onStay, saveLabel = "Save as Draft", }: Props) { const [saving, setSaving] = useState(false); const [error, setError] = useState(""); async function handleSave() { setSaving(true); setError(""); const result = await onSaveDraft(); setSaving(false); // On success the form navigates away; on failure keep the prompt open so // the user can fix the problem (or discard). if (!result.ok) setError(result.error ?? "Could not save draft."); } function handleStay() { if (saving) return; setError(""); onStay(); } return (

You have unsaved changes to this purchase order. Would you like to save it as a draft before leaving?

{error && (

{error}

)}
); }