Deploy
This commit is contained in:
@@ -0,0 +1,105 @@
|
|||||||
|
'use client'
|
||||||
|
import { useState, useEffect } from 'react'
|
||||||
|
import { useMedusaProducts } from '@/hooks/useMedusaProducts'
|
||||||
|
import { useMedusaCart } from '@/hooks/useMedusaCart'
|
||||||
|
import Image from 'next/image'
|
||||||
|
import Link from 'next/link'
|
||||||
|
import { ShoppingCart, UtensilsCrossed } from 'lucide-react'
|
||||||
|
|
||||||
|
interface Category { id: string; name: string }
|
||||||
|
|
||||||
|
export default function MenuPage() {
|
||||||
|
const { products: menuItems, loading, error } = useMedusaProducts(100)
|
||||||
|
const { addToCart } = useMedusaCart()
|
||||||
|
const [categories, setCategories] = useState<Category[]>([])
|
||||||
|
const [activeCategory, setActiveCategory] = useState<string | null>(null)
|
||||||
|
const [adding, setAdding] = useState<string | null>(null)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
fetch('/api/medusa/store/product-categories?limit=20', {
|
||||||
|
headers: { 'x-publishable-api-key': process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY || '' },
|
||||||
|
})
|
||||||
|
.then(r => r.json())
|
||||||
|
.then(d => setCategories(d.product_categories || []))
|
||||||
|
.catch(() => {})
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
const filtered = activeCategory
|
||||||
|
? menuItems.filter(p => p.categories?.some((c: any) => c.id === activeCategory))
|
||||||
|
: menuItems
|
||||||
|
|
||||||
|
const handleAdd = async (variantId: string, itemId: string) => {
|
||||||
|
setAdding(itemId)
|
||||||
|
await addToCart(variantId, 1)
|
||||||
|
setAdding(null)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loading) return (
|
||||||
|
<div className="min-h-screen flex items-center justify-center">
|
||||||
|
<div className="text-center">
|
||||||
|
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-primary mx-auto mb-4" />
|
||||||
|
<p className="text-muted-foreground">Loading menu...</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
|
||||||
|
if (error) return (
|
||||||
|
<div className="min-h-screen flex items-center justify-center">
|
||||||
|
<p className="text-destructive">Failed to load menu. Please try again.</p>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main className="min-h-screen bg-background">
|
||||||
|
<div className="container mx-auto px-4 py-12">
|
||||||
|
<div className="flex items-center justify-between mb-8">
|
||||||
|
<h1 className="text-3xl font-bold text-foreground">Our Menu</h1>
|
||||||
|
<Link href="/order" className="flex items-center gap-2 text-muted-foreground hover:text-foreground transition-colors">
|
||||||
|
<ShoppingCart className="h-5 w-5" />
|
||||||
|
<span className="text-sm font-medium">View Order</span>
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
{categories.length > 0 && (
|
||||||
|
<div className="flex gap-2 flex-wrap mb-8">
|
||||||
|
<button onClick={() => setActiveCategory(null)} className={"px-4 py-2 rounded-full text-sm font-medium transition-colors " + (activeCategory === null ? "bg-primary text-primary-foreground" : "bg-muted text-muted-foreground hover:bg-accent")}>All</button>
|
||||||
|
{categories.map(cat => (
|
||||||
|
<button key={cat.id} onClick={() => setActiveCategory(cat.id)} className={"px-4 py-2 rounded-full text-sm font-medium transition-colors " + (activeCategory === cat.id ? "bg-primary text-primary-foreground" : "bg-muted text-muted-foreground hover:bg-accent")}>{cat.name}</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{filtered.length === 0 ? (
|
||||||
|
<div className="text-center py-20">
|
||||||
|
<UtensilsCrossed className="h-16 w-16 mx-auto mb-4 text-muted-foreground/40" />
|
||||||
|
<p className="text-muted-foreground text-lg">{activeCategory ? 'No items in this category.' : 'Menu coming soon.'}</p>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||||
|
{filtered.map((item) => {
|
||||||
|
const variant = item.variants?.[0]
|
||||||
|
const price = variant?.prices?.[0]
|
||||||
|
return (
|
||||||
|
<div key={item.id} className="bg-card rounded-xl overflow-hidden border border-border hover:shadow-lg transition-all duration-300 flex flex-col">
|
||||||
|
{item.thumbnail && (
|
||||||
|
<div className="relative aspect-video overflow-hidden bg-muted">
|
||||||
|
<Image src={item.thumbnail} alt={item.title} fill sizes="(max-width: 640px) 100vw, (max-width: 1024px) 50vw, 33vw" className="object-cover hover:scale-105 transition-transform duration-500" />
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<div className="p-4 flex flex-col flex-1">
|
||||||
|
<h2 className="font-semibold text-foreground mb-1">{item.title}</h2>
|
||||||
|
{item.description && <p className="text-sm text-muted-foreground mb-3 flex-1">{item.description}</p>}
|
||||||
|
{price && <p className="text-lg font-bold text-primary mb-3">R{(price.amount / 100).toFixed(2)}</p>}
|
||||||
|
<button
|
||||||
|
onClick={() => variant?.id && handleAdd(variant.id, item.id)}
|
||||||
|
disabled={!variant?.id || adding === item.id}
|
||||||
|
className="w-full bg-primary text-primary-foreground py-2 px-4 rounded-lg hover:bg-primary/90 transition-colors text-sm font-medium disabled:opacity-50"
|
||||||
|
>{adding === item.id ? 'Adding...' : 'Add to Order'}</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user