From 20fe5e2cf693a834e2c9eb9cc6fef4b105102b56 Mon Sep 17 00:00:00 2001 From: Vula Builder Date: Tue, 28 Jul 2026 12:39:41 +0000 Subject: [PATCH] Deploy --- src/components/shop/MostPopular.tsx | 93 +++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/components/shop/MostPopular.tsx diff --git a/src/components/shop/MostPopular.tsx b/src/components/shop/MostPopular.tsx new file mode 100644 index 0000000..06906b8 --- /dev/null +++ b/src/components/shop/MostPopular.tsx @@ -0,0 +1,93 @@ +'use client' +import { useState, useEffect } from 'react' +import { useMedusaCart } from '@/hooks/useMedusaCart' +import type { MedusaProduct } from '@/hooks/useMedusaProducts' +import Link from 'next/link' +import Image from 'next/image' +import { ShoppingBag, TrendingUp } from 'lucide-react' + +const CURRENCY_SYMBOLS: Record = { zar: 'R', usd: '$', eur: '€', gbp: '£', kes: 'KSh', ngn: '₦', ghs: '₵' } +const formatPrice = (amount: number, code?: string) => { + const sym = CURRENCY_SYMBOLS[(code ?? '').toLowerCase()] ?? (code?.toUpperCase() ?? '') + return `${sym}${(amount / 100).toFixed(2)}` +} + +export default function MostPopular() { + 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(() => { + // fallback to latest products + fetch('/api/medusa/products?limit=8') + .then(r => r.json()) + .then(d => { setProducts(d.products ?? []); setLoading(false) }) + .catch(() => setLoading(false)) + }) + }, []) + + const featured = products.slice(0, 4) + + if (loading) return ( +
+
+
+ ) + if (featured.length === 0) return null + + return ( +
+
+ +

Popular Picks

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

{product.title}

+ + {price && ( +

+ {formatPrice(price.amount, price.currency_code)} +

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