diff --git a/components/Forms/BookingWidget/FormContent/Search/index.tsx b/components/Forms/BookingWidget/FormContent/Search/index.tsx index 061646635..c1f681cc8 100644 --- a/components/Forms/BookingWidget/FormContent/Search/index.tsx +++ b/components/Forms/BookingWidget/FormContent/Search/index.tsx @@ -26,8 +26,8 @@ import { ActionType, type SetStorageData, } from "@/types/components/form/bookingwidget" -import type { SearchProps } from "@/types/components/search" -import type { Location } from "@/types/trpc/routers/hotel/locations" +import type { SearchHistoryItem, SearchProps } from "@/types/components/search" +import type { Location, Locations } from "@/types/trpc/routers/hotel/locations" const name = "search" @@ -99,23 +99,30 @@ export default function Search({ locations, handlePressEnter }: SearchProps) { sessionStorage.setItem(sessionStorageKey, stringified) setValue(name, selectedItem.name) - const searchHistoryMap = new Map() - searchHistoryMap.set(selectedItem.name, selectedItem) - if (state.searchHistory) { - state.searchHistory.forEach((location) => { - searchHistoryMap.set(location.name, location) - }) + const newHistoryItem: SearchHistoryItem = { + type: selectedItem.type, + id: selectedItem.id, } + const oldSearchHistoryWithoutTheNew = + state.searchHistory?.filter( + (h) => h.type !== newHistoryItem.type || h.id !== newHistoryItem.id + ) ?? [] + const oldHistoryItems = oldSearchHistoryWithoutTheNew.map((h) => ({ + id: h.id, + type: h.type, + })) - const searchHistory: Location[] = [] - searchHistoryMap.forEach((location) => { - searchHistory.push(location) - }) + const searchHistory = [newHistoryItem, ...oldHistoryItems] localStorage.setItem(localStorageKey, JSON.stringify(searchHistory)) + + const enhancedSearchHistory: Locations = [ + ...getEnhancedSearchHistory([newHistoryItem], locations), + ...oldSearchHistoryWithoutTheNew, + ] dispatch({ payload: { location: selectedItem, - searchHistory, + searchHistory: enhancedSearchHistory, }, type: ActionType.SELECT_ITEM, }) @@ -132,13 +139,16 @@ export default function Search({ locations, handlePressEnter }: SearchProps) { payload.searchData = JSON.parse(searchData) } if (searchHistory) { - payload.searchHistory = JSON.parse(searchHistory) + payload.searchHistory = getEnhancedSearchHistory( + JSON.parse(searchHistory), + locations + ) } dispatch({ payload, type: ActionType.SET_STORAGE_DATA, }) - }, [dispatch]) + }, [dispatch, locations]) const stayType = state.searchData?.type === "cities" ? "city" : "hotel" const stayValue = @@ -261,9 +271,27 @@ export function SearchSkeleton() { {intl.formatMessage({ id: "Where to" })} -