fix(SW-1111) refactor state of active hotel card and hotel pin

This commit is contained in:
Pontus Dreij
2024-12-09 16:49:15 +01:00
parent 7f50d34431
commit 15c5afc43a
15 changed files with 108 additions and 115 deletions

View File

@@ -4,7 +4,8 @@ import { memo, useCallback } from "react"
import { useIntl } from "react-intl"
import { Lang } from "@/constants/languages"
import { selectHotelMap, selectRate } from "@/constants/routes/hotelReservation"
import { selectRate } from "@/constants/routes/hotelReservation"
import { useHotelsMapStore } from "@/stores/hotels-map"
import { mapFacilityToIcon } from "@/components/ContentType/HotelPage/data"
import ImageGallery from "@/components/ImageGallery"
@@ -32,26 +33,27 @@ function HotelCard({
hotel,
type = HotelCardListingTypeEnum.PageListing,
state = "default",
onHotelCardHover,
}: HotelCardProps) {
const params = useParams()
const lang = params.lang as Lang
const intl = useIntl()
const { setActiveHotelPin, setActiveHotelCard } = useHotelsMapStore()
const { hotelData } = hotel
const { price } = hotel
const handleMouseEnter = useCallback(() => {
if (onHotelCardHover && hotelData) {
onHotelCardHover(hotelData.name)
if (hotelData) {
setActiveHotelPin(hotelData.name)
}
}, [onHotelCardHover, hotelData])
}, [setActiveHotelPin, hotelData])
const handleMouseLeave = useCallback(() => {
if (onHotelCardHover) {
onHotelCardHover(null)
if (hotelData) {
setActiveHotelPin(null)
setActiveHotelCard(null)
}
}, [onHotelCardHover])
}, [setActiveHotelPin, hotelData, setActiveHotelCard])
if (!hotel || !hotelData) return null

View File

