import React, { useState, useEffect, useRef } from "react"; import Navbar from "./Navbar"; import Footer from "./footer"; import "leaflet/dist/leaflet.css"; const Pin = () => { const [suggestions, setSuggestions] = useState([]); const [selectedLocation, setSelectedLocation] = useState(null); const [ratings, setRatings] = useState({}); const [ratingInput, setRatingInput] = useState(0); const [commentInput, setCommentInput] = useState(""); const commentsContainerRef = useRef(null); const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); const handleInputChange = async (e) => { const value = e.target.value; if (value.length >= 1) { try { await delay(2000); const response = await fetch( `https://photon.komoot.io/api/?q=${value}&limit=5` ); const data = await response.json(); setSuggestions(data.features); } catch (error) { console.error("Error fetching data from Photon:", error); } } else { setSuggestions([]); } }; const handleSuggestionClick = (suggestion) => { const addressString = [ suggestion.properties.name, suggestion.properties.city, suggestion.properties.postcode, suggestion.properties.country, ] .filter((part) => part) .join(", "); setSelectedLocation({ id: suggestion.properties.osm_id || suggestion.properties.osm_type, name: addressString, }); setSuggestions([]); }; const handleRatingSubmit = async () => { if (selectedLocation) { const locationName = selectedLocation.name; const userName = localStorage.getItem('username'); try { const response = await fetch('https://jp-backend-kc80.onrender.com/api/reviews', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ location: locationName, rating: ratingInput, comment: commentInput, username: userName, }), }); if (response.ok) { fetchRatings(); setRatingInput(0); setCommentInput(""); } else { console.error('Failed to submit rating:', response.statusText); } } catch (error) { console.error('Error submitting rating:', error); } } }; const fetchRatings = async () => { try { const response = await fetch('https://jp-backend-kc80.onrender.com/api/reviews'); if (response.ok) { const data = await response.json(); const groupedRatings = data.reduce((acc, review) => { if (!acc[review.location]) { acc[review.location] = []; } acc[review.location].push(review); return acc; }, {}); setRatings(groupedRatings); } else { console.error('Failed to fetch ratings:', response.statusText); } } catch (error) { console.error('Error fetching ratings:', error); } }; useEffect(() => { fetchRatings(); }, []); const calculateAverageRating = (locationName) => { const locationRatings = ratings[locationName] || []; if (locationRatings.length === 0) return 0; const total = locationRatings.reduce((sum, entry) => sum + entry.rating, 0); return (total / locationRatings.length).toFixed(1); }; const sortedLocations = Object.keys(ratings) .map((locationName) => ({ name: locationName, averageRating: calculateAverageRating(locationName), reviews: ratings[locationName], })) .sort((a, b) => b.averageRating - a.averageRating); return (