import React, { useState, useEffect } from 'react'; import { useSearchParams, useNavigate, Link } from 'react-router-dom'; import { Search, Calendar, AlertCircle, ArrowLeft, Home, Users, } from 'lucide-react'; import { RoomCard, RoomCardSkeleton, Pagination, } from '../../components/rooms'; import { searchAvailableRooms } from '../../services/api/roomService'; import type { Room } from '../../services/api/roomService'; import { toast } from 'react-toastify'; const SearchResultsPage: React.FC = () => { const [searchParams] = useSearchParams(); const navigate = useNavigate(); const [rooms, setRooms] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [pagination, setPagination] = useState({ total: 0, page: 1, limit: 12, totalPages: 1, }); // Get search params const from = searchParams.get('from') || ''; const to = searchParams.get('to') || ''; const type = searchParams.get('type') || ''; const capacityParam = searchParams.get('capacity') || ''; const capacity = capacityParam ? Number(capacityParam) : undefined; const pageParam = searchParams.get('page') || ''; const page = pageParam ? Number(pageParam) : 1; useEffect(() => { // Validate required params if (!from || !to) { toast.error( 'Missing search information. ' + 'Please select check-in and check-out dates.' ); navigate('/'); return; } fetchAvailableRooms(); }, [from, to, type, capacity, page]); const fetchAvailableRooms = async () => { try { setLoading(true); setError(null); const response = await searchAvailableRooms({ from, to, type: type || undefined, capacity: capacity || undefined, page, limit: 12, }); if ( response.success || response.status === 'success' ) { setRooms(response.data.rooms || []); if (response.data.pagination) { setPagination(response.data.pagination); } else { // Fallback compute const total = response.data.rooms ? response.data.rooms.length : 0; const limit = 12; setPagination({ total, page, limit, totalPages: Math.max(1, Math.ceil(total / limit)), }); } } else { throw new Error('Unable to search rooms'); } } catch (err: any) { console.error('Error searching rooms:', err); const message = err.response?.data?.message || 'Unable to search available rooms'; setError(message); toast.error(message); } finally { setLoading(false); } }; const formatDate = (dateString: string) => { const date = new Date(dateString); return date.toLocaleDateString('en-US', { day: '2-digit', month: '2-digit', year: 'numeric', }); }; return (
{/* Back Button */} Back to home {/* Search Info Header */}

Search Results

Check-in:{' '} {formatDate(from)}
Check-out:{' '} {formatDate(to)}
{type && (
Room Type:{' '} {type}
)} {capacity && (
Guests:{' '} {capacity}
)}
{/* Loading State */} {loading && (

Searching for available rooms...

{[...Array(6)].map((_, index) => ( ))}
)} {/* Error State */} {error && !loading && (

{error}

)} {/* Results */} {!loading && !error && ( <> {rooms.length > 0 ? ( <>
{rooms.map((room) => ( ))}
) : ( // Empty State

No matching rooms found

Sorry, there are no available rooms for the selected dates. Please try searching with different dates or room types.

View All Rooms
)} )}
); }; export default SearchResultsPage;