import React, { useEffect, useMemo, useState } from 'react'; import { IonButton, IonCard, IonCardContent, IonChip, IonContent, IonHeader, IonIcon, IonPage, IonSearchbar, IonText, IonTitle, IonToolbar, useIonViewWillEnter, } from '@ionic/react'; import { bagHandleOutline, cartOutline, receiptOutline, searchOutline, star, } from 'ionicons/icons'; import { useHistory } from 'react-router-dom'; import { setStatusBarBackground, setStatusBarStyle, Style, } from '../utils/statusBar'; import avocadoOilImage from '../assets/organic-avocado-oil.png'; import sourdoughBreadImage from '../assets/artisan-sourdough-bread.png'; import strawberriesPackImage from '../assets/fresh-strawberries-pack.png'; import farmEggsImage from '../assets/farm-eggs-carton.png'; export type Product = { id: string; name: string; category: string; description: string; price: number; rating: number; prepTime: string; image: string; imageAlt: string; }; export type CartItem = { id: string; name: string; price: number; quantity: number; }; export const CART_STORAGE_KEY = 'simple-shop-cart'; export const LAST_ORDER_STORAGE_KEY = 'simple-shop-last-order'; export const products: Product[] = [ { id: '1', name: 'Organic Avocado Oil', category: 'Pantry', description: 'Cold-pressed avocado oil for cooking, roasting, and healthy everyday meals.', price: 8.99, rating: 4.8, prepTime: 'Ready to ship', image: avocadoOilImage, imageAlt: 'Bottle of organic avocado oil with fresh avocados', }, { id: '2', name: 'Artisan Sourdough Bread', category: 'Bakery', description: 'Freshly baked sourdough loaf with a crisp crust and soft, airy center.', price: 7.49, rating: 4.6, prepTime: 'Baked today', image: sourdoughBreadImage, imageAlt: 'Fresh artisan sourdough bread loaf on a wooden board', }, { id: '3', name: 'Fresh Strawberries Pack', category: 'Fruit', description: 'Sweet, bright strawberries packed fresh for snacks, desserts, and smoothies.', price: 9.25, rating: 4.9, prepTime: 'Picked fresh', image: strawberriesPackImage, imageAlt: 'Pack of fresh strawberries with berries beside it', }, { id: '4', name: 'Free Range Farm Eggs', category: 'Dairy & Eggs', description: 'Farm-fresh eggs with rich yolks, ideal for breakfast, baking, and meal prep.', price: 4.5, rating: 4.7, prepTime: 'Stocked daily', image: farmEggsImage, imageAlt: 'Carton of free range farm eggs on a clean background', }, ]; export const readCart = (): CartItem[] => { if (typeof window === 'undefined') { return []; } try { const savedCart = window.localStorage.getItem(CART_STORAGE_KEY); if (!savedCart) { return []; } const parsed = JSON.parse(savedCart) as CartItem[]; return Array.isArray(parsed) ? parsed : []; } catch { return []; } }; export const writeCart = (cart: CartItem[]) => { if (typeof window === 'undefined') { return; } window.localStorage.setItem(CART_STORAGE_KEY, JSON.stringify(cart)); window.dispatchEvent(new CustomEvent('cart:updated')); }; const Home: React.FC = () => { const history = useHistory(); const [query, setQuery] = useState(''); const [cartCount, setCartCount] = useState(0); useIonViewWillEnter(() => { setStatusBarStyle(Style.Dark); setStatusBarBackground('#fff8f2'); const count = readCart().reduce((sum, item) => sum + item.quantity, 0); setCartCount(count); }); useEffect(() => { const syncCount = () => { const count = readCart().reduce((sum, item) => sum + item.quantity, 0); setCartCount(count); }; syncCount(); window.addEventListener('storage', syncCount); window.addEventListener('cart:updated', syncCount as EventListener); return () => { window.removeEventListener('storage', syncCount); window.removeEventListener('cart:updated', syncCount as EventListener); }; }, []); const filteredProducts = useMemo(() => { const term = query.toLowerCase().trim(); return products.filter((product) => { if (!term) return true; return [product.name, product.category, product.description] .join(' ') .toLowerCase() .includes(term); }); }, [query]); return ( QuickCart history.push('/orders')} aria-label="Track orders" > history.push('/cart')} aria-label="View cart" > {cartCount}
Fast delivery

Find your next favorite meal.

Browse simple picks, add them to your cart, and place your order in seconds.

setQuery(event.detail.value ?? '')} placeholder="Search products" searchIcon={searchOutline} />

Popular products

{filteredProducts.length} items available

{filteredProducts.length === 0 ? (

No products found

Try a different search term to browse what is available.

setQuery('')}>Clear search
) : (
{filteredProducts.map((product) => ( history.push(`/product/${product.id}`)} > {product.imageAlt}
{product.category}
{product.rating}

{product.name}

{product.description}

${product.price.toFixed(2)} {product.prepTime}
))}
)}
); }; export default Home;