From c65afe8ced50eda9d764f94d17c083d8ea94c0d6 Mon Sep 17 00:00:00 2001 From: Sabda Yagra Date: Fri, 26 Sep 2025 14:46:28 +0700 Subject: [PATCH] feat: add authorization in report download --- .../contributor/report/components/columns.tsx | 38 +++++++++++++++---- service/report/report.ts | 7 +++- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/app/[locale]/(protected)/contributor/report/components/columns.tsx b/app/[locale]/(protected)/contributor/report/components/columns.tsx index 1b9579e5..f6c67f1d 100644 --- a/app/[locale]/(protected)/contributor/report/components/columns.tsx +++ b/app/[locale]/(protected)/contributor/report/components/columns.tsx @@ -26,6 +26,7 @@ import { deleteBlog } from "@/service/blog/blog"; import { error, loading, close } from "@/lib/swal"; import { useTranslations } from "next-intl"; import axios from "axios"; +import { downloadReport } from "@/service/report/report"; const useTableColumns = ({ handlePreview, @@ -127,14 +128,9 @@ const useTableColumns = ({ const handleDownload = async (id: string) => { try { loading(); - 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 response = await downloadReport(id); + const url = window.URL.createObjectURL(new Blob([response?.data])); const link = document.createElement("a"); link.href = url; link.setAttribute("download", `report-${id}.pdf`); @@ -152,6 +148,34 @@ const useTableColumns = ({ } }; + // const handleDownload = async (id: string) => { + // try { + // loading(); + // 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(); + // close(); + // } catch (error) { + // console.error("Download failed", error); + // MySwal.fire({ + // title: "Gagal", + // text: "Terjadi kesalahan saat mengunduh file.", + // icon: "error", + // }); + // } + // }; + return ( diff --git a/service/report/report.ts b/service/report/report.ts index f469359e..ca8e6e15 100644 --- a/service/report/report.ts +++ b/service/report/report.ts @@ -32,4 +32,9 @@ export async function getUserReports(id: any) { export async function saveUserReportsAction(id: any, action: any) { const url = `users/reports/action?id=${id}&action=${action}`; return httpPostInterceptor(url); -} +}; + +export async function downloadReport(id: any) { + const url = `/media/report/download?id=${id}`; + return httpGetInterceptor(url); +} \ No newline at end of file