TTMT.ManageWebGUI/src/routes/_authenticated/command/index.tsx

74 lines
2.2 KiB
TypeScript
Raw Normal View History

2025-09-24 16:13:57 +07:00
import { createFileRoute } from "@tanstack/react-router";
import { FormSubmitTemplate } from "@/template/form-submit-template";
import { ShellCommandForm } from "@/components/command-form";
import { useMutationData } from "@/hooks/useMutationData";
2025-09-26 17:56:55 +07:00
import { useQueryData } from "@/hooks/useQueryData";
2025-09-24 16:13:57 +07:00
import { BASE_URL, API_ENDPOINTS } from "@/config/api";
import { toast } from "sonner";
2025-09-26 17:56:55 +07:00
import type { Room } from "@/types/room";
type SendCommandRequest = { Command: string };
type SendCommandResponse = { status: string; message: string };
2025-09-24 16:13:57 +07:00
export const Route = createFileRoute("/_authenticated/command/")({
head: () => ({ meta: [{ title: "Gửi lệnh CMD" }] }),
component: CommandPage,
});
function CommandPage() {
2025-09-26 17:56:55 +07:00
// Lấy danh sách phòng từ API
const { data: roomData } = useQueryData({
queryKey: ["rooms"],
url: BASE_URL + API_ENDPOINTS.DEVICE_COMM.GET_ROOM_LIST,
});
// Map từ object sang string[]
const rooms: string[] = Array.isArray(roomData)
? (roomData as Room[]).map((r) => r.name)
: [];
// Mutation gửi lệnh
2025-09-24 16:13:57 +07:00
const sendCommandMutation = useMutationData<
2025-09-26 17:56:55 +07:00
SendCommandRequest,
SendCommandResponse
2025-09-24 16:13:57 +07:00
>({
2025-09-26 17:56:55 +07:00
url: "", // sẽ set động theo roomName khi gọi
2025-09-24 16:13:57 +07:00
method: "POST",
onSuccess: (data) => {
2025-09-26 17:56:55 +07:00
if (data.status === "OK") {
toast.success("Gửi lệnh thành công!");
2025-09-24 16:13:57 +07:00
} else {
2025-09-26 17:56:55 +07:00
toast.error("Gửi lệnh thất bại!");
2025-09-24 16:13:57 +07:00
}
},
onError: (error) => {
console.error("Send command error:", error);
toast.error("Gửi lệnh thất bại!");
},
});
return (
<FormSubmitTemplate
title="CMD Command"
description="Gửi lệnh shell xuống thiết bị để thực thi"
isLoading={sendCommandMutation.isPending}
2025-09-26 17:56:55 +07:00
rooms={rooms}
onSubmit={(roomName, command) => {
sendCommandMutation.mutateAsync({
url: BASE_URL + API_ENDPOINTS.DEVICE_COMM.SEND_COMMAND(roomName),
data: { Command: command },
});
}}
submitLoading={sendCommandMutation.isPending}
2025-09-24 16:13:57 +07:00
>
2025-09-26 17:56:55 +07:00
{({ command, setCommand }) => (
<ShellCommandForm
command={command}
onCommandChange={setCommand}
disabled={sendCommandMutation.isPending}
/>
)}
2025-09-24 16:13:57 +07:00
</FormSubmitTemplate>
);
}