import { useEffect, useState } from "preact/hooks"; import { getMenuItemsService } from "../../services/locations"; export interface MenuItemRow { id: number; location_id: number; name: string; price: number; category: string; description: string; is_available: boolean; submitted_by: number; avg_score: number | null; } const CATEGORY_LABELS: Record = { appetizer: 'Appetizer', main_course: 'Main Course', dessert: 'Dessert', beverages: 'Beverages', snack: 'Snack', }; function formatPrice(price: number): string { return 'Rp' + price.toLocaleString('id-ID'); } function getRatingColor(rating: number): string { if (rating >= 70) return '#3ba55d'; if (rating >= 40) return '#faa61a'; return '#ed4245'; } interface Props { locationId: number; locationName: string; onClose: () => void; } export default function MenuPopup({ locationId, locationName, onClose }: Props) { const [items, setItems] = useState([]); const [loading, setLoading] = useState(true); const [activeCategory, setActiveCategory] = useState('all'); const [search, setSearch] = useState(''); useEffect(() => { getMenuItemsService(locationId).then(res => { if (res.data) setItems(res.data); setLoading(false); }); }, [locationId]); const categories = ['all', ...Array.from(new Set(items.map(i => i.category).filter(Boolean)))]; const filtered = items.filter(item => { const matchCat = activeCategory === 'all' || item.category === activeCategory; const matchSearch = item.name.toLowerCase().includes(search.toLowerCase()); return matchCat && matchSearch; }); // split into two columns const mid = Math.ceil(filtered.length / 2); const col1 = filtered.slice(0, mid); const col2 = filtered.slice(mid); return (
e.stopPropagation()}>

{locationName} Menu

setSearch((e.target as HTMLInputElement).value)} />
{categories.map(cat => ( ))}
{loading ? (

Loading...

) : filtered.length === 0 ? (

No items found.

) : (
{[col1, col2].map((col, ci) => (
Rating
{col.map(item => (
{item.name} {formatPrice(item.price)} {item.avg_score != null ? ( {Math.round(item.avg_score)} ) : ( )}
))}
))}
)}
); }