import { useState, useEffect, useCallback } from 'react' import { useSearchParams, Link } from 'react-router-dom' import { useTranslation } from 'react-i18next' import Navbar from '../components/Navbar' import Footer from '../components/Footer' import ProductCard from '../components/ProductCard' import EmptyState from '../components/EmptyState' import DynamicPageRenderer from '../components/DynamicPageRenderer' import { API_BASE, type Category, type Product, type PageSection } from '../types' export default function SearchPage() { const [searchParams, setSearchParams] = useSearchParams() const query = searchParams.get('q') ?? '' const [input, setInput] = useState(query) const [categories, setCategories] = useState([]) const [products, setProducts] = useState([]) const [loading, setLoading] = useState(false) const [searched, setSearched] = useState(false) const [cmsSections, setCmsSections] = useState([]) const { i18n } = useTranslation() useEffect(() => { fetch(`${API_BASE}/categories?lang=${i18n.language}`).then(r => r.json()).then(setCategories).catch(console.error) }, [i18n.language]) useEffect(() => { fetch(`${API_BASE}/pages/search-layout?lang=${i18n.language}`) .then((r) => { if (!r.ok) throw new Error('Page not found'); return r.json(); }) .then((data) => { if (data && data.sections) { setCmsSections(data.sections); } }) .catch((err) => { console.log('Search dynamic layout sections not loaded:', err.message); }); }, [i18n.language]) const doSearch = useCallback(async (q: string) => { if (!q.trim()) { setProducts([]); setSearched(false); return } setLoading(true) setSearched(true) try { const res = await fetch(`${API_BASE}/products?search=${encodeURIComponent(q)}&lang=${i18n.language}`) const paginated = await res.json() setProducts(paginated.data || []) } catch (err) { console.error(err) } finally { setLoading(false) } }, [i18n.language]) // Search when URL query changes useEffect(() => { setInput(query) doSearch(query) }, [query, doSearch]) const handleSubmit = (e: React.FormEvent) => { e.preventDefault() setSearchParams(input.trim() ? { q: input.trim() } : {}) } const announcementSections = cmsSections.filter(s => s.isActive && s.type.toUpperCase() === 'ANNOUNCEMENT_BAR'); const heroSections = cmsSections.filter(s => s.isActive && (s.type.toUpperCase() === 'HERO' || s.type.toUpperCase() === 'BANNER')); const bottomSections = cmsSections.filter(s => s.isActive && s.type.toUpperCase() !== 'ANNOUNCEMENT_BAR' && s.type.toUpperCase() !== 'HERO' && s.type.toUpperCase() !== 'BANNER'); return (
{announcementSections.length > 0 && ( )} {/* Search Header */} {heroSections.length > 0 ? (
) : (

Discover

Search Products

setInput(e.target.value)} placeholder="Search by scent name, category..." className="bg-transparent border-none outline-none text-base p-4 pl-0 w-full text-pop-black placeholder-spa-stone/60" autoFocus />
{/* Quick category links */}
{categories.map((cat) => ( {typeof cat.name === 'string' ? cat.name : (cat.name as any)?.[i18n.language] || (cat.name as any)?.en || ''} ))}
)} {/* Results */}
{!searched && !query && (

🔍

Type something to discover our collection

)} {searched && (

{loading ? 'Searching...' : `${products.length} result${products.length !== 1 ? 's' : ''} for `} {!loading && "{query}"}

)} {loading ? (
{Array.from({ length: 6 }).map((_, i) => (
))}
) : searched && products.length === 0 ? ( ) : (
{products.map((p) => )}
)}
{bottomSections.length > 0 && ( )}
) }