Fix/book 149 incorrect onfocus behaviour booking widget * fix(BOOK-149): fixed labels shifting * fix(BOOK-149): reintroduced sticky position * fix(BOOK-149): added missing border to "where" text field * added overflow to datepicker * comment fixes * removed separate typography declaration * changed to onPress * fix(BOOK-149): moved components to separate files * fix(BOOK-149): removed desktop & mobile specific css classes * fix(BOOK-149): new implementation of date and room modals * dependencies update * fix(BOOK-149): fixed child age dropdown issue, related error message, and Rooms & Guests container height * updated info button to new variant * fix(BOOK-149): prevent scrolling of background when modals are open in Tablet mode * fixed overlay issue and added focus indicator on mobile * fixed missing space in css * rebase and fixed icon buttons after update * simplified to use explicit boolean * PR comments fixes * more PR comment fixes * PR comment fixes * fixed setIsOpen((prev) => !prev) * fixed issues with room error not showing properly on mobile * fixing pr comments * fixed flickering on GuestRoomModal Approved-by: Erik Tiekstra
216 lines
6.7 KiB
TypeScript
216 lines
6.7 KiB
TypeScript
"use client"
|
|
|
|
import { useCallback, useEffect } from "react"
|
|
import { useFormContext, useWatch } from "react-hook-form"
|
|
import { useIntl } from "react-intl"
|
|
|
|
import { Button } from "@scandic-hotels/design-system/Button"
|
|
import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon"
|
|
import { Typography } from "@scandic-hotels/design-system/Typography"
|
|
import { SEARCH_TYPE_REDEMPTION } from "@scandic-hotels/trpc/constants/booking"
|
|
|
|
import { useBookingFlowConfig } from "../../../bookingFlowConfig/bookingFlowConfigContext"
|
|
import { useIsDesktop } from "../../../hooks/useBreakpoint"
|
|
import { GuestsRoom } from "./GuestsRoom"
|
|
|
|
import styles from "./guests-rooms-picker.module.css"
|
|
|
|
import type { GuestsRoom as TGuestsRoom } from ".."
|
|
import type { BookingWidgetSchema } from "../Client"
|
|
|
|
const MAX_ROOMS = 4
|
|
|
|
interface GuestsRoomsPickerDialogProps {
|
|
rooms: TGuestsRoom[]
|
|
onClose: () => void
|
|
}
|
|
|
|
export default function GuestsRoomsPickerDialog({
|
|
rooms,
|
|
onClose,
|
|
}: GuestsRoomsPickerDialogProps) {
|
|
const intl = useIntl()
|
|
const isDesktop = useIsDesktop()
|
|
const config = useBookingFlowConfig()
|
|
const { getFieldState, trigger, setValue, getValues } =
|
|
useFormContext<BookingWidgetSchema>()
|
|
const roomsValue = useWatch<BookingWidgetSchema, "rooms">({ name: "rooms" })
|
|
const addRoomLabel = intl.formatMessage({
|
|
id: "bookingWidget.roomsPicker.addRoom",
|
|
defaultMessage: "Add room",
|
|
})
|
|
const doneLabel = intl.formatMessage({
|
|
id: "bookingWidget.roomsPicker.done",
|
|
defaultMessage: "Done",
|
|
})
|
|
// Disable add room if booking code is either voucher or corporate cheque, or reward night is enabled
|
|
const addRoomDisabledTextForSpecialRate = getValues(SEARCH_TYPE_REDEMPTION)
|
|
? config.variant === "partner-sas"
|
|
? intl.formatMessage({
|
|
id: "partnerSas.bookingWidget.roomsPicker.disabled",
|
|
defaultMessage:
|
|
"Multi-room booking is not available with EuroBonus points.",
|
|
})
|
|
: intl.formatMessage({
|
|
id: "error.multiroomRewardNightUnavailable",
|
|
defaultMessage:
|
|
"Multi-room booking is not available with reward night.",
|
|
})
|
|
: getValues("bookingCode.value")?.toLowerCase().startsWith("vo") &&
|
|
intl.formatMessage({
|
|
id: "error.multiroomBookingCodeUnavailable",
|
|
defaultMessage:
|
|
"Multi-room booking is not available with this booking code.",
|
|
})
|
|
const isInvalid =
|
|
getFieldState("rooms").invalid ||
|
|
roomsValue.some((room) =>
|
|
room.childrenInRoom.some((child) => child.age === undefined)
|
|
)
|
|
|
|
const handleClose = useCallback(async () => {
|
|
const isValid = await trigger("rooms")
|
|
if (isValid) onClose()
|
|
}, [trigger, onClose])
|
|
|
|
const handleAddRoom = useCallback(() => {
|
|
setValue("rooms", [...roomsValue, { adults: 1, childrenInRoom: [] }], {
|
|
shouldValidate: true,
|
|
shouldDirty: true,
|
|
})
|
|
}, [roomsValue, setValue])
|
|
|
|
const handleRemoveRoom = useCallback(
|
|
(index: number) => {
|
|
const updatedRooms = roomsValue.filter((_, i) => i !== index)
|
|
|
|
setValue("rooms", updatedRooms, {
|
|
shouldValidate: true,
|
|
shouldDirty: true,
|
|
})
|
|
|
|
if (updatedRooms.length === 1) {
|
|
trigger("bookingCode.value")
|
|
trigger(SEARCH_TYPE_REDEMPTION)
|
|
}
|
|
},
|
|
[roomsValue, trigger, setValue]
|
|
)
|
|
|
|
// Validate rooms when they change
|
|
useEffect(() => {
|
|
const fieldState = getFieldState("rooms")
|
|
if (fieldState.invalid) trigger("rooms")
|
|
}, [roomsValue, getFieldState, trigger])
|
|
|
|
const canAddRooms = rooms.length < MAX_ROOMS
|
|
|
|
const addRoomButtonDisabled =
|
|
!!addRoomDisabledTextForSpecialRate || !canAddRooms
|
|
|
|
return (
|
|
<>
|
|
<section className={styles.contentWrapper}>
|
|
<header className={styles.header}>
|
|
<button type="button" className={styles.close} onClick={onClose}>
|
|
<MaterialIcon icon="close" />
|
|
</button>
|
|
</header>
|
|
|
|
<div className={styles.contentContainer}>
|
|
{rooms.map((room, index) => (
|
|
<GuestsRoom
|
|
key={index}
|
|
room={room}
|
|
index={index}
|
|
onRemove={handleRemoveRoom}
|
|
/>
|
|
))}
|
|
{!isDesktop && (
|
|
<>
|
|
<div className={styles.addRoomBtnContainer}>
|
|
<Button
|
|
className={styles.addRoomBtn}
|
|
variant="Text"
|
|
wrapping
|
|
color="Primary"
|
|
onPress={handleAddRoom}
|
|
isDisabled={addRoomButtonDisabled}
|
|
size="sm"
|
|
>
|
|
<MaterialIcon icon="add" color="CurrentColor" />
|
|
{addRoomLabel}
|
|
</Button>
|
|
</div>
|
|
|
|
{addRoomDisabledTextForSpecialRate && (
|
|
<div className={styles.errorContainer}>
|
|
<Typography
|
|
className={styles.error}
|
|
variant="Body/Supporting text (caption)/smRegular"
|
|
>
|
|
<span>
|
|
<MaterialIcon
|
|
icon="error"
|
|
size={20}
|
|
color="Icon/Feedback/Error"
|
|
isFilled
|
|
/>
|
|
{addRoomDisabledTextForSpecialRate}
|
|
</span>
|
|
</Typography>
|
|
</div>
|
|
)}
|
|
</>
|
|
)}
|
|
</div>
|
|
</section>
|
|
<footer className={styles.footer}>
|
|
<div className={styles.footerButtons}>
|
|
{isDesktop && (
|
|
<Button
|
|
variant="Text"
|
|
wrapping
|
|
color="Primary"
|
|
isDisabled={addRoomButtonDisabled}
|
|
size="sm"
|
|
onPress={handleAddRoom}
|
|
>
|
|
<MaterialIcon icon="add_circle" color="CurrentColor" />
|
|
{addRoomLabel}
|
|
</Button>
|
|
)}
|
|
<Button
|
|
onPress={handleClose}
|
|
isDisabled={isInvalid}
|
|
className={styles.doneButton}
|
|
variant={isDesktop ? "Tertiary" : "Primary"}
|
|
color="Primary"
|
|
size={isDesktop ? "sm" : "md"}
|
|
>
|
|
{doneLabel}
|
|
</Button>
|
|
</div>
|
|
|
|
{/* DESKTOP INLINE ERROR MESSAGE */}
|
|
{addRoomDisabledTextForSpecialRate && isDesktop && (
|
|
<Typography
|
|
className={styles.error}
|
|
variant="Body/Supporting text (caption)/smRegular"
|
|
>
|
|
<div className={styles.errorContainer}>
|
|
<MaterialIcon
|
|
icon="error"
|
|
size={20}
|
|
color="Icon/Feedback/Error"
|
|
isFilled
|
|
/>
|
|
{addRoomDisabledTextForSpecialRate}
|
|
</div>
|
|
</Typography>
|
|
)}
|
|
</footer>
|
|
</>
|
|
)
|
|
}
|