Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
244 lines
8.1 KiB
TypeScript
244 lines
8.1 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useId, useState, type FormEvent } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import { toast } from "sonner";
|
|
|
|
import { reviewRibbonSubmission } from "@/app/(frontend)/awards/design/actions";
|
|
import { Button } from "@/components/ui/button";
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from "@/components/ui/dialog";
|
|
import { Checkbox } from "@/components/ui/checkbox";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Label } from "@/components/ui/label";
|
|
import {
|
|
Select,
|
|
SelectContent,
|
|
SelectItem,
|
|
SelectTrigger,
|
|
SelectValue,
|
|
} from "@/components/ui/select";
|
|
import { Textarea } from "@/components/ui/textarea";
|
|
import type { RibbonSubmission } from "@/payload-types";
|
|
|
|
type GrantorType = "division" | "squad" | "team" | "special";
|
|
type RecipientScope = "same_command" | "anyone";
|
|
|
|
const GRANTOR_OPTIONS: readonly { readonly value: GrantorType; readonly label: string }[] = [
|
|
{ value: "division", label: "Division Leader" },
|
|
{ value: "squad", label: "Squad Leader" },
|
|
{ value: "team", label: "Team Leader" },
|
|
{ value: "special", label: "Special Assignment Leader" },
|
|
];
|
|
|
|
interface ReviewDialogProps {
|
|
readonly submission: RibbonSubmission;
|
|
readonly open: boolean;
|
|
readonly onOpenChange: (open: boolean) => void;
|
|
}
|
|
|
|
export function ApproveRibbonDialog({ submission, open, onOpenChange }: ReviewDialogProps) {
|
|
const router = useRouter();
|
|
const fieldId = useId();
|
|
const [awardXp, setAwardXp] = useState("1");
|
|
const [grantorTypes, setGrantorTypes] = useState<GrantorType[]>(["division"]);
|
|
const [recipientScope, setRecipientScope] = useState<RecipientScope>("same_command");
|
|
const [maxGrantsPerGrantor, setMaxGrantsPerGrantor] = useState("");
|
|
const [finalName, setFinalName] = useState(submission.name);
|
|
const [reviewNote, setReviewNote] = useState("");
|
|
const [pending, setPending] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (!open) return;
|
|
setAwardXp("1");
|
|
setGrantorTypes(["division"]);
|
|
setRecipientScope("same_command");
|
|
setMaxGrantsPerGrantor("");
|
|
setFinalName(submission.name);
|
|
setReviewNote("");
|
|
setPending(false);
|
|
}, [open, submission.name]);
|
|
|
|
async function handleSubmit(event: FormEvent<HTMLFormElement>) {
|
|
event.preventDefault();
|
|
|
|
const xp = Number(awardXp);
|
|
const trimmedName = finalName.trim();
|
|
const trimmedNote = reviewNote.trim();
|
|
const maxGrantsValue = maxGrantsPerGrantor.trim() ? Number(maxGrantsPerGrantor) : null;
|
|
|
|
if (!Number.isInteger(xp) || xp < 1) {
|
|
toast.error("Experience Points must be at least 1.");
|
|
return;
|
|
}
|
|
if (grantorTypes.length === 0) {
|
|
toast.error("Choose at least one grantor type.");
|
|
return;
|
|
}
|
|
if (maxGrantsValue !== null && (!Number.isInteger(maxGrantsValue) || maxGrantsValue < 1)) {
|
|
toast.error("Max grants per grantor must be a whole number of at least 1.");
|
|
return;
|
|
}
|
|
if (!trimmedName) {
|
|
toast.error("Enter an award name.");
|
|
return;
|
|
}
|
|
|
|
setPending(true);
|
|
try {
|
|
const result = await reviewRibbonSubmission(submission.id, "approved", {
|
|
awardXp: xp,
|
|
grantorTypes,
|
|
recipientScope,
|
|
maxGrantsPerGrantor: maxGrantsValue || null,
|
|
finalName: trimmedName !== submission.name ? trimmedName : undefined,
|
|
reviewNote: trimmedNote || undefined,
|
|
});
|
|
|
|
if (!result.success) {
|
|
toast.error(result.error ?? "Could not approve this ribbon.");
|
|
return;
|
|
}
|
|
|
|
toast.success("Ribbon approved and award created");
|
|
onOpenChange(false);
|
|
router.refresh();
|
|
} catch (error) {
|
|
toast.error(error instanceof Error ? error.message : "Could not approve this ribbon.");
|
|
} finally {
|
|
setPending(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Dialog
|
|
open={open}
|
|
onOpenChange={(nextOpen) => {
|
|
if (!pending) onOpenChange(nextOpen);
|
|
}}
|
|
>
|
|
<DialogContent className="max-h-[90vh] overflow-y-auto sm:max-w-lg">
|
|
<DialogHeader>
|
|
<DialogTitle>Approve ribbon</DialogTitle>
|
|
<DialogDescription>
|
|
Set the award rules before creating this ribbon as a grantable award.
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<form className="flex flex-col gap-4" onSubmit={handleSubmit}>
|
|
<div className="flex flex-col gap-1.5">
|
|
<Label htmlFor={`${fieldId}-xp`}>Experience Points</Label>
|
|
<Input
|
|
id={`${fieldId}-xp`}
|
|
type="number"
|
|
min={1}
|
|
step={1}
|
|
required
|
|
value={awardXp}
|
|
onChange={(event) => setAwardXp(event.target.value)}
|
|
disabled={pending}
|
|
/>
|
|
</div>
|
|
|
|
<fieldset className="flex flex-col gap-2">
|
|
<legend className="text-sm font-medium">Grantor types</legend>
|
|
{GRANTOR_OPTIONS.map((option) => {
|
|
const optionId = `${fieldId}-${option.value}`;
|
|
return (
|
|
<div key={option.value} className="flex items-center gap-2">
|
|
<Checkbox
|
|
id={optionId}
|
|
checked={grantorTypes.includes(option.value)}
|
|
onCheckedChange={(checked) => {
|
|
setGrantorTypes((current) =>
|
|
checked === true
|
|
? [...current, option.value]
|
|
: current.filter((type) => type !== option.value),
|
|
);
|
|
}}
|
|
disabled={pending}
|
|
/>
|
|
<Label htmlFor={optionId}>{option.label}</Label>
|
|
</div>
|
|
);
|
|
})}
|
|
</fieldset>
|
|
|
|
<div className="flex flex-col gap-1.5">
|
|
<Label htmlFor={`${fieldId}-scope`}>Recipient scope</Label>
|
|
<Select
|
|
value={recipientScope}
|
|
onValueChange={(value) => {
|
|
if (value === "same_command" || value === "anyone") setRecipientScope(value);
|
|
}}
|
|
disabled={pending}
|
|
>
|
|
<SelectTrigger id={`${fieldId}-scope`}>
|
|
<SelectValue />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="same_command">Own command tree</SelectItem>
|
|
<SelectItem value="anyone">Anyone (unit-wide)</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
|
|
<div className="flex flex-col gap-1.5">
|
|
<Label htmlFor={`${fieldId}-limit`}>Max grants per grantor (empty = unlimited)</Label>
|
|
<Input
|
|
id={`${fieldId}-limit`}
|
|
type="number"
|
|
min={1}
|
|
step={1}
|
|
value={maxGrantsPerGrantor}
|
|
onChange={(event) => setMaxGrantsPerGrantor(event.target.value)}
|
|
disabled={pending}
|
|
/>
|
|
</div>
|
|
|
|
<div className="flex flex-col gap-1.5">
|
|
<Label htmlFor={`${fieldId}-name`}>Award name</Label>
|
|
<Input
|
|
id={`${fieldId}-name`}
|
|
value={finalName}
|
|
maxLength={60}
|
|
required
|
|
onChange={(event) => setFinalName(event.target.value)}
|
|
disabled={pending}
|
|
/>
|
|
</div>
|
|
|
|
<div className="flex flex-col gap-1.5">
|
|
<Label htmlFor={`${fieldId}-note`}>Review note</Label>
|
|
<Textarea
|
|
id={`${fieldId}-note`}
|
|
value={reviewNote}
|
|
onChange={(event) => setReviewNote(event.target.value)}
|
|
rows={3}
|
|
disabled={pending}
|
|
/>
|
|
</div>
|
|
|
|
<DialogFooter>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() => onOpenChange(false)}
|
|
disabled={pending}
|
|
>
|
|
Cancel
|
|
</Button>
|
|
<Button type="submit" disabled={pending || grantorTypes.length === 0}>
|
|
{pending ? "Approving..." : "Approve ribbon"}
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|