Add a Campaign Status dropdown on the campaign detail page, gated by campaign ownership or the campaigns:update permission (mirroring the existing requireCampaignOwnership gate). Owners can set On Hold to pause a campaign they are not actively running, and flip it back; admins act through the same control. The editor optimistically updates the field, toasts on success, refreshes the view, and reverts on failure. - new CampaignStatusEditor client component (optimistic select + save) - updateCampaignStatus server action (owner OR campaigns:update) - render the editor on the detail page for authorized users - add "On Hold" to the create dialog status options - color + label the new status on cards and campaign rows (missions, factions, dashboard) so it never renders as raw "onHold"
64 lines
2 KiB
TypeScript
64 lines
2 KiB
TypeScript
"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 (
|
|
<select
|
|
value={status}
|
|
disabled={saving}
|
|
onChange={(e) => void handleChange(e.target.value)}
|
|
className="w-full rounded-md border border-border/50 bg-background px-2 py-1.5 text-sm font-medium text-primary ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:opacity-70"
|
|
>
|
|
{STATUS_OPTIONS.map((option) => (
|
|
<option key={option.value} value={option.value}>
|
|
{option.label}
|
|
</option>
|
|
))}
|
|
</select>
|
|
);
|
|
}
|