From be8be3b468f65fab8137f732e35cffe6def192ed Mon Sep 17 00:00:00 2001 From: Vula Builder Date: Fri, 31 Jul 2026 18:44:28 +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..9098052 --- /dev/null +++ b/src/components/shop/MostPopular.tsx @@ -0,0 +1,93 @@ +'use client' +import { useState, useEffect } from 'react' +import { useVulaCart } from '@/hooks/useVulaCart' +import type { VulaProduct } from '@/hooks/useProducts' +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 } = useVulaCart() + + 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/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)} +

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