'use client'; import { useState } from "react"; import Section from "@/components/Section"; import DealCard from "@/components/DealCard"; type Result = { results: any[] }; export default function Page() { const [origin, setOrigin] = useState("YOW"); const [dest, setDest] = useState("CUN,PUJ,MBJ"); const [startDate, setStartDate] = useState(new Date().toISOString().slice(0,10)); const [endDate, setEndDate] = useState(new Date(Date.now() + 1000*60*60*24*60).toISOString().slice(0,10)); const [minN, setMinN] = useState(5); const [maxN, setMaxN] = useState(9); const [budget, setBudget] = useState(''); const [nonStop, setNonStop] = useState(false); const [loading, setLoading] = useState(false); const [results, setResults] = useState([]); const [useDeals, setUseDeals] = useState(true); const [useSky, setUseSky] = useState(true); const [useG, setUseG] = useState(true); const [useAC, setUseAC] = useState(true); const [useAT, setUseAT] = useState(true); async function onSubmit(e: React.FormEvent) { e.preventDefault(); setLoading(true); setResults([]); const payload = { origin: origin.trim().toUpperCase(), destinations: dest.split(',').map(s => s.trim().toUpperCase()).filter(Boolean), startDate, endDate, tripLengthMin: Number(minN), tripLengthMax: Number(maxN), budget: budget === '' ? null : Number(budget), currency: "CAD", nonStopOnly: nonStop, sources: [ useDeals ? "Deals" : "", useSky ? "Skyscanner" : "", useG ? "GoogleFlights" : "", useAC ? "AirCanada" : "", useAT ? "AirTransat" : "" ].filter(Boolean) }; const res = await fetch("/api/search", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(payload) }); const data: Result = await res.json(); // Sort by destination, then by source/provider const sorted = (data.results || []).sort((a: any, b: any) => { // First sort by destination const destCompare = (a.destination || '').localeCompare(b.destination || ''); if (destCompare !== 0) return destCompare; // Then by source/provider return (a.source || '').localeCompare(b.source || ''); }); setResults(sorted); setLoading(false); } return (
setOrigin(e.target.value.toUpperCase())} maxLength={4} placeholder="YOW" />
setDest(e.target.value)} placeholder="CUN,PUJ,MBJ" />
setStartDate(e.target.value)} />
setEndDate(e.target.value)} />
setMinN(Number(e.target.value))} min={1} />
setMaxN(Number(e.target.value))} min={minN} />
setBudget(e.target.value === '' ? '' : Number(e.target.value))} />
{!loading && results.length === 0 && (
No results yet. Fill the form and hit search.
)} {results.length > 0 && (() => { // Group by destination const grouped: Record = {}; results.forEach((r: any) => { const dest = r.destination || 'Unknown'; if (!grouped[dest]) grouped[dest] = []; grouped[dest].push(r); }); return Object.keys(grouped).sort().map(destination => (

{destination}

{(() => { // Group by source/provider within this destination const bySource: Record = {}; grouped[destination].forEach((r: any) => { const src = r.source || 'Other'; if (!bySource[src]) bySource[src] = []; bySource[src].push(r); }); return Object.keys(bySource).sort().map(source => (

{source}

{bySource[source].map((r: any) => )}
)); })()}
)); })()}
); }