Deploy
This commit is contained in:
@@ -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<string, string> = { 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<MedusaProduct[]>([])
|
||||
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 (
|
||||
<section className="py-20">
|
||||
<div className="container mx-auto px-4">
|
||||
<div className="grid grid-cols-2 md:grid-cols-4 gap-6">
|
||||
{Array.from({ length: 4 }).map((_, i) => (
|
||||
<div key={i} className="aspect-square rounded-xl bg-muted animate-pulse" />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
const featured = products.slice(0, 4)
|
||||
if (!featured.length) return null
|
||||
|
||||
return (
|
||||
<section data-vula-section="mostpopular" className="py-20">
|
||||
<div className="container mx-auto px-4">
|
||||
<div className="flex items-center justify-between mb-10">
|
||||
<div className="flex items-center gap-3">
|
||||
<TrendingUp className="h-6 w-6 text-primary" />
|
||||
<div>
|
||||
<p className="text-xs uppercase tracking-widest text-primary font-semibold mb-1">Best Sellers</p>
|
||||
<h2 className="text-3xl font-bold text-foreground">Most Popular</h2>
|
||||
</div>
|
||||
</div>
|
||||
<Link href="/products" className="text-sm font-semibold text-primary hover:underline underline-offset-4 hidden md:inline-flex items-center gap-1">View all →</Link>
|
||||
</div>
|
||||
<div className="grid grid-cols-2 lg:grid-cols-4 gap-6">
|
||||
{featured.map((product, idx) => {
|
||||
const variant = product.variants?.[0]
|
||||
const price = variant?.prices?.[0]
|
||||
return (
|
||||
<div key={product.id} className="group bg-card rounded-xl overflow-hidden border border-border hover:shadow-md transition-all relative">
|
||||
{idx < 2 && (
|
||||
<span className="absolute top-2 left-2 z-10 bg-primary text-primary-foreground text-xs font-semibold px-2 py-0.5 rounded-full">
|
||||
#{ idx + 1 } Best Seller
|
||||
</span>
|
||||
)}
|
||||
<Link href={`/products/${product.id}`} className="block overflow-hidden">
|
||||
<div className="relative aspect-square bg-muted">
|
||||
{product.thumbnail ? (
|
||||
<img src={product.thumbnail} alt={product.title || ''} className="w-full h-full object-cover group-hover:scale-105 transition-transform duration-300" />
|
||||
) : (
|
||||
<div className="w-full h-full flex items-center justify-center">
|
||||
<ShoppingBag className="h-8 w-8 text-muted-foreground/30" />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</Link>
|
||||
<div className="p-4">
|
||||
<Link href={`/products/${product.id}`}>
|
||||
<p className="text-sm font-semibold text-foreground line-clamp-2 mb-1 hover:text-primary transition-colors">{product.title}</p>
|
||||
</Link>
|
||||
{price && (
|
||||
<p className="text-sm font-bold text-primary mb-3">{fmt(price.amount, price.currency_code)}</p>
|
||||
)}
|
||||
<button
|
||||
onClick={() => { if (!variant?.id) return; addToCart(variant.id, 1); window.dispatchEvent(new CustomEvent('cart-drawer-open')) }}
|
||||
disabled={!variant?.id}
|
||||
className="w-full text-xs bg-primary/10 text-primary hover:bg-primary hover:text-primary-foreground py-2 rounded-lg transition-colors font-medium disabled:opacity-40"
|
||||
>
|
||||
Add to Cart
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user