import React from 'react' import "leaflet/dist/leaflet.css" import { MapContainer, TileLayer, Marker, Popup, useMapEvents } from "react-leaflet" import Navbar from "./Navbar" import L from "leaflet"; import pin from "../images/pin.png"; import { useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; const Home = () => { const [pins, setPins] = useState([]); const [username, setUsername] = useState(''); const [isLoggedIn, setIsLoggedIn] = useState(false); const [pinMode, setPinMode] = useState(false); const [showPinForm, setShowPinForm] = useState(false); const [currentPinLocation, setCurrentPinLocation] = useState(null); const [pinDetails, setPinDetails] = useState({ pin_name: '', description: '', category: '', public: false, x: 0, y: 0 }); // Fetch username and login status from localStorage useEffect(() => { const storedUsername = localStorage.getItem('username'); const storedLoginStatus = localStorage.getItem('login'); if (storedUsername && storedLoginStatus === 'true') { setUsername(storedUsername); setIsLoggedIn(true); fetchPins(storedUsername); } else { fetchPins(null); } }, []); // Fetch pins from server const fetchPins = (username) => { fetch(`https://jp-backend-kc80.onrender.com/api/pins`) .then(response => response.json()) .then(data => { // Filter pins: show public ones OR private ones belonging to the logged-in user const filteredPins = data.filter(pin => pin.public || (username && pin.username === username) ); const formattedPins = filteredPins.map(pin => ({ ...pin, lat: pin.y, lng: pin.x })); setPins(formattedPins); }) .catch(error => console.error("Error fetching pins:", error)); }; // Handle form input changes const handleInputChange = (e) => { const { name, value, type, checked } = e.target; setPinDetails(prev => ({ ...prev, [name]: type === 'checkbox' ? checked : value })); }; // Function to handle pin placement const MapClickHandler = () => { useMapEvents({ click: (e) => { const { lat, lng } = e.latlng; setCurrentPinLocation({ lat, lng }); setPinDetails(prev => ({ ...prev, x: lng, y: lat })); setShowPinForm(true); }, }); return null; }; // Function to save pin with details const savePinWithDetails = () => { if (!currentPinLocation || !username) return; const newPin = { ...pinDetails, username: username, x: currentPinLocation.lng, y: currentPinLocation.lat, lat: currentPinLocation.lat, lng: currentPinLocation.lng }; // Send to backend using same URL format as GET fetch(`https://jp-backend-kc80.onrender.com/api/pins?username=${username}`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ pin_name: pinDetails.pin_name, username: username, x: currentPinLocation.lng, y: currentPinLocation.lat, description: pinDetails.description, category: pinDetails.category, public: pinDetails.public }), }) .then((response) => response.json()) .then((data) => { console.log("Pin saved:", data); // Update local state only after successful save setPins((prevPins) => [...prevPins, newPin]); setShowPinForm(false); setPinDetails({ pin_name: '', description: '', category: '', public: false, x: 0, y: 0 }); // Refresh pins from server fetchPins(username); }) .catch((error) => { console.error("Error saving pin:", error); alert("Failed to save pin. Please try again."); }); }; // Custom pin icon const pinIcon = new L.Icon({ iconUrl: pin, iconSize: [24, 40], iconAnchor: [15, 40], popupAnchor: [0, -40], }); return (
X (Longitude): {pinDetails.x.toFixed(5)}
Y (Latitude): {pinDetails.y.toFixed(5)}