|
1 | | -import { ComponentType, FC } from 'react'; |
| 1 | +import { ComponentType, FC, useEffect } from 'react'; |
2 | 2 | import { useTranslation } from 'react-i18next'; |
3 | 3 | import { TranslationKey } from '../../../../../i18n'; |
4 | 4 | import { LoadingWrapper } from '../../../../../uiComponents/LoadingWrapper'; |
@@ -36,21 +36,43 @@ export const StopPlaceSearchResults: FC<StopPlaceSearchResultsProps> = ({ |
36 | 36 | filters, |
37 | 37 | sortingInfo: { sortBy }, |
38 | 38 | }, |
| 39 | + historyState, |
| 40 | + setHistoryState, |
39 | 41 | } = useStopSearchRouterState(); |
40 | 42 |
|
41 | 43 | const { stopPlaces, loading } = useFindStopPlaces(filters, placeType); |
42 | 44 |
|
43 | 45 | const groupByArea = |
44 | 46 | sortBy === groupingField || sortBy === SortStopsBy.DEFAULT; |
45 | 47 |
|
| 48 | + // When switching back to grouped view, knownStopIds may still be in 'flat' |
| 49 | + // mode. Reset it to 'grouped'. |
| 50 | + useEffect(() => { |
| 51 | + if (!groupByArea) { |
| 52 | + return; |
| 53 | + } |
| 54 | + setHistoryState((p) => { |
| 55 | + if (p.knownStopIds.listingMode === 'grouped') { |
| 56 | + return p; |
| 57 | + } |
| 58 | + return { |
| 59 | + ...p, |
| 60 | + knownStopIds: { listingMode: 'grouped', ids: [], groups: {} }, |
| 61 | + }; |
| 62 | + }); |
| 63 | + }, [groupByArea, setHistoryState]); |
| 64 | + |
| 65 | + const isReadyToShowGrouped = |
| 66 | + groupByArea && historyState.knownStopIds.listingMode === 'grouped'; |
| 67 | + |
46 | 68 | return ( |
47 | 69 | <LoadingWrapper |
48 | 70 | className="flex justify-center" |
49 | 71 | loadingText={t('search.searching')} |
50 | 72 | loading={stopPlaces.length === 0 ? loading : false} |
51 | 73 | testId={testIds.loadingSearchResults} |
52 | 74 | > |
53 | | - {groupByArea ? ( |
| 75 | + {isReadyToShowGrouped ? ( |
54 | 76 | <SearchGroupedStopsResults |
55 | 77 | groupingField={groupingField} |
56 | 78 | stopPlaces={stopPlaces} |
|
0 commit comments