From e645b15c6eaae7ed3035190ed6ce1319191f5fba Mon Sep 17 00:00:00 2001 From: Tobias Johansson Date: Fri, 13 Jun 2025 07:41:31 +0000 Subject: [PATCH] Merged in fix/SW-2841-filter-popup-closing-on-select (pull request #2289) Fix/SW-2841 filter popup closing on select * fix(SW-2841): refactored so that filter modal is not closed when selecting filters * fix(SW-2841): rename component * fix: review feedback * fix: move font-family * fix: change init value of filteredHotelIds * fix * fix: add Typography tag Approved-by: Michael Zetterberg Approved-by: Christian Andolf --- .../HotelCardListing/index.tsx | 5 +- .../filterAndSortModal.module.css | 0 .../FilterAndSortModal/index.tsx | 68 ++++++-- .../FilterCheckbox/filterCheckbox.module.css | 0 .../FilterContent}/FilterCheckbox/index.tsx | 0 .../FilterContent/filterContent.module.css | 42 +++++ .../Filters/FilterContent/index.tsx | 131 +++++++++++++++ .../HotelFilter/hotelFilter.module.css | 0 .../SelectHotel/Filters/HotelFilter/index.tsx | 85 ++++++++++ .../SelectHotel/Filters/index.ts | 2 + .../SelectHotel/HotelFilter/index.tsx | 153 ------------------ .../MobileMapButtonContainer/index.tsx | 2 +- .../SelectHotelMap/HotelListing/index.tsx | 6 +- .../SelectHotelMapContent/index.tsx | 9 +- .../HotelReservation/SelectHotel/index.tsx | 2 +- apps/scandic-web/stores/hotel-filters.ts | 7 +- .../selectHotel/hotelCardListingProps.ts | 1 + .../hotelReservation/selectHotel/map.ts | 1 + 18 files changed, 339 insertions(+), 175 deletions(-) rename apps/scandic-web/components/HotelReservation/SelectHotel/{ => Filters}/FilterAndSortModal/filterAndSortModal.module.css (100%) rename apps/scandic-web/components/HotelReservation/SelectHotel/{ => Filters}/FilterAndSortModal/index.tsx (74%) rename apps/scandic-web/components/HotelReservation/SelectHotel/{HotelFilter => Filters/FilterContent}/FilterCheckbox/filterCheckbox.module.css (100%) rename apps/scandic-web/components/HotelReservation/SelectHotel/{HotelFilter => Filters/FilterContent}/FilterCheckbox/index.tsx (100%) create mode 100644 apps/scandic-web/components/HotelReservation/SelectHotel/Filters/FilterContent/filterContent.module.css create mode 100644 apps/scandic-web/components/HotelReservation/SelectHotel/Filters/FilterContent/index.tsx rename apps/scandic-web/components/HotelReservation/SelectHotel/{ => Filters}/HotelFilter/hotelFilter.module.css (100%) create mode 100644 apps/scandic-web/components/HotelReservation/SelectHotel/Filters/HotelFilter/index.tsx create mode 100644 apps/scandic-web/components/HotelReservation/SelectHotel/Filters/index.ts delete mode 100644 apps/scandic-web/components/HotelReservation/SelectHotel/HotelFilter/index.tsx diff --git a/apps/scandic-web/components/HotelReservation/HotelCardListing/index.tsx b/apps/scandic-web/components/HotelReservation/HotelCardListing/index.tsx index b6fc1d9a9..377929e3b 100644 --- a/apps/scandic-web/components/HotelReservation/HotelCardListing/index.tsx +++ b/apps/scandic-web/components/HotelReservation/HotelCardListing/index.tsx @@ -28,6 +28,7 @@ import { BookingCodeFilterEnum } from "@/types/enums/bookingCodeFilter" export default function HotelCardListing({ hotelData, + unfilteredHotelCount, type = HotelCardListingTypeEnum.PageListing, }: HotelCardListingProps) { const { data: session } = useSession() @@ -104,8 +105,8 @@ export default function HotelCardListing({ }, [activeHotel, type]) useEffect(() => { - setResultCount(hotels.length) - }, [hotels, setResultCount]) + setResultCount(hotels.length, unfilteredHotelCount) + }, [hotels, setResultCount, unfilteredHotelCount]) function isHotelActiveInMapView(hotelName: string): boolean { return ( diff --git a/apps/scandic-web/components/HotelReservation/SelectHotel/FilterAndSortModal/filterAndSortModal.module.css b/apps/scandic-web/components/HotelReservation/SelectHotel/Filters/FilterAndSortModal/filterAndSortModal.module.css similarity index 100% rename from apps/scandic-web/components/HotelReservation/SelectHotel/FilterAndSortModal/filterAndSortModal.module.css rename to apps/scandic-web/components/HotelReservation/SelectHotel/Filters/FilterAndSortModal/filterAndSortModal.module.css diff --git a/apps/scandic-web/components/HotelReservation/SelectHotel/FilterAndSortModal/index.tsx b/apps/scandic-web/components/HotelReservation/SelectHotel/Filters/FilterAndSortModal/index.tsx similarity index 74% rename from apps/scandic-web/components/HotelReservation/SelectHotel/FilterAndSortModal/index.tsx rename to apps/scandic-web/components/HotelReservation/SelectHotel/Filters/FilterAndSortModal/index.tsx index 06fbd972e..03d207a2f 100644 --- a/apps/scandic-web/components/HotelReservation/SelectHotel/FilterAndSortModal/index.tsx +++ b/apps/scandic-web/components/HotelReservation/SelectHotel/Filters/FilterAndSortModal/index.tsx @@ -4,7 +4,7 @@ import { usePathname, useSearchParams, } from "next/dist/client/components/navigation" -import { useCallback, useState } from "react" +import { useCallback, useEffect, useState } from "react" import { Dialog as AriaDialog, DialogTrigger, @@ -24,8 +24,8 @@ import Footnote from "@/components/TempDesignSystem/Text/Footnote" import Subtitle from "@/components/TempDesignSystem/Text/Subtitle" import useInitializeFiltersFromUrl from "@/hooks/useInitializeFiltersFromUrl" -import HotelFilter from "../HotelFilter" -import { DEFAULT_SORT } from "../HotelSorter" +import { DEFAULT_SORT } from "../../HotelSorter" +import FilterContent from "../FilterContent" import styles from "./filterAndSortModal.module.css" @@ -40,14 +40,32 @@ export default function FilterAndSortModal({ setShowSkeleton, }: FilterAndSortModalProps) { const intl = useIntl() + useInitializeFiltersFromUrl() const searchParams = useSearchParams() const pathname = usePathname() - const resultCount = useHotelFilterStore((state) => state.resultCount) - const setFilters = useHotelFilterStore((state) => state.setFilters) - const activeFilters = useHotelFilterStore((state) => state.activeFilters) + + const { resultCount, setFilters, activeFilters, unfilteredResultCount } = + useHotelFilterStore((state) => ({ + resultCount: state.resultCount, + setFilters: state.setFilters, + activeFilters: state.activeFilters, + unfilteredResultCount: state.unfilteredResultCount, + })) + const [sort, setSort] = useState(searchParams.get("sort") ?? DEFAULT_SORT) + const [selectedFilters, setSelectedFilters] = + useState(activeFilters) + + const [filteredCount, setFilteredCount] = useState(resultCount) + + useEffect(() => { + if (activeFilters.length) { + setSelectedFilters(activeFilters) + } + }, [activeFilters]) + const sortItems: SortItem[] = [ { label: intl.formatMessage({ @@ -81,14 +99,21 @@ export default function FilterAndSortModal({ const handleApplyFiltersAndSorting = useCallback( (close: () => void) => { + setFilters(selectedFilters) + if (setShowSkeleton) { setShowSkeleton(true) } - if (sort === searchParams.get("sort")) { - close() - } const newSearchParams = new URLSearchParams(searchParams) + + const values = selectedFilters.join(",") + if (values === "") { + newSearchParams.delete("filters") + } else { + newSearchParams.set("filters", values) + } + newSearchParams.set("sort", sort) window.history.replaceState( @@ -103,7 +128,7 @@ export default function FilterAndSortModal({ }, 500) } }, - [pathname, searchParams, sort, setShowSkeleton] + [pathname, searchParams, sort, setShowSkeleton, selectedFilters, setFilters] ) return ( @@ -159,7 +184,19 @@ export default function FilterAndSortModal({
- + { + const isSelected = selectedFilters.includes(id) + setSelectedFilters((prev) => + isSelected + ? prev.filter((s) => s !== id) + : [...prev, id] + ) + }} + onFilteredCountChange={setFilteredCount} + />