feat(campaigns): submit/approve/reject UI on campaign detail page
This commit is contained in:
@@ -19,12 +19,14 @@ import {
|
||||
} from "lucide-react";
|
||||
import {
|
||||
getCampaign,
|
||||
activateCampaign,
|
||||
completeCampaign,
|
||||
deleteCampaign,
|
||||
removeTestFromCampaign,
|
||||
scheduleCampaign,
|
||||
getCampaignHistory,
|
||||
submitCampaignForApproval,
|
||||
approveCampaign,
|
||||
rejectCampaign,
|
||||
type Campaign,
|
||||
type CampaignHistoryEntry,
|
||||
} from "../api/campaigns";
|
||||
@@ -68,8 +70,10 @@ export default function CampaignDetailPage() {
|
||||
const [showAddTestModal, setShowAddTestModal] = useState(false);
|
||||
// 0 = hidden, 1 = first confirmation, 2 = ask about tests
|
||||
const [deleteStep, setDeleteStep] = useState<0 | 1 | 2>(0);
|
||||
// Start-date confirmation modal — shown when campaign has a future start_date
|
||||
const [startDateWarning, setStartDateWarning] = useState<string | null>(null);
|
||||
const [approveModalOpen, setApproveModalOpen] = useState(false);
|
||||
const [approveStartDate, setApproveStartDate] = useState("");
|
||||
const [rejectModalOpen, setRejectModalOpen] = useState(false);
|
||||
const [rejectReason, setRejectReason] = useState("");
|
||||
|
||||
const showToast = (message: string, type: "success" | "error") => {
|
||||
setToast({ message, type });
|
||||
@@ -79,6 +83,7 @@ export default function CampaignDetailPage() {
|
||||
const role = user?.role ?? "";
|
||||
const canManage = role === "admin" || role === "red_lead" || role === "blue_lead";
|
||||
const canComplete = role === "admin" || role === "red_lead";
|
||||
const isManager = role === "admin" || role === "manager";
|
||||
|
||||
const {
|
||||
data: campaign,
|
||||
@@ -90,33 +95,35 @@ export default function CampaignDetailPage() {
|
||||
enabled: !!campaignId,
|
||||
});
|
||||
|
||||
const activateMutation = useMutation<Campaign, unknown, boolean>({
|
||||
mutationFn: (force: boolean) => activateCampaign(campaignId!, force ? { force: true } : undefined),
|
||||
const submitMutation = useMutation({
|
||||
mutationFn: () => submitCampaignForApproval(campaignId!),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["campaign", campaignId] });
|
||||
setStartDateWarning(null);
|
||||
showToast("Campaign activated", "success");
|
||||
showToast("Campaign submitted for approval", "success");
|
||||
},
|
||||
onError: (err: unknown) => {
|
||||
// The Axios interceptor (client.ts) transforms errors into enhanced Error objects
|
||||
// with .status (HTTP status), .detail (raw FastAPI detail), and .message (readable string)
|
||||
type EnhancedError = Error & {
|
||||
status?: number;
|
||||
detail?: { code?: string; message?: string } | string;
|
||||
};
|
||||
const e = err as EnhancedError;
|
||||
onError: (err: Error) => showToast(err.message, "error"),
|
||||
});
|
||||
|
||||
if (e.status === 409) {
|
||||
// Future start_date — show confirmation modal using the message from detail
|
||||
const warningMsg =
|
||||
typeof e.detail === "object" && e.detail?.message
|
||||
? e.detail.message
|
||||
: e.message;
|
||||
setStartDateWarning(warningMsg);
|
||||
} else {
|
||||
showToast(e.message || "Failed to activate campaign", "error");
|
||||
}
|
||||
const approveMutation = useMutation({
|
||||
mutationFn: () => approveCampaign(campaignId!, approveStartDate),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["campaign", campaignId] });
|
||||
setApproveModalOpen(false);
|
||||
setApproveStartDate("");
|
||||
showToast("Campaign approved and activated", "success");
|
||||
},
|
||||
onError: (err: Error) => showToast(err.message, "error"),
|
||||
});
|
||||
|
||||
const rejectMutation = useMutation({
|
||||
mutationFn: () => rejectCampaign(campaignId!, rejectReason),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["campaign", campaignId] });
|
||||
setRejectModalOpen(false);
|
||||
setRejectReason("");
|
||||
showToast("Campaign rejected", "success");
|
||||
},
|
||||
onError: (err: Error) => showToast(err.message, "error"),
|
||||
});
|
||||
|
||||
const completeMutation = useMutation({
|
||||
@@ -262,6 +269,11 @@ export default function CampaignDetailPage() {
|
||||
{campaign.description && (
|
||||
<MarkdownText content={campaign.description} className="mt-1 text-sm text-gray-400" />
|
||||
)}
|
||||
{campaign.status === "draft" && campaign.rejection_reason && (
|
||||
<div className="mt-2 rounded-lg border border-amber-500/30 bg-amber-900/20 px-3 py-2 text-xs text-amber-400">
|
||||
Rejected by manager: {campaign.rejection_reason}
|
||||
</div>
|
||||
)}
|
||||
<div className="mt-2 flex flex-wrap items-center gap-3 text-xs text-gray-500">
|
||||
{campaign.threat_actor_name && (
|
||||
<button
|
||||
@@ -319,18 +331,35 @@ export default function CampaignDetailPage() {
|
||||
)}
|
||||
{canManage && campaign.status === "draft" && (
|
||||
<button
|
||||
onClick={() => activateMutation.mutate(false)}
|
||||
disabled={activateMutation.isPending}
|
||||
onClick={() => submitMutation.mutate()}
|
||||
disabled={submitMutation.isPending}
|
||||
className="flex items-center gap-1.5 rounded-lg bg-cyan-600 px-4 py-2 text-sm font-medium text-white hover:bg-cyan-500 disabled:opacity-50 transition-colors"
|
||||
>
|
||||
{activateMutation.isPending ? (
|
||||
{submitMutation.isPending ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<Play className="h-4 w-4" />
|
||||
)}
|
||||
Activate
|
||||
Submit for Approval
|
||||
</button>
|
||||
)}
|
||||
{isManager && campaign.status === "pending_approval" && (
|
||||
<>
|
||||
<button
|
||||
onClick={() => setRejectModalOpen(true)}
|
||||
className="flex items-center gap-1.5 rounded-lg border border-red-500/30 bg-red-900/20 px-4 py-2 text-sm font-medium text-red-400 hover:bg-red-900/40 transition-colors"
|
||||
>
|
||||
Reject
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setApproveModalOpen(true)}
|
||||
className="flex items-center gap-1.5 rounded-lg bg-green-600 px-4 py-2 text-sm font-medium text-white hover:bg-green-500 transition-colors"
|
||||
>
|
||||
<CheckCircle className="h-4 w-4" />
|
||||
Approve
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
{canComplete && campaign.status === "active" && (
|
||||
<button
|
||||
onClick={() => completeMutation.mutate()}
|
||||
@@ -688,31 +717,69 @@ export default function CampaignDetailPage() {
|
||||
onSuccess={() => showToast("Test added to campaign", "success")}
|
||||
/>
|
||||
|
||||
{/* Start-date confirmation modal */}
|
||||
{startDateWarning && (
|
||||
{/* Approve modal */}
|
||||
{approveModalOpen && (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/70 backdrop-blur-sm">
|
||||
<div className="mx-4 w-full max-w-md rounded-xl border border-amber-500/30 bg-gray-900 p-6 shadow-2xl">
|
||||
<div className="mb-4 flex items-center gap-3">
|
||||
<div className="rounded-lg bg-amber-500/10 p-2">
|
||||
<Clock className="h-5 w-5 text-amber-400" />
|
||||
</div>
|
||||
<h3 className="text-lg font-semibold text-white">Campaign not started yet</h3>
|
||||
</div>
|
||||
<p className="mb-6 text-sm text-gray-300 leading-relaxed">{startDateWarning}</p>
|
||||
<div className="flex justify-end gap-3">
|
||||
<div className="mx-4 w-full max-w-md rounded-xl border border-green-500/30 bg-gray-900 p-6 shadow-2xl">
|
||||
<h3 className="mb-4 text-lg font-semibold text-white">Approve Campaign</h3>
|
||||
<label className="mb-1.5 block text-sm font-medium text-gray-300">
|
||||
Start date <span className="text-red-400">*</span>
|
||||
</label>
|
||||
<input
|
||||
type="datetime-local"
|
||||
value={approveStartDate}
|
||||
onChange={(e) => setApproveStartDate(e.target.value)}
|
||||
className="w-full rounded-lg border border-gray-700 bg-gray-800 px-3 py-2 text-sm text-gray-200 focus:border-cyan-500 focus:outline-none [color-scheme:dark]"
|
||||
/>
|
||||
<div className="mt-6 flex justify-end gap-3">
|
||||
<button
|
||||
onClick={() => setStartDateWarning(null)}
|
||||
onClick={() => setApproveModalOpen(false)}
|
||||
className="rounded-lg border border-gray-700 px-4 py-2 text-sm text-gray-400 hover:bg-gray-800 transition-colors"
|
||||
>
|
||||
Keep scheduled
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
onClick={() => activateMutation.mutate(true)}
|
||||
disabled={activateMutation.isPending}
|
||||
className="flex items-center gap-1.5 rounded-lg bg-amber-600 px-4 py-2 text-sm font-medium text-white hover:bg-amber-500 disabled:opacity-50 transition-colors"
|
||||
onClick={() => approveMutation.mutate()}
|
||||
disabled={!approveStartDate || approveMutation.isPending}
|
||||
className="flex items-center gap-1.5 rounded-lg bg-green-600 px-4 py-2 text-sm font-medium text-white hover:bg-green-500 disabled:opacity-50 transition-colors"
|
||||
>
|
||||
{activateMutation.isPending && <Loader2 className="h-4 w-4 animate-spin" />}
|
||||
Activate now anyway
|
||||
{approveMutation.isPending && <Loader2 className="h-4 w-4 animate-spin" />}
|
||||
Approve & Activate
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Reject modal */}
|
||||
{rejectModalOpen && (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/70 backdrop-blur-sm">
|
||||
<div className="mx-4 w-full max-w-md rounded-xl border border-red-500/30 bg-gray-900 p-6 shadow-2xl">
|
||||
<h3 className="mb-4 text-lg font-semibold text-white">Reject Campaign</h3>
|
||||
<label className="mb-1.5 block text-sm font-medium text-gray-300">
|
||||
Reason <span className="text-red-400">*</span>
|
||||
</label>
|
||||
<textarea
|
||||
value={rejectReason}
|
||||
onChange={(e) => setRejectReason(e.target.value)}
|
||||
rows={3}
|
||||
placeholder="Explain what needs to change before this can be approved…"
|
||||
className="w-full rounded-lg border border-gray-700 bg-gray-800 px-3 py-2 text-sm text-gray-200 placeholder-gray-500 focus:border-cyan-500 focus:outline-none"
|
||||
/>
|
||||
<div className="mt-6 flex justify-end gap-3">
|
||||
<button
|
||||
onClick={() => setRejectModalOpen(false)}
|
||||
className="rounded-lg border border-gray-700 px-4 py-2 text-sm text-gray-400 hover:bg-gray-800 transition-colors"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
onClick={() => rejectMutation.mutate()}
|
||||
disabled={!rejectReason.trim() || rejectMutation.isPending}
|
||||
className="flex items-center gap-1.5 rounded-lg bg-red-600 px-4 py-2 text-sm font-medium text-white hover:bg-red-500 disabled:opacity-50 transition-colors"
|
||||
>
|
||||
{rejectMutation.isPending && <Loader2 className="h-4 w-4 animate-spin" />}
|
||||
Reject
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user