refactor(FE-363): Fetch export data on demand via callback

This commit is contained in:
rstubryan
2025-12-18 13:50:38 +07:00
parent 8fb1ccbdce
commit 1be596921a
@@ -275,47 +275,43 @@ const PurchasesPerSupplierTab = () => {
? purchasePerSupplier.meta ? purchasePerSupplier.meta
: null; : null;
const { data: allDataForExport } = useSWR( // ===== EXPORT DATA FETCHER =====
isSubmitted const logisticPurchasePerSupplierExport =
? () => { useCallback(async (): Promise<LogisticPurchasePerSupplierReport | null> => {
const params = { const params = {
area_id: area_id:
tableFilterState.area_id.length > 0 tableFilterState.area_id.length > 0
? tableFilterState.area_id.join(',') ? tableFilterState.area_id.join(',')
: undefined, : undefined,
supplier_id: supplier_id:
tableFilterState.supplier_id.length > 0 tableFilterState.supplier_id.length > 0
? tableFilterState.supplier_id.join(',') ? tableFilterState.supplier_id.join(',')
: undefined, : undefined,
product_id: product_id:
tableFilterState.product_id.length > 0 tableFilterState.product_id.length > 0
? tableFilterState.product_id.join(',') ? tableFilterState.product_id.join(',')
: undefined, : undefined,
product_category_id: product_category_id:
tableFilterState.product_category_id.length > 0 tableFilterState.product_category_id.length > 0
? tableFilterState.product_category_id.join(',') ? tableFilterState.product_category_id.join(',')
: undefined, : undefined,
received_date: received_date:
tableFilterState.filter_by === 'received_date' tableFilterState.filter_by === 'received_date'
? tableFilterState.start_date || undefined ? tableFilterState.start_date || undefined
: undefined, : undefined,
po_date: po_date:
tableFilterState.filter_by === 'po_date' tableFilterState.filter_by === 'po_date'
? tableFilterState.start_date || undefined ? tableFilterState.start_date || undefined
: undefined, : undefined,
start_date: tableFilterState.start_date || undefined, start_date: tableFilterState.start_date || undefined,
end_date: tableFilterState.end_date || undefined, end_date: tableFilterState.end_date || undefined,
sort_by: tableFilterState.sort_by || undefined, sort_by: tableFilterState.sort_by || undefined,
filter_by: tableFilterState.filter_by || undefined, filter_by: tableFilterState.filter_by || undefined,
limit: 10000, limit: 10000,
page: 1, page: 1,
}; };
return ['logistic-purchase-report-export', params]; const response = await LogisticApi.getLogisticPurchasePerSupplierReport(
}
: null,
([, params]) =>
LogisticApi.getLogisticPurchasePerSupplierReport(
params.area_id, params.area_id,
params.supplier_id, params.supplier_id,
params.product_id, params.product_id,
@@ -328,27 +324,28 @@ const PurchasesPerSupplierTab = () => {
params.filter_by, params.filter_by,
params.page, params.page,
params.limit params.limit
) );
);
const allExportData: LogisticPurchasePerSupplierReport['rows'] = useMemo( return isResponseSuccess(response) ? response.data : null;
() => }, [tableFilterState]);
isResponseSuccess(allDataForExport)
? (allDataForExport?.data
?.rows as LogisticPurchasePerSupplierReport['rows']) || []
: [],
[allDataForExport]
);
// ===== EXPORT HANDLERS ===== // ===== EXPORT HANDLERS =====
const handleExportExcel = useCallback(() => { const handleExportExcel = useCallback(async () => {
if (allExportData.length === 0) {
toast.error('Tidak ada data untuk diekspor.');
return;
}
setIsExcelExportLoading(true); setIsExcelExportLoading(true);
try { 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 } = {}; const groupedBySupplier: { [key: string]: typeof allExportData } = {};
allExportData.forEach((item) => { allExportData.forEach((item) => {
@@ -461,16 +458,26 @@ const PurchasesPerSupplierTab = () => {
} finally { } finally {
setIsExcelExportLoading(false); setIsExcelExportLoading(false);
} }
}, [allExportData, tableFilterState, areaOptions, supplierOptions]); }, [
logisticPurchasePerSupplierExport,
tableFilterState,
areaOptions,
supplierOptions,
]);
const handleExportPdf = useCallback(async () => { const handleExportPdf = useCallback(async () => {
if (allExportData.length === 0) {
toast.error('Tidak ada data untuk diekspor.');
return;
}
setIsPdfExportLoading(true); setIsPdfExportLoading(true);
try { try {
const allDataForExport = await logisticPurchasePerSupplierExport();
if (
!allDataForExport ||
!allDataForExport?.rows ||
allDataForExport.rows.length === 0
) {
toast.error('Tidak ada data untuk diekspor.');
return;
}
const areaName = const areaName =
tableFilterState.area_id.length > 0 tableFilterState.area_id.length > 0
? tableFilterState.area_id ? tableFilterState.area_id
@@ -526,7 +533,10 @@ const PurchasesPerSupplierTab = () => {
end_date: tableFilterState.end_date || '', end_date: tableFilterState.end_date || '',
}; };
await generatePurchasesPerSupplierPDF(allExportData, exportParams); await generatePurchasesPerSupplierPDF(
allDataForExport.rows,
exportParams
);
toast.success('PDF berhasil dibuat dan diunduh.'); toast.success('PDF berhasil dibuat dan diunduh.');
} catch { } catch {
toast.error('Gagal membuat PDF. Silakan coba lagi.'); toast.error('Gagal membuat PDF. Silakan coba lagi.');
@@ -534,7 +544,7 @@ const PurchasesPerSupplierTab = () => {
setIsPdfExportLoading(false); setIsPdfExportLoading(false);
} }
}, [ }, [
allExportData, logisticPurchasePerSupplierExport,
tableFilterState, tableFilterState,
areaOptions, areaOptions,
supplierOptions, supplierOptions,