mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
refactor(FE-363): Fetch export data on demand via callback
This commit is contained in:
@@ -275,9 +275,9 @@ const PurchasesPerSupplierTab = () => {
|
||||
? purchasePerSupplier.meta
|
||||
: null;
|
||||
|
||||
const { data: allDataForExport } = useSWR(
|
||||
isSubmitted
|
||||
? () => {
|
||||
// ===== EXPORT DATA FETCHER =====
|
||||
const logisticPurchasePerSupplierExport =
|
||||
useCallback(async (): Promise<LogisticPurchasePerSupplierReport | null> => {
|
||||
const params = {
|
||||
area_id:
|
||||
tableFilterState.area_id.length > 0
|
||||
@@ -311,11 +311,7 @@ const PurchasesPerSupplierTab = () => {
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
setIsExcelExportLoading(true);
|
||||
|
||||
try {
|
||||
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) {
|
||||
setIsPdfExportLoading(true);
|
||||
try {
|
||||
const allDataForExport = await logisticPurchasePerSupplierExport();
|
||||
|
||||
if (
|
||||
!allDataForExport ||
|
||||
!allDataForExport?.rows ||
|
||||
allDataForExport.rows.length === 0
|
||||
) {
|
||||
toast.error('Tidak ada data untuk diekspor.');
|
||||
return;
|
||||
}
|
||||
|
||||
setIsPdfExportLoading(true);
|
||||
try {
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user