1
1
import { FormEvent , MouseEvent , useEffect , useRef , useState } from 'react' ;
2
2
3
- import { analyticsEvent } from '../helpers' ;
3
+ import { useNavigate } from 'react-router-dom' ;
4
+
5
+ import { analyticsEvent , formatUrl } from '../helpers' ;
4
6
import { useData , useInput , useSettings } from '../hooks' ;
5
7
import {
6
8
controlsCss ,
@@ -18,9 +20,10 @@ import Dropdown from './Dropdown';
18
20
19
21
export default function Controls ( ) {
20
22
const { capabilities, meetings, slugs } = useData ( ) ;
23
+ const navigate = useNavigate ( ) ;
21
24
const { settings, strings } = useSettings ( ) ;
22
25
const [ dropdown , setDropdown ] = useState < string > ( ) ;
23
- const { input, setInput } = useInput ( ) ;
26
+ const { input } = useInput ( ) ;
24
27
const [ search , setSearch ] = useState ( input . search ) ;
25
28
const searchInput = useRef < HTMLInputElement > ( null ) ;
26
29
@@ -78,7 +81,7 @@ export default function Controls() {
78
81
79
82
if ( value === input . search ) return ;
80
83
81
- setInput ( input => ( { ...input , search : value } ) ) ;
84
+ navigate ( formatUrl ( { ...input , search : value } , settings ) ) ;
82
85
} , [ searchInput . current ?. value ] ) ;
83
86
84
87
// update search when global state changes
@@ -106,7 +109,7 @@ export default function Controls() {
106
109
} ) ;
107
110
}
108
111
109
- setInput ( input => ( { ...input , search } ) ) ;
112
+ navigate ( formatUrl ( { ...input , search } , settings ) ) ;
110
113
} ;
111
114
112
115
// set search mode dropdown and reset distances
@@ -127,7 +130,7 @@ export default function Controls() {
127
130
// focus after waiting for disabled to clear
128
131
setTimeout ( ( ) => searchInput . current ?. focus ( ) , 100 ) ;
129
132
130
- setInput ( input => ( {
133
+ const newInput = {
131
134
...input ,
132
135
distance :
133
136
mode === 'search'
@@ -136,13 +139,9 @@ export default function Controls() {
136
139
mode,
137
140
region : mode === 'search' ? input . region : [ ] ,
138
141
search,
139
- } ) ) ;
140
- } ;
142
+ } ;
141
143
142
- // toggle list/map view
143
- const setView = ( e : MouseEvent , view : 'table' | 'map' ) => {
144
- e . preventDefault ( ) ;
145
- setInput ( input => ( { ...input , view } ) ) ;
144
+ navigate ( formatUrl ( newInput , settings ) ) ;
146
145
} ;
147
146
148
147
return ! slugs . length ? null : (
@@ -224,7 +223,7 @@ export default function Controls() {
224
223
css = { index ? controlsGroupLastCss : controlsGroupFirstCss }
225
224
data-active = { input . view === view }
226
225
key = { view }
227
- onClick = { e => setView ( e , view ) }
226
+ onClick = { ( ) => navigate ( formatUrl ( { ... input , view } , settings ) ) }
228
227
type = "button"
229
228
>
230
229
{ strings . views [ view ] }
0 commit comments