TTMT.ManageWebGUI/src/routes/_auth/blacklists/index.tsx

165 lines
4.5 KiB
TypeScript
Raw Normal View History

2025-12-22 14:53:19 +07:00
import {
useGetBlacklist,
useGetRoomList,
useAddBlacklist,
useDeleteBlacklist,
useUpdateDeviceBlacklist,
} from "@/hooks/queries";
2025-10-20 16:46:17 +07:00
import { createFileRoute } from "@tanstack/react-router";
import type { ColumnDef } from "@tanstack/react-table";
2025-11-19 14:55:14 +07:00
import type { Blacklist } from "@/types/black-list";
import { BlackListManagerTemplate } from "@/template/table-manager-template";
import { toast } from "sonner";
2025-10-20 16:46:17 +07:00
import { useState } from "react";
2025-10-06 15:52:48 +07:00
2026-03-04 14:41:34 +07:00
export const Route = createFileRoute("/_auth/blacklists/")({
2025-10-20 16:46:17 +07:00
head: () => ({ meta: [{ title: "Danh sách các ứng dụng bị chặn" }] }),
component: BlacklistComponent,
2026-03-04 14:41:34 +07:00
loader: async ({ context }) => {
context.breadcrumbs = [
{ title: "Quản lý danh sách chặn", path: "/_auth/blacklists/" },
];
},
2025-10-20 16:46:17 +07:00
});
function BlacklistComponent() {
2025-11-19 14:55:14 +07:00
const [selectedRows, setSelectedRows] = useState<Set<number>>(new Set());
// Lấy danh sách blacklist
2025-12-22 14:53:19 +07:00
const { data, isLoading } = useGetBlacklist();
2025-10-20 16:46:17 +07:00
2025-11-19 14:55:14 +07:00
// Lấy danh sách phòng
2025-12-22 14:53:19 +07:00
const { data: roomData = [] } = useGetRoomList();
2025-11-19 14:55:14 +07:00
2025-10-20 16:46:17 +07:00
const blacklist: Blacklist[] = Array.isArray(data)
? (data as Blacklist[])
: [];
2025-11-19 14:55:14 +07:00
const columns: ColumnDef<Blacklist>[] = [
2025-10-20 16:46:17 +07:00
{
2025-11-19 14:55:14 +07:00
accessorKey: "id",
header: "STT",
cell: (info) => info.getValue(),
2025-10-20 16:46:17 +07:00
},
{
2025-11-19 14:55:14 +07:00
accessorKey: "appName",
header: "Tên ứng dụng",
cell: (info) => info.getValue(),
2025-10-20 16:46:17 +07:00
},
{
2025-11-19 14:55:14 +07:00
accessorKey: "processName",
header: "Tên tiến trình",
cell: (info) => info.getValue(),
2025-10-20 16:46:17 +07:00
},
{
2025-11-19 14:55:14 +07:00
accessorKey: "createdAt",
header: "Ngày tạo",
cell: (info) => info.getValue(),
2025-10-20 16:46:17 +07:00
},
{
2025-11-19 14:55:14 +07:00
accessorKey: "updatedAt",
header: "Ngày cập nhật",
cell: (info) => info.getValue(),
2025-10-20 16:46:17 +07:00
},
{
2025-11-19 14:55:14 +07:00
accessorKey: "createdBy",
header: "Người tạo",
cell: (info) => info.getValue(),
2025-10-20 16:46:17 +07:00
},
2025-11-19 14:55:14 +07:00
{
id: "select",
header: () => (
<input
type="checkbox"
onChange={(e) => {
2025-12-22 14:53:19 +07:00
if (e.target.checked && data) {
2025-11-19 14:55:14 +07:00
const allIds = data.map((item: { id: number }) => item.id);
setSelectedRows(new Set(allIds));
} else {
setSelectedRows(new Set());
}
}}
/>
),
cell: ({ row }) => (
<input
type="checkbox"
checked={selectedRows.has(row.original.id)}
onChange={(e) => {
const newSelected = new Set(selectedRows);
if (e.target.checked) {
newSelected.add(row.original.id);
} else {
newSelected.delete(row.original.id);
}
setSelectedRows(newSelected);
}}
/>
),
},
];
2025-12-22 14:53:19 +07:00
// API mutations
const addNewBlacklistMutation = useAddBlacklist();
const deleteBlacklistMutation = useDeleteBlacklist();
const updateDeviceMutation = useUpdateDeviceBlacklist();
2025-11-19 14:55:14 +07:00
// Thêm blacklist
2025-12-22 14:53:19 +07:00
const handleAddNewBlacklist = async (blacklistData: {
2025-11-19 14:55:14 +07:00
appName: string;
processName: string;
}) => {
try {
2025-12-22 14:53:19 +07:00
await addNewBlacklistMutation.mutateAsync(blacklistData);
toast.success("Thêm mới thành công!");
} catch (error: any) {
console.error("Add blacklist error:", error);
2025-11-19 14:55:14 +07:00
toast.error("Thêm mới thất bại!");
}
};
// Xoá blacklist
const handleDeleteBlacklist = async () => {
try {
for (const blacklistId of selectedRows) {
2025-12-22 14:53:19 +07:00
await deleteBlacklistMutation.mutateAsync(blacklistId);
2025-11-19 14:55:14 +07:00
}
2025-12-22 14:53:19 +07:00
toast.success("Xóa thành công!");
2025-11-19 14:55:14 +07:00
setSelectedRows(new Set());
2025-12-22 14:53:19 +07:00
} catch (error: any) {
console.error("Delete blacklist error:", error);
toast.error("Xóa thất bại!");
}
2025-11-19 14:55:14 +07:00
};
const handleUpdateDevice = async (target: string | string[]) => {
const targets = Array.isArray(target) ? target : [target];
try {
2025-12-22 14:53:19 +07:00
for (const roomName of targets) {
2025-11-19 14:55:14 +07:00
await updateDeviceMutation.mutateAsync({
2025-12-22 14:53:19 +07:00
roomName,
data: {},
2025-11-19 14:55:14 +07:00
});
2025-12-22 14:53:19 +07:00
toast.success(`Đã gửi cập nhật cho ${roomName}`);
2025-11-19 14:55:14 +07:00
}
2025-12-22 14:53:19 +07:00
} catch (e: any) {
console.error("Update device error:", e);
2025-11-19 14:55:14 +07:00
toast.error("Có lỗi xảy ra khi cập nhật!");
}
};
2025-10-20 16:46:17 +07:00
2025-11-19 14:55:14 +07:00
return (
<BlackListManagerTemplate<Blacklist>
title="Danh sách các ứng dụng bị chặn"
description="Quản lý các ứng dụng và tiến trình bị chặn trên thiết bị"
data={blacklist}
columns={columns}
isLoading={isLoading}
rooms={roomData}
onAdd={handleAddNewBlacklist}
2025-12-22 14:53:19 +07:00
onDelete={handleDeleteBlacklist}
2026-03-04 14:41:34 +07:00
onUpdate={handleUpdateDevice}
2025-11-19 14:55:14 +07:00
/>
);
2025-10-06 15:52:48 +07:00
}