"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { toast } from "sonner"; import { updateCampaignStatus } from "./create"; const STATUS_OPTIONS: { value: "concept" | "inProgress" | "onHold" | "completed"; label: string }[] = [ { value: "concept", label: "Concept" }, { value: "inProgress", label: "In Progress" }, { value: "onHold", label: "On Hold" }, { value: "completed", label: "Completed" }, ]; export function CampaignStatusEditor({ campaignId, initialStatus, }: { campaignId: number; initialStatus: string; }) { const router = useRouter(); const [status, setStatus] = useState(initialStatus); const [committedStatus, setCommittedStatus] = useState(initialStatus); const [saving, setSaving] = useState(false); const handleChange = async (newValue: string) => { // Optimistic UI: reflect the pick immediately, then confirm with the server. setStatus(newValue); setSaving(true); const result = await updateCampaignStatus( campaignId, newValue as "concept" | "inProgress" | "onHold" | "completed", ); setSaving(false); if (result.success) { setCommittedStatus(newValue); toast.success("Campaign status updated."); router.refresh(); return; } toast.error(result.error ?? "Failed to update campaign status."); // Revert to the last known-good status on failure. setStatus(committedStatus); }; return ( ); }