From 7a3535c6191dd80732aea158ce98da5e86dc4f89 Mon Sep 17 00:00:00 2001 From: Vula Builder Date: Mon, 27 Jul 2026 18:06:16 +0000 Subject: [PATCH] Deploy --- .../sections/MostPopularSection.tsx | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/components/sections/MostPopularSection.tsx diff --git a/src/components/sections/MostPopularSection.tsx b/src/components/sections/MostPopularSection.tsx new file mode 100644 index 0000000..30b1ac4 --- /dev/null +++ b/src/components/sections/MostPopularSection.tsx @@ -0,0 +1,105 @@ +'use client' +import { useState, useEffect } from 'react' +import { useMedusaCart } from '@/hooks/useMedusaCart' +import type { MedusaProduct } from '@/hooks/useMedusaProducts' +import Link from 'next/link' +import { ShoppingBag, TrendingUp } from 'lucide-react' + +const CURRENCY_SYMBOLS: Record = { zar: 'R', usd: '$', eur: '€', gbp: '£', kes: 'KSh', ngn: '₦', ghs: '₵' } +const fmt = (amount: number, code?: string) => { + const sym = CURRENCY_SYMBOLS[(code ?? '').toLowerCase()] ?? (code?.toUpperCase() ?? '') + return `${sym}${(amount / 100).toFixed(2)}` +} + +export default function MostPopularSection() { + const [products, setProducts] = useState([]) + const [loading, setLoading] = useState(true) + const { addToCart } = useMedusaCart() + + useEffect(() => { + fetch('/api/popular-products?limit=8') + .then(r => r.ok ? r.json() : Promise.reject(r.status)) + .then(d => { setProducts(d.products ?? []); setLoading(false) }) + .catch(() => { + fetch('/api/medusa/products?limit=8') + .then(r => r.json()) + .then(d => { setProducts(d.products ?? []); setLoading(false) }) + .catch(() => setLoading(false)) + }) + }, []) + + if (loading) { + return ( +
+
+
+ {Array.from({ length: 4 }).map((_, i) => ( +
+ ))} +
+
+
+ ) + } + + const featured = products.slice(0, 4) + if (!featured.length) return null + + return ( +
+
+
+
+ +
+

Best Sellers

+

Most Popular

+
+
+ View all → +
+
+ {featured.map((product, idx) => { + const variant = product.variants?.[0] + const price = variant?.prices?.[0] + return ( +
+ {idx < 2 && ( + + #{ idx + 1 } Best Seller + + )} + +
+ {product.thumbnail ? ( + {product.title + ) : ( +
+ +
+ )} +
+ +
+ +

{product.title}

+ + {price && ( +

{fmt(price.amount, price.currency_code)}

+ )} + +
+
+ ) + })} +
+
+
+ ) +}