11import { FC } from 'react' ;
22import { useTranslation } from 'react-i18next' ;
3- import { useDispatch } from 'react-redux' ;
4- import { Link , To , useLocation } from 'react-router' ;
53import { Container , Row } from '../../../layoutComponents' ;
6- import { resetSelectedRowsAction } from '../../../redux' ;
7- import { Path } from '../../../router/routeDetails' ;
84import { defaultPagingInfo } from '../../../types' ;
5+ import { CloseIconButton } from '../../../uiComponents' ;
6+ import { areEqual } from '../../../utils' ;
97import { PageTitle } from '../../common' ;
10- import { useToggle } from '../../common/hooks/useToggle ' ;
8+ import { OpenDefaultMapButton } from '../../common/OpenDefaultMapButton ' ;
119import { StopsByLineSearchResults } from './by-line' ;
1210import { StopSearchByStopResults } from './by-stop' ;
1311import { 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
2724const 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
4368const Results : FC < StopSearchResultsProps > = ( {
4469 filters,
@@ -95,26 +120,19 @@ const Results: FC<StopSearchResultsProps> = ({
95120} ;
96121
97122export 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} ;
0 commit comments