45 lines
2.1 KiB
TypeScript
45 lines
2.1 KiB
TypeScript
import { useState } from "react";
|
|
import { Button } from "../ui/Button";
|
|
import { Card, CardBody, CardHeader } from "../ui/Card";
|
|
import { Field, Textarea } from "../ui/Field";
|
|
import { maxLength, minLength, required } from "../../lib/validation";
|
|
import type { Dcr, Role } from "../../types";
|
|
|
|
export type ReviewAction = "return" | "approve";
|
|
|
|
/** Decision box for the manager whose turn it is. Renders nothing for anyone else. */
|
|
export function ReviewPanel({ dcr, role, busy, onDecide }: {
|
|
dcr: Dcr; role: Role; busy: boolean; onDecide: (action: ReviewAction, comment: string) => Promise<boolean>;
|
|
}) {
|
|
const [comment, setComment] = useState("");
|
|
const [problem, setProblem] = useState<string | null>(null);
|
|
// The Area Manager is the final approver: approve and lock, or return for correction.
|
|
const canDecide = role === "AM" && dcr.status === "PENDING_AM";
|
|
if (!canDecide) return null;
|
|
|
|
const check = (action: ReviewAction, text: string): string | null =>
|
|
maxLength(2000, "Comment")(text)
|
|
?? (action === "return" ? required("A reason")(text) ?? minLength(10, "The reason")(text) : null);
|
|
|
|
const decide = async (action: ReviewAction) => {
|
|
const message = check(action, comment);
|
|
setProblem(message);
|
|
if (message) return;
|
|
if (await onDecide(action, comment.trim())) setComment("");
|
|
};
|
|
return (
|
|
<Card>
|
|
<CardHeader title="Review decision" description="Your note is saved to the approval history." />
|
|
<CardBody>
|
|
<Field label="Comment" error={problem} hint="Required (10+ characters) when returning a report.">
|
|
<Textarea value={comment} onChange={(e) => { setComment(e.target.value); if (problem) setProblem(null); }} rows={3} placeholder="Add a clear review note" />
|
|
</Field>
|
|
<div className="button-row">
|
|
<Button variant="danger" disabled={busy} onClick={() => void decide("return")}>Return for correction</Button>
|
|
<Button variant="primary" icon="check" loading={busy} onClick={() => void decide("approve")}>Approve and lock</Button>
|
|
</div>
|
|
</CardBody>
|
|
</Card>
|
|
);
|
|
}
|