"use client"; import React, { useContext, useState } from "react"; import { LocationContext } from "@/context/LocationContext"; import { coordType } from "@/types/types"; export default function LocationSearch() { const [searchLocation, setSearchLocation] = useState(""); const { setGeoLocation } = useContext(LocationContext); const [pending, setPending] = useState(false); const [error, setError] = useState(null); const handleChange = (e: React.ChangeEvent) => { setSearchLocation(e.target.value); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setPending(true); setError(null); try { const coordinates = await getLocation(searchLocation); if (coordinates) { setGeoLocation(coordinates); } } catch (error) { console.error("Error fetching location:", error); setError("Error fetching location"); } finally { setPending(false); } }; const getLocation = async ( searchLocation: string ): Promise => { try { const response = await fetch( `/api/location?location=${encodeURIComponent(searchLocation)}` ); if (!response.ok) { throw new Error("Failed to fetch the location data"); } const data = await response.json(); return data; } catch (error: any) { console.error("Error fetching location:", error); setError(error.message); return null; } }; return (
{error &&

{error}

}
); }