"use client"; import React, { useState, useEffect } from "react"; import { ChevronDown, Search, X } from "lucide-react"; import { fetchServices, searchBusinesses } from "../api/index"; // Keep the same interfaces from SearchSection interface LocationResult { place_id: number; name: string; display_name: string; lat: string; lon: string; osm_type?: string; osm_id?: number; class?: string; type?: string; boundingbox?: string[]; } interface ServiceResult { id?: number; name: string; slug?: string; } interface BusinessProfile { name: string; slug: string; city: string; user: { name: string; phone: string; }; } interface SearchResults { message: string; profiles: BusinessProfile[]; } const jobs = [ "Plumber", "Electrician", "Carpenter", "Painter", "Gardener", "Mechanic", "Cleaner", ] as const; export const ServiceSearchSection = () => { const [currentJobIndex, setCurrentJobIndex] = useState(0); const [isAnimating, setIsAnimating] = useState(false); const [showLocations, setShowLocations] = useState(false); const [showServices, setShowServices] = useState(false); const [selectedCity, setSelectedCity] = useState(""); const [searchText, setSearchText] = useState(""); const [locationSearchInput, setLocationSearchInput] = useState(""); const [locationResults, setLocationResults] = useState([]); const [serviceResults, setServiceResults] = useState([]); const [isLoading, setIsLoading] = useState(false); const [selectedLocation, setSelectedLocation] = useState<{ name: string; lat: string; lon: string; } | null>(null); const [searchResults, setSearchResults] = useState(null); const [showModal, setShowModal] = useState(false); // Function to fetch locations from OpenStreetMap API (same as original) const fetchLocations = async (query: string): Promise => { try { const response = await fetch( `https://nominatim.openstreetmap.org/search.php?q=${encodeURIComponent( query )}&countrycodes=in&accept-language=en&format=json` ); if (!response.ok) { throw new Error("Failed to fetch locations"); } return await response.json(); } catch (error) { console.error("Error fetching locations:", error); return []; } }; // Same effects for location search, service search, and job animation useEffect(() => { if (locationSearchInput.trim().length < 2) { setLocationResults([]); return; } const timer = setTimeout(() => { fetchLocations(locationSearchInput).then(setLocationResults); }, 500); return () => clearTimeout(timer); }, [locationSearchInput]); useEffect(() => { if (searchText.trim().length < 2) { setServiceResults([]); setShowServices(false); return; } setIsLoading(true); const timer = setTimeout(async () => { try { const response = await fetchServices(searchText); console.log("Service response:", response); const results = response.data ? response.data.map((item: string) => ({ name: item })) : response.map((item: any) => typeof item === "string" ? { name: item } : item ); setServiceResults(results); setShowServices(true); } catch (error) { console.error("Error fetching services:", error); setServiceResults([]); } finally { setIsLoading(false); } }, 500); return () => clearTimeout(timer); }, [searchText]); useEffect(() => { const interval = setInterval(() => { setIsAnimating(true); setTimeout(() => { setCurrentJobIndex((prev) => (prev + 1) % jobs.length); setIsAnimating(false); }, 500); }, 3000); return () => clearInterval(interval); }, []); // Close dropdowns when clicking outside useEffect(() => { const handleClickOutside = () => { setShowLocations(false); setShowServices(false); }; document.addEventListener("click", handleClickOutside); return () => { document.removeEventListener("click", handleClickOutside); }; }, []); // Handle the combined search const handleCombinedSearch = async () => { if (selectedLocation && searchText.trim()) { try { setIsLoading(true); setShowServices(false); const results = await searchBusinesses(searchText, { lat: selectedLocation.lat, lon: selectedLocation.lon, }); console.log("Search results:", results); if (results?.data?.profiles) { setSearchResults(results.data); setShowModal(true); } } catch (error) { console.error("Error searching businesses:", error); } finally { setIsLoading(false); } } }; return (
{/* Location Input - White theme */}
e.stopPropagation()} > { setLocationSearchInput(e.target.value); setShowLocations(true); }} onClick={(e) => { e.stopPropagation(); setShowLocations(true); }} /> { e.stopPropagation(); setShowLocations(!showLocations); }} />
{/* Location Dropdown - White theme */} {showLocations && (
e.stopPropagation()} >
{locationResults.length > 0 ? ( locationResults.map((location) => (
{ setSelectedCity(location.name); setSelectedLocation({ name: location.name, lat: location.lat, lon: location.lon, }); setLocationSearchInput(location.name); setShowLocations(false); }} > {location.display_name}
)) ) : locationSearchInput.trim().length > 0 ? (
No results found
) : (
Type to search locations
)}
)}
{/* Search Input - White theme */}
{ setSearchText(e.target.value); if (e.target.value.trim().length >= 2) { setShowServices(true); } }} onClick={(e) => { e.stopPropagation(); if (searchText.trim().length >= 2) { setShowServices(true); } }} /> {!searchText && (
Search for {jobs[currentJobIndex]}
)} {/* Search Button - Fixed to always show when there's text */} {searchText.trim().length >= 2 && !isLoading ? ( ) : searchText.trim().length >= 2 && isLoading ? (
) : (
)} {/* Service Results Dropdown - White theme */} {showServices && searchText.trim().length >= 2 && (
e.stopPropagation()} >
{serviceResults.length > 0 ? ( serviceResults.map((service, index) => (
{ setSearchText(service.name); setShowServices(false); }} > {service.name}
)) ) : isLoading ? (
Searching...
) : (
No results found
)}
)}
{/* Search Results Modal - White theme */} {showModal && searchResults && (
e.stopPropagation()} >

Search Results

{searchResults.profiles.length > 0 ? (
{searchResults.profiles.map((profile, index) => (

{profile.name}

📍 {profile.city}
#{index + 1}
👤 {profile.user.name}
📞 {profile.user.phone}
View Profile
))}
) : (
No profiles found matching your search criteria.
)}
)}
); }; export default ServiceSearchSection;