Skip to content

Commit 06c4271

Browse files
committed
StopSearch: Simplify and use new router state hook
* Drop the duplicate StopRegistryMainPage. In the MVP version the only thing on that page is the search filters form. For now we can just drop the page completely and use the actual proper Search page as the "Main" or front page of the Stop Registry. * Switch from manual History API state handling to the new unified typed router state hook.
1 parent 08126e6 commit 06c4271

File tree

14 files changed

+302
-172
lines changed

14 files changed

+302
-172
lines changed

cypress/e2e/stop-registry/stopSearch.cy.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -177,24 +177,18 @@ describe('Stop search', () => {
177177
);
178178
}
179179

180-
function setupTestsAndNavigateToPage(
181-
qs: Record<string, unknown>,
182-
startOnResultPage: boolean = false,
183-
) {
180+
function setupTestsAndNavigateToPage(qs: Record<string, unknown>) {
184181
cy.setupTests();
185182
cy.mockLogin();
186183

187-
if (startOnResultPage) {
188-
cy.visit({ url: '/stop-registry/search', qs });
189-
} else {
190-
cy.visit({ url: '/stop-registry', qs });
191-
}
184+
cy.visit({ url: '/stop-registry/search', qs });
185+
192186
stopSearchBar.getSearchInput().clear();
193187
}
194188

195189
function initWithHardcodedData() {
196190
insertHardcodedTestDataBeforeEach();
197-
setupTestsAndNavigateToPage({}, false);
191+
setupTestsAndNavigateToPage({});
198192
}
199193

200194
describe('by label', () => {
@@ -1272,7 +1266,7 @@ describe('Stop search', () => {
12721266
stopPlaces,
12731267
});
12741268

1275-
setupTestsAndNavigateToPage({}, true);
1269+
setupTestsAndNavigateToPage({});
12761270
});
12771271

12781272
it('should visualise priorities correctly', () => {
@@ -1468,7 +1462,7 @@ describe('Stop search', () => {
14681462
.then((infoSpotData) => cy.task('insertInfoSpots', infoSpotData));
14691463
});
14701464

1471-
beforeEach(() => setupTestsAndNavigateToPage({ pageSize: 100 }, true));
1465+
beforeEach(() => setupTestsAndNavigateToPage({ pageSize: 100 }));
14721466

14731467
function shouldHaveResultWithout(
14741468
...unexpectedStops: ReadonlyArray<string | ReadonlyArray<string>>
@@ -1782,7 +1776,7 @@ describe('Stop search', () => {
17821776
}
17831777

17841778
it('Should show and zoom-in on a tightly packed result set', () => {
1785-
setupTestsAndNavigateToPage({ pageSize: 100 }, true);
1779+
setupTestsAndNavigateToPage({ pageSize: 100 });
17861780

17871781
searchAndOpenPoleTypeResultsOnAMap(
17881782
StopRegistryShelterType.Urban,
@@ -1794,7 +1788,7 @@ describe('Stop search', () => {
17941788

17951789
it('Should show and zoom-out to a sparsely distributed result set', () => {
17961790
// Simulate situation with many results that don't fit on a single page.
1797-
setupTestsAndNavigateToPage({ pageSize: 2 }, true);
1791+
setupTestsAndNavigateToPage({ pageSize: 2 });
17981792

17991793
searchAndOpenPoleTypeResultsOnAMap(
18001794
StopRegistryShelterType.Post,
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export * from './main';
21
export * from './search';
32
export * from './stops';

ui/src/components/stop-registry/main/StopRegistryMainPage.tsx

Lines changed: 0 additions & 76 deletions
This file was deleted.

ui/src/components/stop-registry/main/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

ui/src/components/stop-registry/search/StopSearchResultsPage.tsx

Lines changed: 77 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { FC } from 'react';
22
import { useTranslation } from 'react-i18next';
3-
import { useDispatch } from 'react-redux';
4-
import { Link, To, useLocation } from 'react-router';
53
import { Container, Row } from '../../../layoutComponents';
6-
import { resetSelectedRowsAction } from '../../../redux';
7-
import { Path } from '../../../router/routeDetails';
84
import { defaultPagingInfo } from '../../../types';
5+
import { CloseIconButton } from '../../../uiComponents';
6+
import { areEqual } from '../../../utils';
97
import { PageTitle } from '../../common';
10-
import { useToggle } from '../../common/hooks/useToggle';
8+
import { OpenDefaultMapButton } from '../../common/OpenDefaultMapButton';
119
import { StopsByLineSearchResults } from './by-line';
1210
import { StopSearchByStopResults } from './by-stop';
1311
import { StopSearchBar } from './components';
@@ -17,28 +15,55 @@ import {
1715
SearchBy,
1816
SearchFor,
1917
StopSearchFilters,
20-
StopSearchNavigationState,
2118
StopSearchResultsProps,
2219
defaultFilters,
2320
defaultSortingInfo,
2421
} from './types';
25-
import { trSearchFor, useStopSearchUrlState } from './utils';
22+
import { trSearchFor, useStopSearchRouterState } from './utils';
2623

2724
const testIds = {
2825
container: 'StopSearchResultsPage::Container',
2926
closeButton: 'StopSearchResultsPage::closeButton',
3027
};
3128

32-
function useCloseLink(): To {
33-
const { search } = useLocation();
34-
return { pathname: Path.stopRegistry, search };
35-
}
29+
const NoFiltersHeader: FC = () => {
30+
const { t } = useTranslation();
3631

37-
function getNavigationState(
38-
searchIsExpanded: boolean,
39-
): StopSearchNavigationState {
40-
return { searchExpanded: searchIsExpanded };
41-
}
32+
return (
33+
<Row className="justify-between">
34+
<PageTitle.H1>{t('stops.stops')}</PageTitle.H1>
35+
<OpenDefaultMapButton />
36+
</Row>
37+
);
38+
};
39+
40+
type ActiveFiltersHeaderProps = {
41+
readonly resetSearch: () => void;
42+
readonly searchFor: SearchFor;
43+
};
44+
45+
const ActiveFiltersHeader: FC<ActiveFiltersHeaderProps> = ({
46+
resetSearch,
47+
searchFor,
48+
}) => {
49+
const { t } = useTranslation();
50+
51+
return (
52+
<Row className="justify-between">
53+
<PageTitle.H1>
54+
{t('search.searchResultsTitleFor', {
55+
for: trSearchFor(t, searchFor),
56+
})}
57+
</PageTitle.H1>
58+
<CloseIconButton
59+
className="text-lg font-bold text-brand"
60+
label={t('close')}
61+
onClick={resetSearch}
62+
testId={testIds.closeButton}
63+
/>
64+
</Row>
65+
);
66+
};
4267

4368
const Results: FC<StopSearchResultsProps> = ({
4469
filters,
@@ -95,26 +120,19 @@ const Results: FC<StopSearchResultsProps> = ({
95120
};
96121

97122
export const StopSearchResultPage: FC = () => {
98-
const { t } = useTranslation();
99-
const dispatch = useDispatch();
100-
const location = useLocation();
101-
102-
const closeLink = useCloseLink();
103123
const {
104124
state: { filters, pagingInfo, sortingInfo },
105-
setFlatState,
125+
historyState: { searchIsExpanded },
126+
setHistoryState,
106127
setPagingInfo,
128+
setSearchState,
107129
setSortingInfo,
108-
} = useStopSearchUrlState();
130+
} = useStopSearchRouterState();
109131

110-
const [searchIsExpanded, toggleSearchIsExpanded] = useToggle(
111-
location.state?.searchExpanded,
112-
);
132+
const hasActiveFilters = !areEqual(defaultFilters, filters);
113133

114134
const onSubmitFilters = (nextFilters: StopSearchFilters) => {
115-
dispatch(resetSelectedRowsAction());
116-
117-
setFlatState({
135+
setSearchState({
118136
...defaultFilters,
119137
...nextFilters,
120138
...defaultSortingInfo,
@@ -125,34 +143,42 @@ export const StopSearchResultPage: FC = () => {
125143

126144
return (
127145
<Container testId={testIds.container}>
128-
<Row>
129-
<PageTitle.H1>
130-
{`${t('search.searchResultsTitle')} | ${trSearchFor(t, filters.searchFor)}`}
131-
</PageTitle.H1>
132-
<Link
133-
className="ml-auto text-base font-bold text-brand"
134-
to={closeLink}
135-
data-testid={testIds.closeButton}
136-
state={getNavigationState(searchIsExpanded)}
137-
>
138-
{t('close')}
139-
<i className="icon-close-large ml-4 text-lg" />
140-
</Link>
141-
</Row>
146+
{hasActiveFilters ? (
147+
<ActiveFiltersHeader
148+
resetSearch={() =>
149+
setSearchState({
150+
...defaultFilters,
151+
...defaultSortingInfo,
152+
...defaultPagingInfo,
153+
})
154+
}
155+
searchFor={filters.searchFor}
156+
/>
157+
) : (
158+
<NoFiltersHeader />
159+
)}
160+
142161
<StopSearchBar
143162
initialFilters={filters}
144163
searchIsExpanded={searchIsExpanded}
145-
toggleSearchIsExpanded={toggleSearchIsExpanded}
164+
toggleSearchIsExpanded={() =>
165+
setHistoryState((p) => ({
166+
...p,
167+
searchIsExpanded: !p.searchIsExpanded,
168+
}))
169+
}
146170
onSubmit={onSubmitFilters}
147171
/>
148172

149-
<Results
150-
filters={filters}
151-
pagingInfo={pagingInfo}
152-
setPagingInfo={setPagingInfo}
153-
setSortingInfo={setSortingInfo}
154-
sortingInfo={sortingInfo}
155-
/>
173+
{hasActiveFilters && (
174+
<Results
175+
filters={filters}
176+
pagingInfo={pagingInfo}
177+
setPagingInfo={setPagingInfo}
178+
setSortingInfo={setSortingInfo}
179+
sortingInfo={sortingInfo}
180+
/>
181+
)}
156182
</Container>
157183
);
158184
};

ui/src/components/stop-registry/search/types/StopSearchNavigationState.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

ui/src/components/stop-registry/search/types/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ export * from './SearchFor';
33
export * from './SortingInfo';
44
export * from './SortStopsBy';
55
export * from './StopSearchFilters';
6-
export * from './StopSearchNavigationState';
76
export * from './StopSearchResultsProps';
87
export * from './StringMunicipality';
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from './trSearchFor';
22
export * from './useNumericSortingCollator';
3-
export * from './useStopSearchUrlState';
3+
export * from './useStopSearchRouterState';

0 commit comments

Comments
 (0)