From 1be596921a03b9e1092925a936b9a07c8feb9112 Mon Sep 17 00:00:00 2001 From: rstubryan Date: Thu, 18 Dec 2025 13:50:38 +0700 Subject: [PATCH] refactor(FE-363): Fetch export data on demand via callback --- .../tab/PurchasesPerSupplierTab.tsx | 138 ++++++++++-------- 1 file changed, 74 insertions(+), 64 deletions(-) diff --git a/src/components/pages/report/logistic-stock/tab/PurchasesPerSupplierTab.tsx b/src/components/pages/report/logistic-stock/tab/PurchasesPerSupplierTab.tsx index e345ad58..881c85f7 100644 --- a/src/components/pages/report/logistic-stock/tab/PurchasesPerSupplierTab.tsx +++ b/src/components/pages/report/logistic-stock/tab/PurchasesPerSupplierTab.tsx @@ -275,47 +275,43 @@ const PurchasesPerSupplierTab = () => { ? purchasePerSupplier.meta : null; - const { data: allDataForExport } = useSWR( - isSubmitted - ? () => { - const params = { - area_id: - tableFilterState.area_id.length > 0 - ? tableFilterState.area_id.join(',') - : undefined, - supplier_id: - tableFilterState.supplier_id.length > 0 - ? tableFilterState.supplier_id.join(',') - : undefined, - product_id: - tableFilterState.product_id.length > 0 - ? tableFilterState.product_id.join(',') - : undefined, - product_category_id: - tableFilterState.product_category_id.length > 0 - ? tableFilterState.product_category_id.join(',') - : undefined, - received_date: - tableFilterState.filter_by === 'received_date' - ? tableFilterState.start_date || undefined - : undefined, - po_date: - tableFilterState.filter_by === 'po_date' - ? tableFilterState.start_date || undefined - : undefined, - start_date: tableFilterState.start_date || undefined, - end_date: tableFilterState.end_date || undefined, - sort_by: tableFilterState.sort_by || undefined, - filter_by: tableFilterState.filter_by || undefined, - limit: 10000, - page: 1, - }; + // ===== EXPORT DATA FETCHER ===== + const logisticPurchasePerSupplierExport = + useCallback(async (): Promise => { + const params = { + area_id: + tableFilterState.area_id.length > 0 + ? tableFilterState.area_id.join(',') + : undefined, + supplier_id: + tableFilterState.supplier_id.length > 0 + ? tableFilterState.supplier_id.join(',') + : undefined, + product_id: + tableFilterState.product_id.length > 0 + ? tableFilterState.product_id.join(',') + : undefined, + product_category_id: + tableFilterState.product_category_id.length > 0 + ? tableFilterState.product_category_id.join(',') + : undefined, + received_date: + tableFilterState.filter_by === 'received_date' + ? tableFilterState.start_date || undefined + : undefined, + po_date: + tableFilterState.filter_by === 'po_date' + ? tableFilterState.start_date || undefined + : undefined, + start_date: tableFilterState.start_date || undefined, + end_date: tableFilterState.end_date || undefined, + sort_by: tableFilterState.sort_by || undefined, + filter_by: tableFilterState.filter_by || undefined, + limit: 10000, + page: 1, + }; - return ['logistic-purchase-report-export', params]; - } - : null, - ([, params]) => - LogisticApi.getLogisticPurchasePerSupplierReport( + const response = await LogisticApi.getLogisticPurchasePerSupplierReport( params.area_id, params.supplier_id, params.product_id, @@ -328,27 +324,28 @@ const PurchasesPerSupplierTab = () => { params.filter_by, params.page, params.limit - ) - ); + ); - const allExportData: LogisticPurchasePerSupplierReport['rows'] = useMemo( - () => - isResponseSuccess(allDataForExport) - ? (allDataForExport?.data - ?.rows as LogisticPurchasePerSupplierReport['rows']) || [] - : [], - [allDataForExport] - ); + return isResponseSuccess(response) ? response.data : null; + }, [tableFilterState]); // ===== EXPORT HANDLERS ===== - const handleExportExcel = useCallback(() => { - if (allExportData.length === 0) { - toast.error('Tidak ada data untuk diekspor.'); - return; - } + const handleExportExcel = useCallback(async () => { setIsExcelExportLoading(true); - try { + const allDataForExport = await logisticPurchasePerSupplierExport(); + + if ( + !allDataForExport || + !allDataForExport?.rows || + allDataForExport.rows.length === 0 + ) { + toast.error('Tidak ada data untuk diekspor.'); + return; + } + + const allExportData = + allDataForExport.rows as LogisticPurchasePerSupplierReport['rows']; const groupedBySupplier: { [key: string]: typeof allExportData } = {}; allExportData.forEach((item) => { @@ -461,16 +458,26 @@ const PurchasesPerSupplierTab = () => { } finally { setIsExcelExportLoading(false); } - }, [allExportData, tableFilterState, areaOptions, supplierOptions]); + }, [ + logisticPurchasePerSupplierExport, + tableFilterState, + areaOptions, + supplierOptions, + ]); const handleExportPdf = useCallback(async () => { - if (allExportData.length === 0) { - toast.error('Tidak ada data untuk diekspor.'); - return; - } - setIsPdfExportLoading(true); try { + const allDataForExport = await logisticPurchasePerSupplierExport(); + + if ( + !allDataForExport || + !allDataForExport?.rows || + allDataForExport.rows.length === 0 + ) { + toast.error('Tidak ada data untuk diekspor.'); + return; + } const areaName = tableFilterState.area_id.length > 0 ? tableFilterState.area_id @@ -526,7 +533,10 @@ const PurchasesPerSupplierTab = () => { end_date: tableFilterState.end_date || '', }; - await generatePurchasesPerSupplierPDF(allExportData, exportParams); + await generatePurchasesPerSupplierPDF( + allDataForExport.rows, + exportParams + ); toast.success('PDF berhasil dibuat dan diunduh.'); } catch { toast.error('Gagal membuat PDF. Silakan coba lagi.'); @@ -534,7 +544,7 @@ const PurchasesPerSupplierTab = () => { setIsPdfExportLoading(false); } }, [ - allExportData, + logisticPurchasePerSupplierExport, tableFilterState, areaOptions, supplierOptions,