146 lines
4.4 KiB
TypeScript
146 lines
4.4 KiB
TypeScript
"use client";
|
|
|
|
import { useRouter } from "next/navigation";
|
|
import { CheckCircle2, MessageSquarePlus, PlayCircle } from "lucide-react";
|
|
import { FormEvent, useState } from "react";
|
|
|
|
import type { ApiEnvelope, TaskDetail, TaskStatus } from "@/lib/types";
|
|
|
|
type TaskActionPanelProps = {
|
|
taskId: string;
|
|
initialStatus: TaskStatus;
|
|
};
|
|
|
|
type TaskAction = "start" | "complete" | "comment";
|
|
|
|
function normalizeStatus(value: unknown): TaskStatus | null {
|
|
switch (value) {
|
|
case "PENDING":
|
|
case "pending":
|
|
return "pending";
|
|
case "IN_PROGRESS":
|
|
case "in_progress":
|
|
return "in_progress";
|
|
case "COMPLETED":
|
|
case "completed":
|
|
return "completed";
|
|
case "CANCELLED":
|
|
case "cancelled":
|
|
return "cancelled";
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function readMessage(response: Response) {
|
|
const payload = (await response.json().catch(() => null)) as Partial<ApiEnvelope<unknown>> | null;
|
|
return payload?.message || "操作失败";
|
|
}
|
|
|
|
export function TaskActionPanel({ taskId, initialStatus }: TaskActionPanelProps) {
|
|
const router = useRouter();
|
|
const [status, setStatus] = useState(initialStatus);
|
|
const [pendingAction, setPendingAction] = useState<TaskAction | null>(null);
|
|
const [message, setMessage] = useState("");
|
|
const [error, setError] = useState("");
|
|
|
|
async function runAction(action: Exclude<TaskAction, "comment">) {
|
|
setError("");
|
|
setMessage("");
|
|
setPendingAction(action);
|
|
|
|
const response = await fetch(`/api/mobile/tasks/${encodeURIComponent(taskId)}/${action}`, { method: "POST" });
|
|
|
|
if (!response.ok) {
|
|
setError(await readMessage(response));
|
|
setPendingAction(null);
|
|
return;
|
|
}
|
|
|
|
const payload = (await response.json().catch(() => null)) as ApiEnvelope<TaskDetail> | null;
|
|
const nextStatus = normalizeStatus(payload?.data?.status);
|
|
if (nextStatus) setStatus(nextStatus);
|
|
|
|
setMessage(action === "start" ? "任务已开始处理" : "任务已完成");
|
|
setPendingAction(null);
|
|
router.refresh();
|
|
}
|
|
|
|
async function submitComment(event: FormEvent<HTMLFormElement>) {
|
|
event.preventDefault();
|
|
setError("");
|
|
setMessage("");
|
|
setPendingAction("comment");
|
|
|
|
const form = event.currentTarget;
|
|
const formData = new FormData(form);
|
|
const comment = String(formData.get("comment") || "").trim();
|
|
|
|
if (!comment) {
|
|
setError("请填写备注内容");
|
|
setPendingAction(null);
|
|
return;
|
|
}
|
|
|
|
const response = await fetch(`/api/mobile/tasks/${encodeURIComponent(taskId)}/comment`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ comment })
|
|
});
|
|
|
|
if (!response.ok) {
|
|
setError(await readMessage(response));
|
|
setPendingAction(null);
|
|
return;
|
|
}
|
|
|
|
form.reset();
|
|
setMessage("备注已追加");
|
|
setPendingAction(null);
|
|
router.refresh();
|
|
}
|
|
|
|
const canStart = status === "pending";
|
|
const canComplete = status === "pending" || status === "in_progress";
|
|
|
|
return (
|
|
<section className="section-block">
|
|
<div className="section-title">
|
|
<h2>处理任务</h2>
|
|
</div>
|
|
<div className="button-row">
|
|
<button
|
|
className="secondary-button"
|
|
disabled={!canStart || pendingAction !== null}
|
|
onClick={() => void runAction("start")}
|
|
type="button"
|
|
>
|
|
<PlayCircle aria-hidden size={18} />
|
|
{pendingAction === "start" ? "开始中..." : "开始处理"}
|
|
</button>
|
|
<button
|
|
className="primary-button"
|
|
disabled={!canComplete || pendingAction !== null}
|
|
onClick={() => void runAction("complete")}
|
|
type="button"
|
|
>
|
|
<CheckCircle2 aria-hidden size={18} />
|
|
{pendingAction === "complete" ? "完成中..." : "完成任务"}
|
|
</button>
|
|
</div>
|
|
<form className="form-stack" onSubmit={submitComment}>
|
|
<label>
|
|
<span>处理备注</span>
|
|
<textarea className="text-area" maxLength={1000} name="comment" placeholder="填写交接、处理进展或完成说明" />
|
|
</label>
|
|
<button className="secondary-button" disabled={pendingAction !== null} type="submit">
|
|
<MessageSquarePlus aria-hidden size={18} />
|
|
{pendingAction === "comment" ? "提交中..." : "追加备注"}
|
|
</button>
|
|
</form>
|
|
{error ? <p className="form-error">{error}</p> : null}
|
|
{message ? <p className="form-success">{message}</p> : null}
|
|
</section>
|
|
);
|
|
}
|