refactor(FE-357): Refactor export data fetching and handlers

This commit is contained in:
rstubryan
2025-12-17 14:05:29 +07:00
parent 9365320b03
commit e1b562c175
@@ -165,7 +165,7 @@ const HppPerKandangTab = () => {
}, [tableFilterState.period]); }, [tableFilterState.period]);
// ===== DATA FETCHING ===== // ===== DATA FETCHING =====
const { data: hppPerKandang, isLoading } = useSWR( const { data: hppPerKandangResponse, isLoading } = useSWR(
isSubmitted isSubmitted
? () => { ? () => {
const params = { const params = {
@@ -207,51 +207,52 @@ const HppPerKandangTab = () => {
const data: HppPerKandangReport['rows'] = useMemo( const data: HppPerKandangReport['rows'] = useMemo(
() => () =>
isResponseSuccess(hppPerKandang) isResponseSuccess(hppPerKandangResponse)
? (hppPerKandang?.data?.rows as HppPerKandangReport['rows']) || [] ? (hppPerKandangResponse?.data?.rows as HppPerKandangReport['rows']) ||
[]
: [], : [],
[hppPerKandang] [hppPerKandangResponse]
); );
const summary = const summary =
isResponseSuccess(hppPerKandang) && hppPerKandang?.data?.summary isResponseSuccess(hppPerKandangResponse) &&
? hppPerKandang.data.summary hppPerKandangResponse?.data?.summary
? hppPerKandangResponse.data.summary
: undefined; : undefined;
const period = const period =
isResponseSuccess(hppPerKandang) && hppPerKandang?.data?.period isResponseSuccess(hppPerKandangResponse) &&
? hppPerKandang.data.period hppPerKandangResponse?.data?.period
? hppPerKandangResponse.data.period
: undefined; : undefined;
const { data: allDataForExport } = useSWR( // ===== EXPORT DATA FETCHER =====
isSubmitted const fetchAllExportData =
? () => { useCallback(async (): Promise<HppPerKandangReport | null> => {
const params = { const params = {
area_id: tableFilterState.area_id area_id: tableFilterState.area_id
? Number(tableFilterState.area_id) ? Number(tableFilterState.area_id)
: undefined, : undefined,
location_id: tableFilterState.location_id location_id: tableFilterState.location_id
? Number(tableFilterState.location_id) ? Number(tableFilterState.location_id)
: undefined, : undefined,
kandang_id: tableFilterState.kandang_id kandang_id: tableFilterState.kandang_id
? Number(tableFilterState.kandang_id) ? Number(tableFilterState.kandang_id)
: undefined, : undefined,
weight_min: tableFilterState.weight_min weight_min: tableFilterState.weight_min
? Number(tableFilterState.weight_min) ? Number(tableFilterState.weight_min)
: undefined, : undefined,
weight_max: tableFilterState.weight_max weight_max: tableFilterState.weight_max
? Number(tableFilterState.weight_max) ? Number(tableFilterState.weight_max)
: undefined, : undefined,
period: tableFilterState.period || undefined, period: tableFilterState.period || undefined,
sort_by: tableFilterState.sort_by || undefined, sort_by: tableFilterState.sort_by || undefined,
show_unrecorded: tableFilterState.show_unrecorded, show_unrecorded: tableFilterState.show_unrecorded,
}; limit: 10000,
page: 1,
};
return ['hpp-per-kandang-report-export', params]; const response = await SaleReportApi.getHppPerKandangReport(
}
: null,
([, params]) =>
SaleReportApi.getHppPerKandangReport(
params.area_id, params.area_id,
params.location_id, params.location_id,
params.kandang_id, params.kandang_id,
@@ -260,26 +261,29 @@ const HppPerKandangTab = () => {
params.period, params.period,
params.sort_by, params.sort_by,
params.show_unrecorded params.show_unrecorded
) );
);
const allExportData: HppPerKandangReport['rows'] = useMemo( return isResponseSuccess(response) ? response.data : null;
() => }, [tableFilterState]);
isResponseSuccess(allDataForExport)
? (allDataForExport?.data?.rows as HppPerKandangReport['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 fetchAllExportData();
if (
!allDataForExport ||
!allDataForExport?.rows ||
allDataForExport.rows.length === 0
) {
toast.error('Tidak ada data untuk diekspor.');
return;
}
const allExportData =
allDataForExport.rows as HppPerKandangReport['rows'];
const totals = allExportData.reduce( const totals = allExportData.reduce(
(acc, item) => ({ (acc, item) => ({
total_remaining_chicken_birds: total_remaining_chicken_birds:
@@ -410,7 +414,7 @@ const HppPerKandangTab = () => {
setIsExcelExportLoading(false); setIsExcelExportLoading(false);
} }
}, [ }, [
allExportData, fetchAllExportData,
tableFilterState, tableFilterState,
areaOptions, areaOptions,
locationOptions, locationOptions,
@@ -418,13 +422,19 @@ const HppPerKandangTab = () => {
]); ]);
const handleExportPDF = useCallback(async () => { const handleExportPDF = useCallback(async () => {
if (!hppPerKandang || !isResponseSuccess(hppPerKandang)) {
toast.error('Tidak ada data untuk diekspor.');
return;
}
setIsPdfExportLoading(true); setIsPdfExportLoading(true);
try { try {
const allDataForExport = await fetchAllExportData();
if (
!allDataForExport ||
!allDataForExport?.rows ||
allDataForExport.rows.length === 0
) {
toast.error('Tidak ada data untuk diekspor.');
return;
}
const areaName = tableFilterState.area_id const areaName = tableFilterState.area_id
? areaOptions.find( ? areaOptions.find(
(opt) => opt.value === Number(tableFilterState.area_id) (opt) => opt.value === Number(tableFilterState.area_id)
@@ -443,7 +453,7 @@ const HppPerKandangTab = () => {
)?.label || 'Semua Kandang' )?.label || 'Semua Kandang'
: 'Semua Kandang'; : 'Semua Kandang';
await generateHppPerKandangPDF(hppPerKandang.data, { await generateHppPerKandangPDF(allDataForExport, {
area_name: areaName, area_name: areaName,
location_name: locationName, location_name: locationName,
kandang_name: kandangName, kandang_name: kandangName,
@@ -461,7 +471,7 @@ const HppPerKandangTab = () => {
setIsPdfExportLoading(false); setIsPdfExportLoading(false);
} }
}, [ }, [
hppPerKandang, fetchAllExportData,
tableFilterState, tableFilterState,
areaOptions, areaOptions,
locationOptions, locationOptions,