diff --git a/components/HotelReservation/HotelCardDialogListing/index.tsx b/components/HotelReservation/HotelCardDialogListing/index.tsx index 1a2097ada..a91f68041 100644 --- a/components/HotelReservation/HotelCardDialogListing/index.tsx +++ b/components/HotelReservation/HotelCardDialogListing/index.tsx @@ -1,22 +1,54 @@ "use client" -import { useEffect, useRef } from "react" +import { useCallback, useEffect, useRef } from "react" import HotelCardDialog from "../HotelCardDialog" import { getHotelPins } from "./utils" import type { HotelData } from "@/types/components/hotelReservation/selectHotel/hotelCardListingProps" +interface HotelCardDialogListingProps { + hotels: HotelData[] + activeCard: string | null | undefined + onActiveCardChange: (hotelName: string | null) => void +} + export default function HotelCardDialogListing({ hotels, activeCard, -}: { - hotels: HotelData[] - activeCard: string | null | undefined -}) { + onActiveCardChange, +}: HotelCardDialogListingProps) { const hotelsPinData = getHotelPins(hotels) const activeCardRef = useRef(null) + const handleIntersection = useCallback( + (entries: IntersectionObserverEntry[]) => { + entries.forEach((entry) => { + if (entry.isIntersecting) { + const cardName = entry.target.getAttribute("data-name") + if (cardName) { + onActiveCardChange(cardName) + } + } + }) + }, + [onActiveCardChange] + ) + + useEffect(() => { + const observer = new IntersectionObserver(handleIntersection, { + root: null, + threshold: 0.5, // Adjust threshold as needed + }) + + const elements = document.querySelectorAll("[data-name]") + elements.forEach((el) => observer.observe(el)) + + return () => { + elements.forEach((el) => observer.unobserve(el)) + } + }, [handleIntersection]) + useEffect(() => { if (activeCardRef.current) { activeCardRef.current.scrollIntoView({ @@ -33,7 +65,11 @@ export default function HotelCardDialogListing({ hotelsPinData.map((data) => { const isActive = data.name === activeCard return ( -
+
@@ -20,11 +20,15 @@ export default function HotelListing({ hotelData={hotels} type={HotelCardListingTypeEnum.MapListing} activeCard={activeHotelPin} - onHotelCardHover={onHotelCardHover} + onHotelCardHover={setActiveHotelPin} />
- +
) diff --git a/components/HotelReservation/SelectHotel/SelectHotelMap/index.tsx b/components/HotelReservation/SelectHotel/SelectHotelMap/index.tsx index 3367d25b7..f92ec1895 100644 --- a/components/HotelReservation/SelectHotel/SelectHotelMap/index.tsx +++ b/components/HotelReservation/SelectHotel/SelectHotelMap/index.tsx @@ -95,7 +95,7 @@ export default function SelectHotelMap({ {showBackToTop && (