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

95 lines
2.9 KiB
TypeScript
Raw Normal View History

2025-08-11 23:21:36 +07:00
import { createFileRoute } from "@tanstack/react-router";
2025-09-10 09:59:17 +07:00
import { AppManagerTemplate } from "@/template/app-manager-template";
2025-08-11 23:21:36 +07:00
import { useQueryData } from "@/hooks/useQueryData";
import { useMutationData } from "@/hooks/useMutationData";
2025-09-10 09:59:17 +07:00
import { BASE_URL, API_ENDPOINTS } from "@/config/api";
2025-08-11 23:21:36 +07:00
import { toast } from "sonner";
2025-09-10 09:59:17 +07:00
import type { ColumnDef } from "@tanstack/react-table";
2025-08-11 23:21:36 +07:00
import { useState } from "react";
export const Route = createFileRoute("/_authenticated/apps/")({
2025-09-10 09:59:17 +07:00
head: () => ({ meta: [{ title: "Quản lý phần mềm" }] }),
2025-08-11 23:21:36 +07:00
component: AppsComponent,
});
2025-09-10 09:59:17 +07:00
type Version = {
id: number;
version: string;
fileName: string;
folderPath: string;
updatedAt?: string;
};
2025-08-11 23:21:36 +07:00
function AppsComponent() {
2025-09-10 09:59:17 +07:00
const { data, isLoading } = useQueryData({
queryKey: ["software-version"],
url: BASE_URL + API_ENDPOINTS.APP_VERSION.GET_SOFTWARE, // API lấy danh sách file MSI
2025-08-11 23:21:36 +07:00
});
2025-09-10 09:59:17 +07:00
const versionList: Version[] = Array.isArray(data)
? data
: data
? [data]
2025-08-11 23:21:36 +07:00
: [];
2025-09-10 09:59:17 +07:00
const [table, setTable] = useState<any>();
2025-08-11 23:21:36 +07:00
const uploadMutation = useMutationData<FormData>({
url: BASE_URL + API_ENDPOINTS.APP_VERSION.UPLOAD,
method: "POST",
2025-09-10 09:59:17 +07:00
onSuccess: () => toast.success("Upload thành công!"),
onError: () => toast.error("Upload thất bại!"),
2025-08-11 23:21:36 +07:00
});
2025-09-10 09:59:17 +07:00
const installMutation = useMutationData<{ msiFileIds: number[] }>({
url: BASE_URL+ API_ENDPOINTS.DEVICE_COMM.DOWNLOAD_MSI,
onSuccess: () => toast.success("Đã gửi yêu cầu cài đặt MSI!"),
2025-08-11 23:21:36 +07:00
onError: () => toast.error("Gửi yêu cầu thất bại!"),
});
2025-09-10 09:59:17 +07:00
const columns: ColumnDef<Version>[] = [
{
id: "select",
header: () => <span>Thêm vào danh sách yêu cầu</span>,
cell: ({ row }) => (
<input
type="checkbox"
checked={row.getIsSelected?.() ?? false}
onChange={row.getToggleSelectedHandler?.()}
/>
),
enableSorting: false,
enableHiding: false,
2025-08-11 23:21:36 +07:00
},
2025-09-10 09:59:17 +07:00
{ accessorKey: "version", header: "Phiên bản" },
{ accessorKey: "fileName", header: "Tên file" },
{ accessorKey: "folderPath", header: "Đường dẫn" },
{
accessorKey: "updatedAt",
header: "Thời gian cập nhật",
cell: ({ getValue }) =>
getValue()
? new Date(getValue() as string).toLocaleString("vi-VN")
: "N/A",
},
];
2025-08-11 23:21:36 +07:00
return (
2025-09-10 09:59:17 +07:00
<AppManagerTemplate<Version>
title="Quản lý phần mềm"
description="Quản lý và gửi yêu cầu cài đặt phần mềm MSI"
data={versionList}
isLoading={isLoading}
columns={columns}
onUpload={(fd) => uploadMutation.mutateAsync(fd)}
onTableInit={setTable}
onUpdate={() => {
const selectedIds = table
?.getSelectedRowModel()
.rows.map((row: any) => (row.original as Version).id);
installMutation.mutateAsync({ msiFileIds: selectedIds });
}}
updateLoading={installMutation.isPending}
/>
2025-08-11 23:21:36 +07:00
);
}