feat(SW-977): Scenario 1

This commit is contained in:
Pontus Dreij
2024-12-11 13:39:15 +01:00
parent 92bbfcf533
commit 4f92d1da08
2 changed files with 18 additions and 9 deletions

View File

@@ -79,8 +79,8 @@ export default function BookingWidgetClient({
const selectedLocation = bookingWidgetSearchData
? getLocationObj(
(bookingWidgetSearchData.city ??
bookingWidgetSearchData.hotel) as string
(bookingWidgetSearchData.hotel ??
bookingWidgetSearchData.city) as string
)
: undefined

View File

@@ -28,16 +28,19 @@ import type { Location } from "@/types/trpc/routers/hotel/locations"
const name = "search"
export default function Search({ locations, handlePressEnter }: SearchProps) {
const { register, setValue, unregister } =
const { register, setValue, unregister, getValues } =
useFormContext<BookingWidgetSchema>()
const intl = useIntl()
const value = useWatch({ name })
const locationString = getValues("location")
const location = locationString
? JSON.parse(decodeURIComponent(locationString))
: null
const [state, dispatch] = useReducer(
reducer,
{ defaultLocations: locations },
init
)
const handleMatchLocations = useCallback(
function (searchValue: string) {
return locations.filter((location) => {
@@ -157,6 +160,16 @@ export default function Search({ locations, handlePressEnter }: SearchProps) {
}
}, [stayType, stayValue, unregister, setValue])
function getLocationLabel(): string {
if (state.searchData?.type === "hotels") {
return state.searchData?.relationships?.city?.name || ""
}
if (location?.type === "hotels") {
return location?.relationships?.city?.name || ""
}
return intl.formatMessage({ id: "Where to" })
}
return (
<Downshift
initialSelectedItem={state.searchData}
@@ -187,11 +200,7 @@ export default function Search({ locations, handlePressEnter }: SearchProps) {
color={isOpen ? "uiTextActive" : "red"}
asChild
>
<span>
{state.searchData?.type === "hotels"
? state.searchData?.relationships?.city?.name
: intl.formatMessage({ id: "Where to" })}
</span>
<span>{getLocationLabel()}</span>
</Caption>
</label>
<div {...getRootProps({}, { suppressRefError: true })}>