@@ -3,6 +3,8 @@
import { useCallback, useEffect, useRef } from "react"
import { useMediaQuery } from "usehooks-ts"
import { useHotelsMapStore } from "@/stores/hotels-map"
import useClickOutside from "@/hooks/useClickOutside"
import HotelCardDialog from "../HotelCardDialog"
@@ -14,18 +16,21 @@ import type { HotelCardDialogListingProps } from "@/types/components/hotelReserv
export default function HotelCardDialogListing({
hotels,
activeCard,
onActiveCardChange,
}: HotelCardDialogListingProps) {
const hotelsPinData = hotels ? getHotelPins(hotels) : []
const activeCardRef = useRef<HTMLDivElement | null>(null)
const observerRef = useRef<IntersectionObserver | null>(null)
const dialogRef = useRef<HTMLDivElement>(null)
const isMobile = useMediaQuery("(max-width: 768px)")
const { activeHotelCard, setActiveHotelCard, setActiveHotelPin } =
useHotelsMapStore()
useClickOutside(dialogRef, !!activeCard && isMobile, () => {
onActiveCardChange(null)
})
function handleClose() {
setActiveHotelCard(null)
setActiveHotelPin(null)
}
useClickOutside(dialogRef, !!activeHotelCard && isMobile, handleClose)
const handleIntersection = useCallback(
(entries: IntersectionObserverEntry[]) => {
@@ -33,12 +38,12 @@ export default function HotelCardDialogListing({
if (entry.isIntersecting) {
const cardName = entry.target.getAttribute("data-name")
if (cardName) {
onActiveCardChange(cardName)
setActiveHotelCard(cardName)
}
}
})
},
[onActiveCardChange]
[setActiveHotelCard]
)
useEffect(() => {
@@ -73,13 +78,13 @@ export default function HotelCardDialogListing({
elements.forEach((el) => observerRef.current?.observe(el))
}, 1000)
}
}, [activeCard])
}, [activeHotelCard])
return (
<div className={styles.hotelCardDialogListing} ref={dialogRef}>
{!!hotelsPinData?.length &&
hotelsPinData.map((data) => {
const isActive = data.name === activeCard
const isActive = data.name === activeHotelCard
return (
<div
key={data.name}
@@ -88,8 +93,8 @@ export default function HotelCardDialogListing({
>
<HotelCardDialog
data={data}
isOpen={!!activeCard}
handleClose={() => onActiveCardChange(null)}
isOpen={!!activeHotelCard}
handleClose={handleClose}
/>
</div>
)

View File

@@ -4,6 +4,7 @@ import { useEffect, useMemo, useState } from "react"
import { useIntl } from "react-intl"
import { useHotelFilterStore } from "@/stores/hotel-filters"
import { useHotelsMapStore } from "@/stores/hotels-map"
import Alert from "@/components/TempDesignSystem/Alert"
import { BackToTopButton } from "@/components/TempDesignSystem/BackToTopButton"
@@ -24,14 +25,13 @@ import { AlertTypeEnum } from "@/types/enums/alert"
export default function HotelCardListing({
hotelData,
type = HotelCardListingTypeEnum.PageListing,
activeCard,
onHotelCardHover,
}: HotelCardListingProps) {
const searchParams = useSearchParams()
const activeFilters = useHotelFilterStore((state) => state.activeFilters)
const setResultCount = useHotelFilterStore((state) => state.setResultCount)
const [showBackToTop, setShowBackToTop] = useState<boolean>(false)
const intl = useIntl()
const { activeHotelCard } = useHotelsMapStore()
const sortBy = useMemo(
() => searchParams.get("sort") ?? DEFAULT_SORT,
@@ -111,13 +111,16 @@ export default function HotelCardListing({
hotels.map((hotel) => (
<div
key={hotel.hotelData.operaId}
data-active={hotel.hotelData.name === activeCard ? "true" : "false"}
data-active={
hotel.hotelData.name === activeHotelCard ? "true" : "false"
}
>
<HotelCard
hotel={hotel}
type={type}
state={hotel.hotelData.name === activeCard ? "active" : "default"}
onHotelCardHover={onHotelCardHover}
state={
hotel.hotelData.name === activeHotelCard ? "active" : "default"
}
/>
</div>
))

View File

@@ -1,5 +1,7 @@
"use client"
import { useHotelsMapStore } from "@/stores/hotels-map"
import HotelCardDialogListing from "@/components/HotelReservation/HotelCardDialogListing"
import HotelCardListing from "@/components/HotelReservation/HotelCardListing"
@@ -8,27 +10,18 @@ import styles from "./hotelListing.module.css"
import { HotelCardListingTypeEnum } from "@/types/components/hotelReservation/selectHotel/hotelCardListingProps"
import type { HotelListingProps } from "@/types/components/hotelReservation/selectHotel/map"
export default function HotelListing({
hotels,
activeHotelPin,
setActiveHotelPin,
}: HotelListingProps) {
export default function HotelListing({ hotels }: HotelListingProps) {
const { activeHotelPin } = useHotelsMapStore()
return (
<>
<div className={styles.hotelListing}>
<HotelCardListing
hotelData={hotels}
type={HotelCardListingTypeEnum.MapListing}
activeCard={activeHotelPin}
onHotelCardHover={setActiveHotelPin}
/>
</div>
<div className={styles.hotelListingMobile} data-open={!!activeHotelPin}>
<HotelCardDialogListing
hotels={hotels}
activeCard={activeHotelPin}
onActiveCardChange={setActiveHotelPin}
/>
<HotelCardDialogListing hotels={hotels} />
</div>
</>
)

View File

@@ -14,26 +14,18 @@ import type {
HotelData,
NullableHotelData,
} from "@/types/components/hotelReservation/selectHotel/hotelCardListingProps"
import type { SelectHotelSearchParams } from "@/types/components/hotelReservation/selectHotel/selectHotelSearchParams"
import type { Location } from "@/types/trpc/routers/hotel/locations"
import type { SelectHotelMapContainerProps } from "@/types/components/hotelReservation/selectHotel/map"
function isHotelData(hotel: NullableHotelData): hotel is HotelData {
function isValidHotelData(hotel: NullableHotelData): hotel is HotelData {
return hotel !== null && hotel !== undefined
}
type Props = {
city: Location
searchParams: SelectHotelSearchParams
adultsInRoom: number
childrenInRoom: string | undefined
}
export async function SelectHotelMapContainer({
city,
searchParams,
adultsInRoom,
childrenInRoom,
}: Props) {
}: SelectHotelMapContainerProps) {
const googleMapId = env.GOOGLE_DYNAMIC_MAP_ID
const googleMapsApiKey = env.GOOGLE_STATIC_MAP_KEY
@@ -47,15 +39,9 @@ export async function SelectHotelMapContainer({
})
)
const [hotels, hotelsError] = await fetchAvailableHotelsPromise
const [hotels] = await fetchAvailableHotelsPromise
if (hotelsError) {
// TODO: show proper error component
console.error("[SelectHotelMapContainer] unable to fetch hotels")
return null
}
const validHotels = hotels?.filter(isHotelData) || []
const validHotels = hotels?.filter(isValidHotelData) || []
const hotelPins = getHotelPins(validHotels)
const filterList = getFiltersFromHotels(validHotels)

View File

@@ -1,15 +1,12 @@
.container {
max-width: var(--max-width);
height: 100%;
display: flex;
width: 100%;
}
.listingContainer {
background-color: var(--Base-Surface-Secondary-light-Normal);
padding: var(--Spacing-x3) var(--Spacing-x4);
overflow-y: auto;
max-width: 505px;
position: relative;
height: 100%;
display: none;
}
.skeletonContainer {
@@ -27,7 +24,20 @@
width: 440px;
}
.mapContainer {
flex: 1;
}
@media (min-width: 768px) {
.listingContainer {
background-color: var(--Base-Surface-Secondary-light-Normal);
padding: var(--Spacing-x3) var(--Spacing-x4);
overflow-y: auto;
max-width: 505px;
position: relative;
height: 100%;
display: block;
}
.skeletonContainer {
display: flex;
}

View File

@@ -1,3 +1,5 @@
import SkeletonShimmer from "@/components/SkeletonShimmer"
import { RoomCardSkeleton } from "../../SelectRate/RoomSelection/RoomCard/RoomCardSkeleton"
import styles from "./SelectHotelMapContainerSkeleton.module.css"
@@ -18,6 +20,9 @@ export async function SelectHotelMapContainerSkeleton({ count = 2 }: Props) {
))}
</div>
</div>
<div className={styles.mapContainer}>
<SkeletonShimmer width={"100%"} height="100%" />
</div>
</div>
)
}

View File

@@ -1,13 +1,13 @@
"use client"
import { APIProvider } from "@vis.gl/react-google-maps"
import { useSearchParams } from "next/navigation"
import { useEffect, useMemo, useRef, useState } from "react"
import { useIntl } from "react-intl"
import { useMediaQuery } from "usehooks-ts"
import { selectHotel } from "@/constants/routes/hotelReservation"
import { useHotelFilterStore } from "@/stores/hotel-filters"
import { useHotelsMapStore } from "@/stores/hotels-map"
import { CloseIcon, CloseLargeIcon } from "@/components/Icons"
import InteractiveMap from "@/components/Maps/InteractiveMap"
@@ -31,18 +31,13 @@ export default function SelectHotelMap({
filterList,
cityCoordinates,
}: SelectHotelMapProps) {
const searchParams = useSearchParams()
const lang = useLang()
const intl = useIntl()
const isAboveMobile = useMediaQuery("(min-width: 768px)")
const [activeHotelPin, setActiveHotelPin] = useState<string | null>(null)
const [showBackToTop, setShowBackToTop] = useState<boolean>(false)
const listingContainerRef = useRef<HTMLDivElement | null>(null)
const activeFilters = useHotelFilterStore((state) => state.activeFilters)
const selectHotelParams = new URLSearchParams(searchParams.toString())
const selectedHotel = selectHotelParams.get("selectedHotel")
const { activeHotelCard, activeHotelPin } = useHotelsMapStore()
const coordinates = isAboveMobile
? cityCoordinates
@@ -56,13 +51,7 @@ export default function SelectHotelMap({
activeElement.scrollIntoView({ behavior: "smooth", block: "nearest" })
}
}
}, [activeHotelPin])
useEffect(() => {
if (selectedHotel) {
setActiveHotelPin(selectedHotel)
}
}, [selectedHotel])
}, [activeHotelCard, activeHotelPin])
useEffect(() => {
const hotelListingElement = document.querySelector(
@@ -96,8 +85,6 @@ export default function SelectHotelMap({
[activeFilters, hotelPins]
)
console.log("hotelPins", hotelPins)
const closeButton = (
<Button
intent="inverted"
@@ -131,11 +118,7 @@ export default function SelectHotelMap({
</Button>
<FilterAndSortModal filters={filterList} />
</div>
<HotelListing
hotels={hotels}
activeHotelPin={activeHotelPin}
setActiveHotelPin={setActiveHotelPin}
/>
<HotelListing hotels={hotels} />
{showBackToTop && (
<BackToTopButton position="left" onClick={scrollToTop} />
)}
@@ -144,8 +127,6 @@ export default function SelectHotelMap({
closeButton={closeButton}
coordinates={coordinates}
hotelPins={filteredHotelPins}
activeHotelPin={activeHotelPin}
onActiveHotelPinChange={setActiveHotelPin}
mapId={mapId}
/>
</div>