feat(SW-885): ancillary and book next stay

This commit is contained in:
Simon Emanuelsson
2024-12-12 13:39:49 +01:00
parent f531350e2e
commit 9d4998c9c5
12 changed files with 114 additions and 51 deletions

View File

@@ -22,7 +22,10 @@ import SearchList from "./SearchList"
import styles from "./search.module.css"
import type { BookingWidgetSchema } from "@/types/components/bookingWidget"
import { ActionType } from "@/types/components/form/bookingwidget"
import {
ActionType,
SetStorageData,
} from "@/types/components/form/bookingwidget"
import type { SearchProps } from "@/types/components/search"
import type { Location } from "@/types/trpc/routers/hotel/locations"
@@ -40,7 +43,7 @@ export default function Search({ locations, handlePressEnter }: SearchProps) {
: null
const [state, dispatch] = useReducer(
reducer,
{ defaultLocations: locations },
{ defaultLocations: locations, initialValue: value },
init
)
const handleMatchLocations = useCallback(
@@ -122,30 +125,19 @@ export default function Search({ locations, handlePressEnter }: SearchProps) {
}
useEffect(() => {
const searchData =
typeof window !== "undefined"
? sessionStorage.getItem(sessionStorageKey)
: undefined
const searchHistory =
typeof window !== "undefined"
? localStorage.getItem(localStorageKey)
: null
if (searchData || searchHistory) {
dispatch({
payload: {
searchData:
isValidJson(searchData) && searchData
? JSON.parse(searchData)
: undefined,
searchHistory:
isValidJson(searchHistory) && searchHistory
? JSON.parse(searchHistory)
: null,
},
type: ActionType.SET_STORAGE_DATA,
})
const searchData = sessionStorage.getItem(sessionStorageKey)
const searchHistory = localStorage.getItem(localStorageKey)
const payload: SetStorageData["payload"] = {}
if (searchData) {
payload.searchData = JSON.parse(searchData)
}
if (searchHistory) {
payload.searchHistory = JSON.parse(searchHistory)
}
dispatch({
payload,
type: ActionType.SET_STORAGE_DATA,
})
}, [dispatch])
const stayType = state.searchData?.type === "cities" ? "city" : "hotel"