feat: add normalizePrices function to standardize price data and update shared prices initialization
This commit is contained in:
parent
1f61ff895a
commit
a75fae2233
@ -51,6 +51,15 @@ function createEmptyPrices(): Array<{ type: string; price: number }> {
|
|||||||
return PRICE_TYPES.map((pt) => ({ type: pt.key, price: 0 }));
|
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 = {
|
type VariantState = {
|
||||||
name: string;
|
name: string;
|
||||||
stock: number;
|
stock: number;
|
||||||
@ -78,7 +87,11 @@ export default function ProductCreate({ categories }: Props) {
|
|||||||
);
|
);
|
||||||
const [sharedPrices, setSharedPrices] = useState<
|
const [sharedPrices, setSharedPrices] = useState<
|
||||||
Array<{ type: string; price: number }>
|
Array<{ type: string; price: number }>
|
||||||
>(draft?.sharedPrices ?? createEmptyPrices());
|
>(
|
||||||
|
draft?.sharedPrices
|
||||||
|
? normalizePrices(draft.sharedPrices)
|
||||||
|
: createEmptyPrices(),
|
||||||
|
);
|
||||||
const [variants, setVariants] = useState<VariantState[]>(() => {
|
const [variants, setVariants] = useState<VariantState[]>(() => {
|
||||||
if (draft?.variants && draft.variants.length > 0) {
|
if (draft?.variants && draft.variants.length > 0) {
|
||||||
return draft.variants.map((v) => {
|
return draft.variants.map((v) => {
|
||||||
@ -92,6 +105,7 @@ export default function ProductCreate({ categories }: Props) {
|
|||||||
...v,
|
...v,
|
||||||
photos,
|
photos,
|
||||||
uploading: false,
|
uploading: false,
|
||||||
|
prices: normalizePrices(v.prices),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user