mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
refactor(FE-357): Refactor export data fetching and handlers
This commit is contained in:
@@ -165,7 +165,7 @@ const HppPerKandangTab = () => {
|
||||
}, [tableFilterState.period]);
|
||||
|
||||
// ===== DATA FETCHING =====
|
||||
const { data: hppPerKandang, isLoading } = useSWR(
|
||||
const { data: hppPerKandangResponse, isLoading } = useSWR(
|
||||
isSubmitted
|
||||
? () => {
|
||||
const params = {
|
||||
@@ -207,51 +207,52 @@ const HppPerKandangTab = () => {
|
||||
|
||||
const data: HppPerKandangReport['rows'] = useMemo(
|
||||
() =>
|
||||
isResponseSuccess(hppPerKandang)
|
||||
? (hppPerKandang?.data?.rows as HppPerKandangReport['rows']) || []
|
||||
isResponseSuccess(hppPerKandangResponse)
|
||||
? (hppPerKandangResponse?.data?.rows as HppPerKandangReport['rows']) ||
|
||||
[]
|
||||
: [],
|
||||
[hppPerKandang]
|
||||
[hppPerKandangResponse]
|
||||
);
|
||||
|
||||
const summary =
|
||||
isResponseSuccess(hppPerKandang) && hppPerKandang?.data?.summary
|
||||
? hppPerKandang.data.summary
|
||||
isResponseSuccess(hppPerKandangResponse) &&
|
||||
hppPerKandangResponse?.data?.summary
|
||||
? hppPerKandangResponse.data.summary
|
||||
: undefined;
|
||||
|
||||
const period =
|
||||
isResponseSuccess(hppPerKandang) && hppPerKandang?.data?.period
|
||||
? hppPerKandang.data.period
|
||||
isResponseSuccess(hppPerKandangResponse) &&
|
||||
hppPerKandangResponse?.data?.period
|
||||
? hppPerKandangResponse.data.period
|
||||
: undefined;
|
||||
|
||||
const { data: allDataForExport } = useSWR(
|
||||
isSubmitted
|
||||
? () => {
|
||||
const params = {
|
||||
area_id: tableFilterState.area_id
|
||||
? Number(tableFilterState.area_id)
|
||||
: undefined,
|
||||
location_id: tableFilterState.location_id
|
||||
? Number(tableFilterState.location_id)
|
||||
: undefined,
|
||||
kandang_id: tableFilterState.kandang_id
|
||||
? Number(tableFilterState.kandang_id)
|
||||
: undefined,
|
||||
weight_min: tableFilterState.weight_min
|
||||
? Number(tableFilterState.weight_min)
|
||||
: undefined,
|
||||
weight_max: tableFilterState.weight_max
|
||||
? Number(tableFilterState.weight_max)
|
||||
: undefined,
|
||||
period: tableFilterState.period || undefined,
|
||||
sort_by: tableFilterState.sort_by || undefined,
|
||||
show_unrecorded: tableFilterState.show_unrecorded,
|
||||
};
|
||||
// ===== EXPORT DATA FETCHER =====
|
||||
const fetchAllExportData =
|
||||
useCallback(async (): Promise<HppPerKandangReport | null> => {
|
||||
const params = {
|
||||
area_id: tableFilterState.area_id
|
||||
? Number(tableFilterState.area_id)
|
||||
: undefined,
|
||||
location_id: tableFilterState.location_id
|
||||
? Number(tableFilterState.location_id)
|
||||
: undefined,
|
||||
kandang_id: tableFilterState.kandang_id
|
||||
? Number(tableFilterState.kandang_id)
|
||||
: undefined,
|
||||
weight_min: tableFilterState.weight_min
|
||||
? Number(tableFilterState.weight_min)
|
||||
: undefined,
|
||||
weight_max: tableFilterState.weight_max
|
||||
? Number(tableFilterState.weight_max)
|
||||
: undefined,
|
||||
period: tableFilterState.period || undefined,
|
||||
sort_by: tableFilterState.sort_by || undefined,
|
||||
show_unrecorded: tableFilterState.show_unrecorded,
|
||||
limit: 10000,
|
||||
page: 1,
|
||||
};
|
||||
|
||||
return ['hpp-per-kandang-report-export', params];
|
||||
}
|
||||
: null,
|
||||
([, params]) =>
|
||||
SaleReportApi.getHppPerKandangReport(
|
||||
const response = await SaleReportApi.getHppPerKandangReport(
|
||||
params.area_id,
|
||||
params.location_id,
|
||||
params.kandang_id,
|
||||
@@ -260,26 +261,29 @@ const HppPerKandangTab = () => {
|
||||
params.period,
|
||||
params.sort_by,
|
||||
params.show_unrecorded
|
||||
)
|
||||
);
|
||||
);
|
||||
|
||||
const allExportData: HppPerKandangReport['rows'] = useMemo(
|
||||
() =>
|
||||
isResponseSuccess(allDataForExport)
|
||||
? (allDataForExport?.data?.rows as HppPerKandangReport['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 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(
|
||||
(acc, item) => ({
|
||||
total_remaining_chicken_birds:
|
||||
@@ -410,7 +414,7 @@ const HppPerKandangTab = () => {
|
||||
setIsExcelExportLoading(false);
|
||||
}
|
||||
}, [
|
||||
allExportData,
|
||||
fetchAllExportData,
|
||||
tableFilterState,
|
||||
areaOptions,
|
||||
locationOptions,
|
||||
@@ -418,13 +422,19 @@ const HppPerKandangTab = () => {
|
||||
]);
|
||||
|
||||
const handleExportPDF = useCallback(async () => {
|
||||
if (!hppPerKandang || !isResponseSuccess(hppPerKandang)) {
|
||||
toast.error('Tidak ada data untuk diekspor.');
|
||||
return;
|
||||
}
|
||||
|
||||
setIsPdfExportLoading(true);
|
||||
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
|
||||
? areaOptions.find(
|
||||
(opt) => opt.value === Number(tableFilterState.area_id)
|
||||
@@ -443,7 +453,7 @@ const HppPerKandangTab = () => {
|
||||
)?.label || 'Semua Kandang'
|
||||
: 'Semua Kandang';
|
||||
|
||||
await generateHppPerKandangPDF(hppPerKandang.data, {
|
||||
await generateHppPerKandangPDF(allDataForExport, {
|
||||
area_name: areaName,
|
||||
location_name: locationName,
|
||||
kandang_name: kandangName,
|
||||
@@ -461,7 +471,7 @@ const HppPerKandangTab = () => {
|
||||
setIsPdfExportLoading(false);
|
||||
}
|
||||
}, [
|
||||
hppPerKandang,
|
||||
fetchAllExportData,
|
||||
tableFilterState,
|
||||
areaOptions,
|
||||
locationOptions,
|
||||
|
||||
Reference in New Issue
Block a user