refactor(FE-326,327): enhance SalesReportTable to handle empty sales data and conditionally render summary row

This commit is contained in:
rstubryan
2025-12-04 14:08:44 +07:00
parent ba40bbb1d3
commit 1a4a05308f
@@ -102,6 +102,18 @@ const SalesReportTable = ({
}, [initialValues, activeTabId]); }, [initialValues, activeTabId]);
const totals = useMemo(() => { const totals = useMemo(() => {
if (salesBroilerData.length === 0) {
return {
totalQuantity: 0,
totalWeight: 0,
avgWeight: 0,
avgPricePartner: 0,
totalPartner: 0,
avgPriceAct: 0,
totalAct: 0,
};
}
const totalQuantity = salesBroilerData.reduce( const totalQuantity = salesBroilerData.reduce(
(sum, item) => sum + (item.quantity || 0), (sum, item) => sum + (item.quantity || 0),
0 0
@@ -113,7 +125,7 @@ const SalesReportTable = ({
const avgWeight = totalQuantity > 0 ? totalWeight / totalQuantity : 0; const avgWeight = totalQuantity > 0 ? totalWeight / totalQuantity : 0;
const validPriceItems = salesBroilerData.filter( const validPriceItems = salesBroilerData.filter(
(item) => item.price != null (item) => item.price != null && item.price > 0
); );
const avgPricePartner = const avgPricePartner =
validPriceItems.length > 0 validPriceItems.length > 0
@@ -374,6 +386,7 @@ const SalesReportTable = ({
/> />
{/* Summary Row */} {/* Summary Row */}
{salesBroilerData.length > 0 && (
<table className='w-full table-auto text-sm mt-0'> <table className='w-full table-auto text-sm mt-0'>
<tbody> <tbody>
<tr className='bg-gray-100 font-semibold'> <tr className='bg-gray-100 font-semibold'>
@@ -422,6 +435,7 @@ const SalesReportTable = ({
</tr> </tr>
</tbody> </tbody>
</table> </table>
)}
</Card> </Card>
</div> </div>
), ),