diff --git a/app/products/[slug]/page.tsx b/app/products/[slug]/page.tsx index 28ac6f9..b001c45 100644 --- a/app/products/[slug]/page.tsx +++ b/app/products/[slug]/page.tsx @@ -331,6 +331,13 @@ export default async function ProductDetailPage({ params, searchParams }: Produc } const { product, reviews = [], related_products = [], specifications = [] } = data; + const validRatings = (reviews || []) + .map((r: any) => Number(r.rating)) + .filter((r: number) => !isNaN(r) && r > 0); + const calculatedRating = validRatings.length > 0 + ? Number((validRatings.reduce((sum: number, r: number) => sum + r, 0) / validRatings.length).toFixed(1)) + : (product.rating ? Number(product.rating) : 5.0); + const totalReviewsCount = reviews.length; const related = related_products.slice(0, 5); const specGroups = groupSpecs(specifications, product); @@ -513,10 +520,10 @@ export default async function ProductDetailPage({ params, searchParams }: Produc name: 'iFixKart TechShop', }, }, - aggregateRating: reviews.length > 0 ? { + aggregateRating: totalReviewsCount > 0 ? { '@type': 'AggregateRating', - ratingValue: '4.8', - reviewCount: String(reviews.length), + ratingValue: String(calculatedRating), + reviewCount: String(totalReviewsCount), } : undefined, }; @@ -535,7 +542,8 @@ export default async function ProductDetailPage({ params, searchParams }: Produc
diff --git a/components/pdp/ProductInteractiveGrid.tsx b/components/pdp/ProductInteractiveGrid.tsx index e2cde8d..8ff8bd4 100644 --- a/components/pdp/ProductInteractiveGrid.tsx +++ b/components/pdp/ProductInteractiveGrid.tsx @@ -35,6 +35,7 @@ interface ProductInteractiveGridProps { initialReviewsCount: number; relatedProducts?: ProductResponse[]; initialVariantSku?: string; + rating?: number; } const FALLBACK_TECH_IMAGES: string[] = ['/images/placeholder-product.webp']; @@ -87,10 +88,46 @@ export const ProductInteractiveGrid: React.FC = ({ initialReviewsCount, relatedProducts = [], initialVariantSku, + rating: propRating, }) => { const searchParams = useSearchParams(); const router = useRouter(); + const [currentRating, setCurrentRating] = useState(() => { + if (typeof propRating === 'number' && !isNaN(propRating)) return propRating; + if (product.rating !== undefined && product.rating !== null) return Number(product.rating); + return 5.0; + }); + const [currentReviewsCount, setCurrentReviewsCount] = useState(initialReviewsCount || 0); + + useEffect(() => { + if (typeof propRating === 'number' && !isNaN(propRating)) { + setCurrentRating(propRating); + } else if (product.rating !== undefined && product.rating !== null) { + setCurrentRating(Number(product.rating)); + } + }, [propRating, product.rating]); + + useEffect(() => { + setCurrentReviewsCount(initialReviewsCount || 0); + }, [initialReviewsCount]); + + useEffect(() => { + const handleUpdate = (e: Event) => { + const customEvent = e as CustomEvent; + if (customEvent.detail && customEvent.detail.productId === product.product_id) { + if (typeof customEvent.detail.averageRating === 'number') { + setCurrentRating(customEvent.detail.averageRating); + } + if (typeof customEvent.detail.totalReviews === 'number') { + setCurrentReviewsCount(customEvent.detail.totalReviews); + } + } + }; + window.addEventListener('ifixkart:reviews-summary-updated', handleUpdate); + return () => window.removeEventListener('ifixkart:reviews-summary-updated', handleUpdate); + }, [product.product_id]); + const getInitialIndex = () => { const key = initialVariantSku || searchParams?.get('variant') || searchParams?.get('sku'); if (key && product?.variants?.length) { @@ -553,12 +590,12 @@ export const ProductInteractiveGrid: React.FC = ({ ))}
- {Number(product.rating || 5.0).toFixed(1)} + {Number(currentRating).toFixed(1)} • diff --git a/components/pdp/ProductReviewsSection.tsx b/components/pdp/ProductReviewsSection.tsx index 9869fbc..d031d79 100644 --- a/components/pdp/ProductReviewsSection.tsx +++ b/components/pdp/ProductReviewsSection.tsx @@ -32,6 +32,17 @@ export function ProductReviewsSection({ .then((res) => { setData(res); if (onReviewsCountChange) onReviewsCountChange(res.total_reviews); + if (typeof window !== 'undefined') { + window.dispatchEvent( + new CustomEvent('ifixkart:reviews-summary-updated', { + detail: { + productId, + averageRating: res.average_rating, + totalReviews: res.total_reviews, + }, + }) + ); + } }) .finally(() => { setIsLoading(false); @@ -67,11 +78,23 @@ export function ProductReviewsSection({ const starKey = String(newReview.rating); const newDist = { ...(prev?.star_distribution || {}) }; newDist[starKey] = (newDist[starKey] || 0) + 1; + const calculatedAvg = prev ? ((prev.average_rating * prev.total_reviews) + newReview.rating) / newTotal : newReview.rating; if (onReviewsCountChange) onReviewsCountChange(newTotal); + if (typeof window !== 'undefined') { + window.dispatchEvent( + new CustomEvent('ifixkart:reviews-summary-updated', { + detail: { + productId, + averageRating: Number(calculatedAvg.toFixed(1)), + totalReviews: newTotal, + }, + }) + ); + } return { - average_rating: prev ? ((prev.average_rating * prev.total_reviews) + newReview.rating) / newTotal : newReview.rating, + average_rating: calculatedAvg, total_reviews: newTotal, star_distribution: newDist, verified_count: (prev?.verified_count || 0) + (newReview.verified_buyer ? 1 : 0),