Skip to content

Commit 43547f1

Browse files
committed
fix: fixed price info
1 parent 3bb7ac8 commit 43547f1

File tree

4 files changed

+60
-17
lines changed

4 files changed

+60
-17
lines changed

src/app/product/[id]/page.tsx

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -324,18 +324,41 @@ export default function ProductPage({ params }: { params: Promise<{ id: string }
324324

325325
// Calculate price statistics when similar products change
326326
useEffect(() => {
327-
if (!product || !(sameProducts.length - 1)) return;
327+
if (!product) return;
328328

329329
const getPrice = (p: Product) => p.price || p.expectedPrice || 0;
330-
const prices = [...sameProducts.map(getPrice), getPrice(product)].filter(p => p > 0);
331-
332-
if (prices.length) {
330+
const currentProductPrice = getPrice(product);
331+
332+
// If we have similar products, include them in the calculation
333+
if (sameProducts.length > 1) {
334+
const prices = sameProducts.map(getPrice).filter(p => p > 0);
335+
336+
if (prices.length > 0) {
337+
setPriceStats({
338+
min: Math.min(...prices),
339+
max: Math.max(...prices),
340+
q1: calculateQuartile(prices, 0.25),
341+
median: calculateQuartile(prices, 0.5),
342+
q3: calculateQuartile(prices, 0.75)
343+
});
344+
} else if (currentProductPrice > 0) {
345+
// If no similar products have prices, use current product as baseline
346+
setPriceStats({
347+
min: currentProductPrice,
348+
max: currentProductPrice,
349+
q1: currentProductPrice,
350+
median: currentProductPrice,
351+
q3: currentProductPrice
352+
});
353+
}
354+
} else if (currentProductPrice > 0) {
355+
// If no similar products, use current product as sole data point
333356
setPriceStats({
334-
min: Math.min(...prices),
335-
max: Math.max(...prices),
336-
q1: calculateQuartile(prices, 0.25),
337-
median: calculateQuartile(prices, 0.5),
338-
q3: calculateQuartile(prices, 0.75)
357+
min: currentProductPrice,
358+
max: currentProductPrice,
359+
q1: currentProductPrice,
360+
median: currentProductPrice,
361+
q3: currentProductPrice
339362
});
340363
}
341364
}, [sameProducts, product]);

src/components/PriceSpectrum.tsx

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const PriceSpectrum: React.FC<PriceSpectrumProps> = ({
5252
const productPrice = getPrice(product);
5353

5454
// If no real prices available, show simple message
55-
if (productPrice === 0) {
55+
if (productPrice === 0 && priceStats.min === 0 && priceStats.max === 0) {
5656
return (
5757
<div className="w-full h-24 flex items-center justify-center text-sm" style={{
5858
color: colours.text.secondary
@@ -62,12 +62,32 @@ const PriceSpectrum: React.FC<PriceSpectrumProps> = ({
6262
);
6363
}
6464

65+
// If current product has no price but we have price statistics, show different message
66+
if (productPrice === 0 && (priceStats.min > 0 || priceStats.max > 0)) {
67+
return (
68+
<div className="w-full h-24 flex flex-col items-center justify-center text-sm" style={{
69+
color: colours.text.secondary
70+
}}>
71+
<div>Price not available for this product</div>
72+
<div className="mt-1 text-xs">
73+
Similar products range: £{priceStats.min.toFixed(2)} - £{priceStats.max.toFixed(2)}
74+
</div>
75+
</div>
76+
);
77+
}
78+
79+
// Ensure we have valid price range for scaling
80+
const minPrice = Math.max(priceStats.min, 0.01); // Avoid division by zero
81+
const maxPrice = Math.max(priceStats.max, productPrice, 0.01);
82+
6583
// Convert prices to position percentages for visualization, accounting for 5% padding on each side
6684
const scale = (price: number) => {
67-
if (price === priceStats.min) return 5; // Align with left edge of spectrum
68-
if (price === priceStats.max) return 95; // Align with right edge of spectrum
85+
if (price === minPrice) return 5; // Align with left edge of spectrum
86+
if (price === maxPrice) return 95; // Align with right edge of spectrum
6987
// Scale between 5% and 95% for other values
70-
return 5 + ((price - priceStats.min) / (priceStats.max - priceStats.min)) * 90;
88+
const range = maxPrice - minPrice;
89+
if (range === 0) return 50; // If all prices are the same, center it
90+
return 5 + ((price - minPrice) / range) * 90;
7191
};
7292

7393
// Calculate the position of the current product's price
@@ -148,7 +168,7 @@ const PriceSpectrum: React.FC<PriceSpectrumProps> = ({
148168
}}
149169
onClick={onMinClick}
150170
>
151-
£{priceStats.min.toFixed(2)}
171+
£{minPrice.toFixed(2)}
152172
</button>
153173
</div>
154174
<div className="absolute -bottom-4 right-[0%]">
@@ -161,7 +181,7 @@ const PriceSpectrum: React.FC<PriceSpectrumProps> = ({
161181
}}
162182
onClick={onMaxClick}
163183
>
164-
£{priceStats.max.toFixed(2)}
184+
£{maxPrice.toFixed(2)}
165185
</button>
166186
</div>
167187
</div>

src/components/TabbedInfoBox.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ const TabbedInfoBox: React.FC<TabbedInfoBoxProps> = ({
333333
transition: 'opacity 0.2s ease-in-out, height 0.3s ease-in-out'
334334
}}
335335
>
336-
{activeTab === "Price" && reviewSummary && (
336+
{activeTab === "Price" && (
337337
<PriceTabContent
338338
product={product}
339339
reviewSummary={reviewSummary}

src/components/tabs/PriceTabContent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { colours } from "@/styles/colours";
88

99
interface PriceTabContentProps {
1010
product: Product;
11-
reviewSummary: ReviewSummary;
11+
reviewSummary?: ReviewSummary | null;
1212
priceStats: {
1313
min: number;
1414
max: number;

0 commit comments

Comments
 (0)