From a75fae22335553fd5db2f90da860c68795483706 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Sat, 22 Aug 2026 16:10:03 +0700 Subject: [PATCH] feat: add normalizePrices function to standardize price data and update shared prices initialization --- .../js/pages/admin/master/product/create.tsx | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/resources/js/pages/admin/master/product/create.tsx b/resources/js/pages/admin/master/product/create.tsx index 3990cf8..80c88bb 100644 --- a/resources/js/pages/admin/master/product/create.tsx +++ b/resources/js/pages/admin/master/product/create.tsx @@ -51,6 +51,15 @@ function createEmptyPrices(): Array<{ type: string; price: number }> { return PRICE_TYPES.map((pt) => ({ type: pt.key, price: 0 })); } +function normalizePrices( + prices: Array<{ type: string; price: number }>, +): Array<{ type: string; price: number }> { + return PRICE_TYPES.map((pt) => { + const existing = prices.find((p) => p.type === pt.key); + return { type: pt.key, price: existing?.price ?? 0 }; + }); +} + type VariantState = { name: string; stock: number; @@ -78,7 +87,11 @@ export default function ProductCreate({ categories }: Props) { ); const [sharedPrices, setSharedPrices] = useState< Array<{ type: string; price: number }> - >(draft?.sharedPrices ?? createEmptyPrices()); + >( + draft?.sharedPrices + ? normalizePrices(draft.sharedPrices) + : createEmptyPrices(), + ); const [variants, setVariants] = useState(() => { if (draft?.variants && draft.variants.length > 0) { return draft.variants.map((v) => { @@ -92,6 +105,7 @@ export default function ProductCreate({ categories }: Props) { ...v, photos, uploading: false, + prices: normalizePrices(v.prices), }; }); }