import { useState, useEffect } from "react"; import SuggestionList from "./components/SuggestionList"; import GroceryItem from "./components/GroceryItem"; import type { GroceryItemType } from "./types"; export default function App() { const [items, setItems] = useState([]); const [suggestions, setSuggestions] = useState([]); const [itemInput, setItemInput] = useState(""); const [quantity, setQuantity] = useState(1); // Load grocery list const loadList = async () => { const res = await fetch("/api/list"); const data: GroceryItemType[] = await res.json(); setItems(data); }; useEffect(() => { loadList(); }, []); // Suggestion logic const handleSuggest = async (query: string) => { setItemInput(query); if (!query.trim()) return setSuggestions([]); const res = await fetch("/api/suggest?query=" + encodeURIComponent(query)); const data: string[] = await res.json(); setSuggestions(data); }; // Add Item const addItem = async () => { if (!itemInput.trim() || !quantity) return; const existing = items.find( (i) => i.item_name.toLowerCase() === itemInput.toLowerCase() ); if (existing) { if (!confirm("Item already exists. Add more?")) return; await fetch("/api/add", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ item_name: itemInput, quantity: existing.quantity + quantity, }), }); } else { await fetch("/api/add", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ item_name: itemInput, quantity }), }); } setItemInput(""); setQuantity(1); setSuggestions([]); loadList(); }; // Mark bought const markBought = async (id: number) => { if (!confirm("Mark this item as bought?")) return; await fetch("/api/mark-bought", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ id }), }); loadList(); }; return (

Costco Grocery List

handleSuggest(e.target.value)} style={inputStyle} autoComplete="off" /> { setItemInput(val); setSuggestions([]); }} /> setQuantity(Number(e.target.value))} placeholder="Quantity" style={inputStyle} />
    {items.map((item) => ( ))}
); } const inputStyle: React.CSSProperties = { fontSize: "1em", margin: "0.3em 0", padding: "0.5em", width: "100%", boxSizing: "border-box", }; const buttonStyle = { ...inputStyle, cursor: "pointer", };