import React, { useState, useEffect } from 'react'; import { Link } from 'react-router-dom'; import { ArrowRight, AlertCircle, } from 'lucide-react'; import { BannerCarousel, BannerSkeleton, RoomCard, RoomCardSkeleton, SearchRoomForm, } from '../components/rooms'; import { bannerService, roomService } from '../services/api'; import type { Banner } from '../services/api/bannerService'; import type { Room } from '../services/api/roomService'; const HomePage: React.FC = () => { const [banners, setBanners] = useState([]); const [featuredRooms, setFeaturedRooms] = useState([]); const [newestRooms, setNewestRooms] = useState([]); const [isLoadingBanners, setIsLoadingBanners] = useState(true); const [isLoadingRooms, setIsLoadingRooms] = useState(true); const [isLoadingNewest, setIsLoadingNewest] = useState(true); const [error, setError] = useState(null); // Fetch banners useEffect(() => { const fetchBanners = async () => { try { setIsLoadingBanners(true); const response = await bannerService .getBannersByPosition('home'); // Handle both response formats if ( response.success || response.status === 'success' ) { setBanners(response.data?.banners || []); } } catch (err: any) { console.error('Error fetching banners:', err); // Don't show error for banners, just use fallback // Silently fail - banners are not critical for page functionality } finally { setIsLoadingBanners(false); } }; fetchBanners(); }, []); // Fetch featured rooms useEffect(() => { const fetchFeaturedRooms = async () => { try { setIsLoadingRooms(true); setError(null); const response = await roomService.getFeaturedRooms({ featured: true, limit: 6, }); // Handle both response formats if ( response.success || response.status === 'success' ) { const rooms = response.data?.rooms || []; setFeaturedRooms(rooms); // If no rooms found but request succeeded, don't show error if (rooms.length === 0) { setError(null); } } else { // Response didn't indicate success setError( response.message || 'Unable to load room list' ); } } catch (err: any) { console.error('Error fetching rooms:', err); // Check if it's a rate limit error if (err.response?.status === 429) { setError( 'Too many requests. Please wait a moment and refresh the page.' ); } else { setError( err.response?.data?.message || err.message || 'Unable to load room list' ); } } finally { setIsLoadingRooms(false); } }; fetchFeaturedRooms(); }, []); // Fetch newest rooms useEffect(() => { const fetchNewestRooms = async () => { try { setIsLoadingNewest(true); const response = await roomService.getRooms({ page: 1, limit: 6, sort: 'newest', }); // Handle both response formats if ( response.success || response.status === 'success' ) { setNewestRooms(response.data?.rooms || []); } } catch (err: any) { console.error('Error fetching newest rooms:', err); // Silently fail for newest rooms section - not critical } finally { setIsLoadingNewest(false); } }; fetchNewestRooms(); }, []); return (
{/* Banner Section */}
{isLoadingBanners ? ( ) : ( )}
{/* Search Section */}
{/* Featured Rooms Section */}
{/* Section Header */}

Featured Rooms

View All Rooms
{/* Loading State */} {isLoadingRooms && (
{[...Array(6)].map((_, index) => ( ))}
)} {/* Error State */} {error && !isLoadingRooms && (

{error}

)} {/* Rooms Grid */} {!isLoadingRooms && !error && ( <> {featuredRooms.length > 0 ? (
{featuredRooms.map((room) => ( ))}
) : (

No featured rooms available

)} {/* View All Button (Mobile) */} {featuredRooms.length > 0 && (
View All Rooms
)} )}
{/* Newest Rooms Section */}
{/* Section Header */}

Newest Rooms

View All Rooms
{/* Loading State */} {isLoadingNewest && (
{[...Array(6)].map((_, index) => ( ))}
)} {/* Rooms Grid */} {!isLoadingNewest && ( <> {newestRooms.length > 0 ? (
{newestRooms.map((room) => ( ))}
) : (

No new rooms available

)} {/* View All Button (Mobile) */} {newestRooms.length > 0 && (
View All Rooms
)} )}
{/* Features Section */}
🏨

Easy Booking

Search and book rooms with just a few clicks

💰

Best Prices

Best price guarantee in the market

🎧

24/7 Support

Support team always ready to serve

); }; export default HomePage;