From 4e71b4525e728428c11ef0056540e4da4e07eda8 Mon Sep 17 00:00:00 2001 From: Anang Yusman Date: Tue, 22 Apr 2025 13:22:08 +0800 Subject: [PATCH] feat:update create,edit,detail task ta, download report --- .../contributor/report/components/columns.tsx | 62 ++++- .../report/components/report-table.tsx | 112 +++++++-- .../task-ta/components/columns.tsx | 4 +- .../task-ta/components/task-ta-table.tsx | 4 +- .../contributor/task-ta/detail/[id]/page.tsx | 3 +- .../contributor/task-ta/update/[id]/page.tsx | 3 +- ...etail-form.tsx => task-ta-detail-form.tsx} | 190 +++++++++++---- ...sk-edit-form.tsx => task-ta-edit-form.tsx} | 224 ++++++++++++------ components/form/task-ta/task-ta-form.tsx | 27 ++- service/report/report.ts | 25 ++ service/task.ts | 32 +++ 11 files changed, 528 insertions(+), 158 deletions(-) rename components/form/task-ta/{task-detail-form.tsx => task-ta-detail-form.tsx} (94%) rename components/form/task-ta/{task-edit-form.tsx => task-ta-edit-form.tsx} (89%) create mode 100644 service/report/report.ts diff --git a/app/[locale]/(protected)/contributor/report/components/columns.tsx b/app/[locale]/(protected)/contributor/report/components/columns.tsx index 259db757..8bf9f31e 100644 --- a/app/[locale]/(protected)/contributor/report/components/columns.tsx +++ b/app/[locale]/(protected)/contributor/report/components/columns.tsx @@ -25,8 +25,13 @@ import withReactContent from "sweetalert2-react-content"; import { deleteBlog } from "@/service/blog/blog"; import { error, loading } from "@/lib/swal"; import { useTranslations } from "next-intl"; +import axios from "axios"; -const useTableColumns = () => { +const useTableColumns = ({ + handlePreview, +}: { + handlePreview: (id: string) => void; +}) => { const t = useTranslations("Table"); // Panggil di dalam hook const columns: ColumnDef[] = [ @@ -112,6 +117,33 @@ const useTableColumns = () => { } }); }; + + const handleDownload = async (id: string) => { + try { + const response = await axios.get( + `https://netidhub.com/api/media/report/download?id=${id}`, + { + responseType: "blob", + } + ); + + const url = window.URL.createObjectURL(new Blob([response.data])); + const link = document.createElement("a"); + link.href = url; + link.setAttribute("download", `report-${id}.pdf`); + document.body.appendChild(link); + link.click(); + link.remove(); + } catch (error) { + console.error("Download failed", error); + MySwal.fire({ + title: "Gagal", + text: "Terjadi kesalahan saat mengunduh file.", + icon: "error", + }); + } + }; + return ( @@ -124,18 +156,22 @@ const useTableColumns = () => { - - - - Preview - - - - - - Download - - + handlePreview(row.original.id)} + className="p-2 border-b text-default-700 group focus:bg-default focus:text-primary-foreground rounded-none" + > + + Preview + + + handleDownload(row.original.id)} + className="p-2 border-b text-default-700 group focus:bg-default focus:text-primary-foreground rounded-none" + > + + Download + + handleDeleteBlog(row.original.id)} className="p-2 border-b text-destructive bg-destructive/30 focus:bg-destructive focus:text-destructive-foreground rounded-none" diff --git a/app/[locale]/(protected)/contributor/report/components/report-table.tsx b/app/[locale]/(protected)/contributor/report/components/report-table.tsx index 9ba22651..a5853ba8 100644 --- a/app/[locale]/(protected)/contributor/report/components/report-table.tsx +++ b/app/[locale]/(protected)/contributor/report/components/report-table.tsx @@ -43,14 +43,40 @@ import { DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Label } from "@/components/ui/label"; +import Swal from "sweetalert2"; import { listEnableCategory } from "@/service/content/content"; import { useTranslations } from "next-intl"; import { CardHeader, CardTitle } from "@/components/ui/card"; import { Link } from "@/i18n/routing"; import useTableColumns from "./columns"; +import { + getPreviewById, + paginationReport, + saveReport, +} from "@/service/report/report"; +import { + Dialog, + DialogContent, + DialogHeader, + DialogTitle, +} from "@/components/ui/dialog"; +import { format } from "date-fns"; +import withReactContent from "sweetalert2-react-content"; + +type PreviewApiResponse = { + error: boolean; + message: string; + data: { + id: number; + title: string; + filePath: string; + version: number; + } | null; +}; const ReportTable = () => { const router = useRouter(); + const MySwal = withReactContent(Swal); const searchParams = useSearchParams(); const t = useTranslations("Report"); const [dataTable, setDataTable] = React.useState([]); @@ -78,7 +104,16 @@ const ReportTable = () => { const [categoryFilter, setCategoryFilter] = React.useState(""); const [dateFilter, setDateFilter] = React.useState(""); const [statusFilter, setStatusFilter] = React.useState([]); - const columns = useTableColumns(); + const [openPreview, setOpenPreview] = React.useState(false); + const [previewData, setPreviewData] = React.useState(null); + + const handlePreview = (id: string) => { + const url = `https://netidhub.com/api/media/report/view?id=${id}`; + setPreviewData({ url }); + setOpenPreview(true); + }; + + const columns = useTableColumns({ handlePreview }); const table = useReactTable({ data: dataTable, columns, @@ -114,7 +149,7 @@ const ReportTable = () => { async function fetchData() { try { - const res = await paginationBlog( + const res = await paginationReport( showData, page - 1, search, @@ -144,11 +179,10 @@ const ReportTable = () => { } const handleCheckboxChange = (categoryId: number) => { - setSelectedCategories( - (prev: any) => - prev.includes(categoryId) - ? prev.filter((id: any) => id !== categoryId) // Hapus jika sudah dipilih - : [...prev, categoryId] // Tambahkan jika belum dipilih + setSelectedCategories((prev: any) => + prev.includes(categoryId) + ? prev.filter((id: any) => id !== categoryId) + : [...prev, categoryId] ); // Perbarui filter kategori @@ -176,8 +210,61 @@ const ReportTable = () => { table.getColumn("judul")?.setFilterValue(e.target.value); // Set filter tabel }; + const handleGenerateReport = async () => { + const today = new Date(); + const formattedDate = format(today, "dd-MM-yyyy"); // Hasil: 22-04-2025 + const title = `Report ${formattedDate}`; + + const requestData = { + title, + }; + + try { + const response = await saveReport(requestData); + + if (response?.error) { + MySwal.fire( + "Error", + response?.message || "Gagal menyimpan laporan", + "error" + ); + return; + } + + MySwal.fire({ + title: "Sukses", + text: "Laporan berhasil dibuat.", + icon: "success", + confirmButtonColor: "#3085d6", + confirmButtonText: "OK", + }).then(() => { + fetchData(); // Refresh tabel setelah generate + }); + } catch (error) { + console.error("Generate report error:", error); + MySwal.fire("Error", "Terjadi kesalahan saat membuat laporan", "error"); + } + }; + return (
+ + + + Preview Laporan + +
+ {previewData ? ( +