import React, { useState, useEffect } from 'react'; import { Container, Typography, TextField, Button, Box, Grid, Card, CardContent, CardMedia, Rating, IconButton } from '@mui/material'; import MyLocationIcon from '@mui/icons-material/MyLocation'; import SearchIcon from '@mui/icons-material/Search'; const Home = () => { const [restaurants, setRestaurants] = useState([]); const [location, setLocation] = useState(null); const [searchQuery, setSearchQuery] = useState(''); useEffect(() => { fetchRestaurants(); }, []); const fetchRestaurants = async (lat, lng) => { let url = '/restaurants'; if (lat && lng) { url += `?lat=${lat}&lng=${lng}`; } try { const response = await fetch(url); if (response.ok) { const data = await response.json(); setRestaurants(data); } } catch (error) { console.error('Error fetching restaurants:', error); } }; const handleUseCurrentLocation = () => { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition( (position) => { const { latitude, longitude } = position.coords; setLocation({ latitude, longitude }); fetchRestaurants(latitude, longitude); }, (error) => { console.error('Error getting location:', error); } ); } else { alert('Geolocation is not supported by this browser.'); } }; const handleSearch = () => { if (!searchQuery) { fetchRestaurants(location?.latitude, location?.longitude); return; } const filtered = restaurants.filter(r => r.name.toLowerCase().includes(searchQuery.toLowerCase())); setRestaurants(filtered); }; return ( Find the Best Lunch Deals Near You Discover restaurants with amazing lunch specials shared by the community. setSearchQuery(e.target.value)} /> {restaurants.map((restaurant) => ( {restaurant.name} {restaurant.address} ({restaurant.user_rating}/10) ${restaurant.min_price} - ${restaurant.max_price} ))} ); }; export default Home;