mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-25 15:55:48 +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
|
? 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
|
||||||
@@ -311,11 +311,7 @@ const PurchasesPerSupplierTab = () => {
|
|||||||
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) {
|
setIsExcelExportLoading(true);
|
||||||
|
try {
|
||||||
|
const allDataForExport = await logisticPurchasePerSupplierExport();
|
||||||
|
|
||||||
|
if (
|
||||||
|
!allDataForExport ||
|
||||||
|
!allDataForExport?.rows ||
|
||||||
|
allDataForExport.rows.length === 0
|
||||||
|
) {
|
||||||
toast.error('Tidak ada data untuk diekspor.');
|
toast.error('Tidak ada data untuk diekspor.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setIsExcelExportLoading(true);
|
|
||||||
|
|
||||||
try {
|
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) {
|
setIsPdfExportLoading(true);
|
||||||
|
try {
|
||||||
|
const allDataForExport = await logisticPurchasePerSupplierExport();
|
||||||
|
|
||||||
|
if (
|
||||||
|
!allDataForExport ||
|
||||||
|
!allDataForExport?.rows ||
|
||||||
|
allDataForExport.rows.length === 0
|
||||||
|
) {
|
||||||
toast.error('Tidak ada data untuk diekspor.');
|
toast.error('Tidak ada data untuk diekspor.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setIsPdfExportLoading(true);
|
|
||||||
try {
|
|
||||||
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,
|
||||||
|
|||||||
Reference in New Issue
Block a user