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 const selectedLocation = bookingWidgetSearchData
? getLocationObj( ? getLocationObj(
(bookingWidgetSearchData.city ?? (bookingWidgetSearchData.hotel ??
bookingWidgetSearchData.hotel) as string bookingWidgetSearchData.city) as string
) )
: undefined : undefined

View File

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