refactor(FE-356): Use summaryTotal and memoize per-weight summary

This commit is contained in:
rstubryan
2025-12-20 10:18:50 +07:00
parent 9e0631a415
commit 478e9eb541
@@ -219,21 +219,19 @@ const HppPerKandangTab = () => {
[hppPerKandang] [hppPerKandang]
); );
const summary =
isResponseSuccess(hppPerKandang) && hppPerKandang?.data?.summary
? hppPerKandang.data.summary
: undefined;
const summaryTotal = const summaryTotal =
isResponseSuccess(hppPerKandang) && hppPerKandang?.data?.summary?.total isResponseSuccess(hppPerKandang) && hppPerKandang?.data?.summary?.total
? hppPerKandang.data.summary.total ? hppPerKandang.data.summary.total
: undefined; : undefined;
const perWeightRangeSummary = const perWeightRangeSummary = useMemo(
isResponseSuccess(hppPerKandang) && () =>
hppPerKandang?.data?.summary?.per_weight_range isResponseSuccess(hppPerKandang) &&
? hppPerKandang.data.summary.per_weight_range hppPerKandang?.data?.summary?.per_weight_range
: []; ? hppPerKandang.data.summary.per_weight_range
: [],
[hppPerKandang]
);
const period = const period =
isResponseSuccess(hppPerKandang) && hppPerKandang?.data?.period isResponseSuccess(hppPerKandang) && hppPerKandang?.data?.period
@@ -318,7 +316,7 @@ const HppPerKandangTab = () => {
const allExportData = const allExportData =
allDataForExport.rows as HppPerKandangReport['rows']; allDataForExport.rows as HppPerKandangReport['rows'];
const summary = allDataForExport.summary; const summaryTotal = allDataForExport.summary.total;
const excelData: { [key: string]: string | number }[] = allExportData.map( const excelData: { [key: string]: string | number }[] = allExportData.map(
(item: HppPerKandangRow, index: number) => ({ (item: HppPerKandangRow, index: number) => ({
@@ -352,18 +350,21 @@ const HppPerKandangTab = () => {
No: 'TOTAL', No: 'TOTAL',
Kandang: 'ALL', Kandang: 'ALL',
'Rentang Bobot': '-', 'Rentang Bobot': '-',
'Rata-Rata Bobot (KG)': summary?.average_weight_kg || 0, 'Rata-Rata Bobot (KG)': summaryTotal?.average_weight_kg || 0,
'Sisa Ayam (Ekor)': summary?.total_remaining_chicken_birds || 0, 'Sisa Ayam (Ekor)': summaryTotal?.total_remaining_chicken_birds || 0,
'Sisa Ayam (KG)': summary?.total_remaining_chicken_weight_kg || 0, 'Sisa Ayam (KG)': summaryTotal?.total_remaining_chicken_weight_kg || 0,
'Produksi Telur (Butir)': summary?.total_egg_production_pieces || 0, 'Produksi Telur (Butir)':
'Produksi Telur (KG)': summary?.total_egg_production_kg || 0, summaryTotal?.total_egg_production_pieces || 0,
'Produksi Telur (KG)': summaryTotal?.total_egg_production_kg || 0,
'Feed (Supplier)': allFeedSuppliers, 'Feed (Supplier)': allFeedSuppliers,
'DOC (Supplier)': allDocSuppliers, 'DOC (Supplier)': allDocSuppliers,
'Rata-Rata Harga DOC (RP)': summary?.total_average_doc_price_rp || 0, 'Rata-Rata Harga DOC (RP)':
'Nilai Nominal Telur (RP)': summary?.total_egg_value_rp || 0, summaryTotal?.total_average_doc_price_rp || 0,
'HPP Ayam (RP)': summary?.total_hpp_rp || 0, 'Nilai Nominal Telur (RP)': summaryTotal?.total_egg_value_rp || 0,
'HPP Telur (RP/KG)': summary?.average_egg_hpp_rp_per_kg || 0, 'HPP Ayam (RP)': summaryTotal?.total_hpp_rp || 0,
'Nilai Nominal Sisa Ayam (RP)': summary?.total_remaining_value_rp || 0, 'HPP Telur (RP/KG)': summaryTotal?.average_egg_hpp_rp_per_kg || 0,
'Nilai Nominal Sisa Ayam (RP)':
summaryTotal?.total_remaining_value_rp || 0,
}); });
const worksheet = XLSX.utils.json_to_sheet(excelData); const worksheet = XLSX.utils.json_to_sheet(excelData);
@@ -519,7 +520,7 @@ const HppPerKandangTab = () => {
}, },
footer: () => ( footer: () => (
<div className='text-right font-semibold text-gray-900'> <div className='text-right font-semibold text-gray-900'>
{formatNumber(summary?.total?.average_weight_kg || 0)} {formatNumber(summaryTotal?.average_weight_kg || 0)}
</div> </div>
), ),
}, },
@@ -533,7 +534,7 @@ const HppPerKandangTab = () => {
}, },
footer: () => ( footer: () => (
<div className='text-right font-semibold text-gray-900'> <div className='text-right font-semibold text-gray-900'>
{formatNumber(summary?.total?.total_remaining_chicken_birds || 0)} {formatNumber(summaryTotal?.total_remaining_chicken_birds || 0)}
</div> </div>
), ),
}, },
@@ -547,9 +548,7 @@ const HppPerKandangTab = () => {
}, },
footer: () => ( footer: () => (
<div className='text-right font-semibold text-gray-900'> <div className='text-right font-semibold text-gray-900'>
{formatNumber( {formatNumber(summaryTotal?.total_remaining_chicken_weight_kg || 0)}
summary?.total?.total_remaining_chicken_weight_kg || 0
)}
</div> </div>
), ),
}, },
@@ -563,7 +562,7 @@ const HppPerKandangTab = () => {
}, },
footer: () => ( footer: () => (
<div className='text-right font-semibold text-gray-900'> <div className='text-right font-semibold text-gray-900'>
{formatNumber(summary?.total?.total_egg_production_pieces || 0)} {formatNumber(summaryTotal?.total_egg_production_pieces || 0)}
</div> </div>
), ),
}, },
@@ -577,9 +576,7 @@ const HppPerKandangTab = () => {
}, },
footer: () => ( footer: () => (
<div className='text-right font-semibold text-gray-900'> <div className='text-right font-semibold text-gray-900'>
{formatNumber( {formatNumber(summaryTotal?.total_remaining_chicken_weight_kg || 0)}
summary?.total?.total_remaining_chicken_weight_kg || 0
)}
</div> </div>
), ),
}, },
@@ -629,7 +626,7 @@ const HppPerKandangTab = () => {
}, },
footer: () => ( footer: () => (
<div className='text-right font-semibold text-gray-900'> <div className='text-right font-semibold text-gray-900'>
{formatCurrency(summary?.total?.total_average_doc_price_rp || 0)} {formatCurrency(summaryTotal?.total_average_doc_price_rp || 0)}
</div> </div>
), ),
}, },
@@ -643,7 +640,7 @@ const HppPerKandangTab = () => {
}, },
footer: () => ( footer: () => (
<div className='text-right font-semibold text-gray-900'> <div className='text-right font-semibold text-gray-900'>
{formatCurrency(summary?.total?.total_egg_value_rp || 0)} {formatCurrency(summaryTotal?.total_egg_value_rp || 0)}
</div> </div>
), ),
}, },
@@ -657,7 +654,7 @@ const HppPerKandangTab = () => {
}, },
footer: () => ( footer: () => (
<div className='text-right font-semibold text-gray-900'> <div className='text-right font-semibold text-gray-900'>
{formatCurrency(summary?.total?.total_hpp_rp || 0)} {formatCurrency(summaryTotal?.total_hpp_rp || 0)}
</div> </div>
), ),
}, },
@@ -671,7 +668,7 @@ const HppPerKandangTab = () => {
}, },
footer: () => ( footer: () => (
<div className='text-right font-semibold text-gray-900'> <div className='text-right font-semibold text-gray-900'>
{formatCurrency(summary?.total?.average_egg_hpp_rp_per_kg || 0)} {formatCurrency(summaryTotal?.average_egg_hpp_rp_per_kg || 0)}
</div> </div>
), ),
}, },
@@ -685,7 +682,7 @@ const HppPerKandangTab = () => {
}, },
footer: () => ( footer: () => (
<div className='text-right font-semibold text-gray-900'> <div className='text-right font-semibold text-gray-900'>
{formatCurrency(summary?.total?.total_remaining_value_rp || 0)} {formatCurrency(summaryTotal?.total_remaining_value_rp || 0)}
</div> </div>
), ),
}, },