feat(campaigns): let owners set campaign status from the detail page
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"
This commit is contained in:
parent
19112bcde1
commit
6f3d1a4147
8 changed files with 172 additions and 10 deletions
|
|
@ -17,6 +17,8 @@ import {
|
|||
import { Item, ItemContent, ItemDescription, ItemHeader, ItemTitle } from "@/components/ui/item";
|
||||
import { MissionCard } from "@/components/frontend/intelligence/MissionCard";
|
||||
import { statusColors as missionStatusColors } from "@/lib/intelligence/missionColors";
|
||||
import { hasPermission } from "@/utils/access-control/hasPermission";
|
||||
import { CampaignStatusEditor } from "@/components/frontend/intelligence/CampaignStatusEditor";
|
||||
|
||||
interface CampaignDetailPageProps {
|
||||
params: Promise<{ id: string }>;
|
||||
|
|
@ -25,9 +27,17 @@ interface CampaignDetailPageProps {
|
|||
const statusColors: Record<string, string> = {
|
||||
concept: "bg-muted text-muted-foreground",
|
||||
inProgress: "bg-blue-500/20 text-blue-400",
|
||||
onHold: "bg-amber-500/20 text-amber-400",
|
||||
completed: "bg-emerald-500/20 text-emerald-400",
|
||||
};
|
||||
|
||||
const statusLabels: Record<string, string> = {
|
||||
concept: "Concept",
|
||||
inProgress: "In Progress",
|
||||
onHold: "On Hold",
|
||||
completed: "Completed",
|
||||
};
|
||||
|
||||
const factionTypeColors: Record<string, string> = {
|
||||
majorFaction: "bg-blue-500/20 text-blue-400",
|
||||
minorFaction: "bg-muted text-muted-foreground",
|
||||
|
|
@ -72,6 +82,15 @@ export default async function CampaignDetailPage({ params }: CampaignDetailPageP
|
|||
notFound();
|
||||
}
|
||||
|
||||
// Who can change the status: the campaign owner or anyone holding
|
||||
// campaigns:update (mirrors the collection's requireCampaignOwnership gate).
|
||||
let canEditCampaign = await hasPermission(payload, user, "campaigns:update");
|
||||
if (!canEditCampaign && campaign.owner) {
|
||||
const ownerId =
|
||||
typeof campaign.owner === "object" ? (campaign.owner as { id: number }).id : campaign.owner;
|
||||
canEditCampaign = Number(ownerId) === Number(user?.id);
|
||||
}
|
||||
|
||||
const coverImage =
|
||||
typeof campaign.coverImage === "object" && campaign.coverImage !== null
|
||||
? (campaign.coverImage as Media)
|
||||
|
|
@ -148,9 +167,8 @@ export default async function CampaignDetailPage({ params }: CampaignDetailPageP
|
|||
<span
|
||||
className={`rounded-md border border-white/10 px-2 py-1 text-[10px] font-semibold uppercase tracking-wider backdrop-blur-sm ${statusColors[campaign.status] ?? "bg-muted text-muted-foreground"}`}
|
||||
>
|
||||
{campaign.status === "inProgress"
|
||||
? "In Progress"
|
||||
: campaign.status.charAt(0).toUpperCase() + campaign.status.slice(1)}
|
||||
{statusLabels[campaign.status] ??
|
||||
campaign.status.charAt(0).toUpperCase() + campaign.status.slice(1)}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
|
@ -238,6 +256,20 @@ export default async function CampaignDetailPage({ params }: CampaignDetailPageP
|
|||
|
||||
{/* ── Right Sidebar (1/3) ─────────────────────────── */}
|
||||
<div className="flex flex-col gap-4">
|
||||
{canEditCampaign && (
|
||||
<Item variant="outline">
|
||||
<ItemHeader>
|
||||
<ItemTitle>Campaign Status</ItemTitle>
|
||||
</ItemHeader>
|
||||
<ItemContent>
|
||||
<CampaignStatusEditor
|
||||
campaignId={campaign.id}
|
||||
initialStatus={campaign.status}
|
||||
/>
|
||||
</ItemContent>
|
||||
</Item>
|
||||
)}
|
||||
|
||||
{/* Campaign Stats */}
|
||||
<Item variant="outline">
|
||||
<ItemHeader>
|
||||
|
|
|
|||
|
|
@ -418,8 +418,10 @@ export default async function FactionDetailPage({ params }: FactionDetailPagePro
|
|||
>
|
||||
{campaign.status === "inProgress"
|
||||
? "In Progress"
|
||||
: campaign.status.charAt(0).toUpperCase() +
|
||||
campaign.status.slice(1)}
|
||||
: campaign.status === "onHold"
|
||||
? "On Hold"
|
||||
: campaign.status.charAt(0).toUpperCase() +
|
||||
campaign.status.slice(1)}
|
||||
</span>
|
||||
)}
|
||||
<ChevronRightIcon className="size-3 text-muted-foreground opacity-0 group-hover:opacity-100 transition-opacity shrink-0" />
|
||||
|
|
|
|||
|
|
@ -577,9 +577,13 @@ export default async function MissionDetailPage({ params }: MissionDetailPagePro
|
|||
<div>
|
||||
<h3 className="text-sm font-semibold">{campaign.name}</h3>
|
||||
<span
|
||||
className={`text-[10px] font-semibold uppercase tracking-wider px-1.5 py-0.5 rounded mt-1 inline-block ${campaign.status === "inProgress" ? "bg-blue-500/20 text-blue-400" : "bg-muted text-muted-foreground"}`}
|
||||
className={`text-[10px] font-semibold uppercase tracking-wider px-1.5 py-0.5 rounded mt-1 inline-block ${campaign.status === "inProgress" ? "bg-blue-500/20 text-blue-400" : campaign.status === "onHold" ? "bg-amber-500/20 text-amber-400" : "bg-muted text-muted-foreground"}`}
|
||||
>
|
||||
{campaign.status === "inProgress" ? "In Progress" : campaign.status}
|
||||
{campaign.status === "inProgress"
|
||||
? "In Progress"
|
||||
: campaign.status === "onHold"
|
||||
? "On Hold"
|
||||
: campaign.status}
|
||||
</span>
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground line-clamp-3">{campaign.summary}</p>
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ interface ActiveCampaignsProps {
|
|||
const statusColors: Record<string, string> = {
|
||||
inProgress: "bg-blue-500/20 text-blue-400",
|
||||
concept: "bg-muted text-muted-foreground",
|
||||
onHold: "bg-amber-500/20 text-amber-400",
|
||||
};
|
||||
|
||||
export function ActiveCampaigns({ campaigns }: ActiveCampaignsProps) {
|
||||
|
|
@ -66,7 +67,11 @@ export function ActiveCampaigns({ campaigns }: ActiveCampaignsProps) {
|
|||
<span
|
||||
className={`text-[10px] font-semibold uppercase tracking-wider px-1.5 py-0.5 rounded shrink-0 ${statusColors[campaign.status] ?? "bg-muted text-muted-foreground"}`}
|
||||
>
|
||||
{campaign.status === "inProgress" ? "Active" : campaign.status}
|
||||
{campaign.status === "inProgress"
|
||||
? "Active"
|
||||
: campaign.status === "onHold"
|
||||
? "On Hold"
|
||||
: campaign.status}
|
||||
</span>
|
||||
</div>
|
||||
<span className="text-xs text-muted-foreground truncate">{campaign.summary}</span>
|
||||
|
|
|
|||
|
|
@ -26,24 +26,28 @@ interface CampaignCardProps {
|
|||
const statusColors: Record<Campaign["status"], string> = {
|
||||
concept: "bg-muted text-muted-foreground",
|
||||
inProgress: "bg-blue-500/20 text-blue-400",
|
||||
onHold: "bg-amber-500/20 text-amber-400",
|
||||
completed: "bg-emerald-500/20 text-emerald-400",
|
||||
};
|
||||
|
||||
const statusLabels: Record<Campaign["status"], string> = {
|
||||
concept: "Concept",
|
||||
inProgress: "In Progress",
|
||||
onHold: "On Hold",
|
||||
completed: "Completed",
|
||||
};
|
||||
|
||||
const officialStatusLabels: Record<Campaign["status"], string> = {
|
||||
concept: "Planning",
|
||||
inProgress: "Active Deployment",
|
||||
onHold: "On Hold",
|
||||
completed: "Concluded",
|
||||
};
|
||||
|
||||
const statusTextColors: Record<Campaign["status"], string> = {
|
||||
concept: "text-muted-foreground",
|
||||
inProgress: "text-emerald-400",
|
||||
onHold: "text-amber-400",
|
||||
completed: "text-blue-400",
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,64 @@
|
|||
"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>
|
||||
);
|
||||
}
|
||||
|
|
@ -17,7 +17,7 @@ export function CreateCampaignDialog() {
|
|||
const [formData, setFormData] = useState({
|
||||
name: "",
|
||||
summary: "An Arma 3 campaign.",
|
||||
status: "concept" as "concept" | "inProgress" | "completed",
|
||||
status: "concept" as "concept" | "inProgress" | "onHold" | "completed",
|
||||
description: "",
|
||||
startDate: "",
|
||||
endDate: "",
|
||||
|
|
@ -119,6 +119,7 @@ export function CreateCampaignDialog() {
|
|||
>
|
||||
<option value="concept">Concept</option>
|
||||
<option value="inProgress">In Progress</option>
|
||||
<option value="onHold">On Hold</option>
|
||||
<option value="completed">Completed</option>
|
||||
</select>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -3,11 +3,12 @@
|
|||
import config from "@payload-config";
|
||||
import { getPayload } from "payload";
|
||||
import { isPayloadUser } from "@/utils/access-control/isPayloadUser";
|
||||
import { hasPermission } from "@/utils/access-control/hasPermission";
|
||||
|
||||
interface CreateCampaignData {
|
||||
name: string;
|
||||
summary: string;
|
||||
status: "concept" | "inProgress" | "completed";
|
||||
status: "concept" | "inProgress" | "onHold" | "completed";
|
||||
startDate?: string;
|
||||
endDate?: string;
|
||||
description?: string;
|
||||
|
|
@ -76,6 +77,55 @@ export async function createCampaign(data: CreateCampaignData) {
|
|||
}
|
||||
}
|
||||
|
||||
export async function updateCampaignStatus(
|
||||
campaignId: number,
|
||||
status: "concept" | "inProgress" | "onHold" | "completed",
|
||||
) {
|
||||
try {
|
||||
const { payload, user } = await authenticate();
|
||||
|
||||
// Gate: campaign owner OR campaigns:update permission (mirrors the
|
||||
// requireCampaignOwnership access on the campaigns collection).
|
||||
const canManage = await hasPermission(payload, user, "campaigns:update");
|
||||
if (!canManage) {
|
||||
const campaign = await payload.findByID({
|
||||
collection: "campaigns",
|
||||
id: campaignId,
|
||||
depth: 0,
|
||||
overrideAccess: true,
|
||||
select: { owner: true },
|
||||
});
|
||||
const ownerId =
|
||||
campaign && campaign.owner && typeof campaign.owner === "object"
|
||||
? campaign.owner.id
|
||||
: campaign?.owner;
|
||||
if (ownerId == null || Number(ownerId) !== Number(user.id)) {
|
||||
return {
|
||||
success: false,
|
||||
error: "You don't have permission to change this campaign's status.",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const campaign = await payload.update({
|
||||
collection: "campaigns",
|
||||
id: campaignId,
|
||||
data: { status },
|
||||
depth: 1,
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: campaign,
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : "Failed to update campaign status",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function uploadUgcImage(
|
||||
formData: FormData,
|
||||
): Promise<{ success: boolean; error?: string; data?: { id: number; url: string; filename: string } }> {
|
||||
|
|
|
|||
Loading…
Reference in a new issue