Zastosowanie wszystkich poprawek UI i logiki kalkulatora
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import { getProducts } from './products';
|
||||
|
||||
export interface VariantOption {
|
||||
slug: string;
|
||||
moc?: string;
|
||||
moc_float?: number;
|
||||
wykończenie?: string;
|
||||
cena: number;
|
||||
}
|
||||
|
||||
export interface ProductVariants {
|
||||
series: string;
|
||||
currentMoc: string;
|
||||
currentWykończenie: string;
|
||||
moce: VariantOption[];
|
||||
wykończenia: VariantOption[];
|
||||
}
|
||||
|
||||
export function getProductVariants(slug: string): ProductVariants | null {
|
||||
const products = getProducts();
|
||||
const currentProduct = products.find(p => p.slug === slug);
|
||||
if (!currentProduct) return null;
|
||||
|
||||
const skuParts = currentProduct.sku.split('-');
|
||||
if (skuParts.length < 2) return null;
|
||||
const series = skuParts.slice(0, -1).join('-');
|
||||
|
||||
const seriesProducts = products.filter(p => p.sku.startsWith(series + '-'));
|
||||
|
||||
const currentMoc = currentProduct.specs['Moc chłodnicza'] || '';
|
||||
const currentWykończenie = currentProduct.specs['Wykończenie'] || '';
|
||||
|
||||
const moceProducts = seriesProducts.filter(p => (p.specs['Wykończenie'] || '') === currentWykończenie);
|
||||
const moce = moceProducts
|
||||
.filter(p => p.specs['Moc chłodnicza'] && p.price)
|
||||
.map(p => {
|
||||
const m = p.specs['Moc chłodnicza'] as string;
|
||||
return {
|
||||
slug: p.slug,
|
||||
moc: m,
|
||||
moc_float: parseFloat(m.replace(',', '.')),
|
||||
cena: p.price as number,
|
||||
};
|
||||
})
|
||||
.sort((a, b) => (a.moc_float || 0) - (b.moc_float || 0));
|
||||
|
||||
const wykończeniaProducts = seriesProducts.filter(p => (p.specs['Moc chłodnicza'] || '') === currentMoc);
|
||||
let wykończenia = wykończeniaProducts
|
||||
.filter(p => p.specs['Wykończenie'] && p.price)
|
||||
.map(p => ({
|
||||
slug: p.slug,
|
||||
wykończenie: p.specs['Wykończenie'] as string,
|
||||
cena: p.price as number,
|
||||
}));
|
||||
|
||||
|
||||
const allFinishesInSeries = new Set(seriesProducts.map(p => p.specs['Wykończenie'] || '').filter(Boolean));
|
||||
if (allFinishesInSeries.size <= 1) {
|
||||
wykończenia = [];
|
||||
}
|
||||
|
||||
return {
|
||||
series,
|
||||
currentMoc,
|
||||
currentWykończenie,
|
||||
moce: moce.length > 1 ? moce : [],
|
||||
wykończenia,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user