"use client" import { useRouter, useSearchParams } from "next/navigation" import { useSession } from "next-auth/react" import { useEffect, useMemo, useRef } from "react" import { useIntl } from "react-intl" import { BookingCodeFilterEnum, useBookingCodeFilterStore, } from "@scandic-hotels/booking-flow/stores/bookingCode-filter" import { alternativeHotelsMap, selectHotelMap, } from "@scandic-hotels/common/constants/routes/hotelReservation" import { BackToTopButton } from "@scandic-hotels/design-system/BackToTopButton" import { HotelCard } from "@scandic-hotels/design-system/HotelCard" import { useHotelFilterStore } from "@/stores/hotel-filters" import { useHotelsMapStore } from "@/stores/hotels-map" import HotelDetailsSidePeek from "@/components/SidePeeks/HotelDetailsSidePeek" import useLang from "@/hooks/useLang" import { useScrollToTop } from "@/hooks/useScrollToTop" import { isValidClientSession } from "@/utils/clientSession" import { mapApiImagesToGalleryImages } from "@/utils/imageGallery" import { DEFAULT_SORT } from "../SelectHotel/HotelSorter" import { getSortedHotels } from "./utils" import styles from "./hotelCardListing.module.css" import type { HotelType } from "@scandic-hotels/common/constants/hotelType" import { type HotelCardListingProps, HotelCardListingTypeEnum, } from "@/types/components/hotelReservation/selectHotel/hotelCardListingProps" export default function HotelCardListing({ hotelData, unfilteredHotelCount, type = HotelCardListingTypeEnum.PageListing, isAlternative, }: HotelCardListingProps) { const router = useRouter() const lang = useLang() const intl = useIntl() const { data: session } = useSession() const isUserLoggedIn = isValidClientSession(session) const searchParams = useSearchParams() const activeFilters = useHotelFilterStore((state) => state.activeFilters) const setResultCount = useHotelFilterStore((state) => state.setResultCount) const { activeHotel, activate, disengage, engage } = useHotelsMapStore() const { showBackToTop, scrollToTop } = useScrollToTop({ threshold: 490 }) const activeCardRef = useRef(null) const sortBy = searchParams.get("sort") ?? DEFAULT_SORT const bookingCode = searchParams.get("bookingCode") // Special rates (corporate cheque, voucher) will not show regular rate hotels availability const isSpecialRate = bookingCode ? hotelData.find( (hotel) => hotel.availability.productType?.bonusCheque || hotel.availability.productType?.voucher ) : false const activeCodeFilter = useBookingCodeFilterStore( (state) => state.activeCodeFilter ) const isBookingCodeRateAvailable = bookingCode && !isSpecialRate ? hotelData.some((hotel) => hotel.availability.bookingCode) : false const showOnlyBookingCodeRates = isBookingCodeRateAvailable && activeCodeFilter === BookingCodeFilterEnum.Discounted const hotels = useMemo(() => { const sortedHotels = getSortedHotels({ hotels: hotelData, sortBy, bookingCode: isSpecialRate ? null : bookingCode, }) const updatedHotelsList = showOnlyBookingCodeRates ? sortedHotels.filter((hotel) => hotel.availability.bookingCode) : sortedHotels if (!activeFilters.length) { return updatedHotelsList } return updatedHotelsList.filter((hotel) => activeFilters.every((appliedFilterId) => hotel.hotel.detailedFacilities.some( (facility) => facility.id.toString() === appliedFilterId ) ) ) }, [ activeFilters, bookingCode, hotelData, sortBy, showOnlyBookingCodeRates, isSpecialRate, ]) useEffect(() => { if (activeCardRef.current && type === HotelCardListingTypeEnum.MapListing) { activeCardRef.current.scrollIntoView({ behavior: "smooth", block: "nearest", inline: "center", }) } }, [activeHotel, type]) useEffect(() => { setResultCount(hotels.length, unfilteredHotelCount) }, [hotels, setResultCount, unfilteredHotelCount]) function isHotelActiveInMapView(hotelName: string): boolean { return ( hotelName === activeHotel && type === HotelCardListingTypeEnum.MapListing ) } return (
{hotels.map((hotel) => (
engage(hotel.hotel.name)} onHoverEnd={() => disengage()} onAddressClick={() => { const mapUrl = isAlternative ? alternativeHotelsMap(lang) : selectHotelMap(lang) disengage() // Disengage the current hotel to avoid the hover state from being active when clicking on the address activate(hotel.hotel.name) router.push(`${mapUrl}?${searchParams.toString()}`) }} belowInfoSlot={ } distanceToCityCenter={hotel.hotel.location.distanceToCentre} images={mapApiImagesToGalleryImages(hotel.hotel.galleryImages)} isUserLoggedIn={isUserLoggedIn} state={ isHotelActiveInMapView(hotel.hotel.name) ? "active" : "default" } type={type} bookingCode={bookingCode} isAlternative={isAlternative} />
))} {showBackToTop && ( )}
) }