File tree Expand file tree Collapse file tree 3 files changed +26
-6
lines changed
Expand file tree Collapse file tree 3 files changed +26
-6
lines changed Original file line number Diff line number Diff line change @@ -6,14 +6,34 @@ export function throwIrretrievableBeachesError(e) {
66 throw Error ( `Nous n'avons pas pu récupérer les plages: ${ e } ` )
77}
88
9- export const getBeachesFromAPI = async ( ) => {
9+ function sanitizeQuery ( query : string ) {
10+ return query . replace ( / ' / g, "''" )
11+ }
12+
13+ export const getBeachesFromAPI = async ( query : string | undefined ) => {
1014 const geoserverURL = import . meta. env . FRONTEND_GEOSERVER_REMOTE_URL
1115
16+ if ( ! query ) {
17+ return Promise . resolve ( [ ] )
18+ }
19+
20+ const searchQueries = query
21+ . trim ( )
22+ . split ( / [ \s , . \- ; : ( ) ] + / )
23+ . filter ( t => t . length > 0 )
24+
25+ const conditions = searchQueries . map ( searchQuery => {
26+ const sanitizedQuery = sanitizeQuery ( searchQuery )
27+
28+ return `(name ILIKE '%${ sanitizedQuery } %' OR official_name ILIKE '%${ sanitizedQuery } %' OR postcode ILIKE '%${ sanitizedQuery } %')`
29+ } )
30+ const cqlFilter = conditions . join ( ' AND ' )
31+
1232 return fetch (
1333 `${ geoserverURL } /geoserver/wfs?service=WFS&` +
1434 `version=1.1.0&request=GetFeature&typename=${
1535 import . meta. env . FRONTEND_GEOSERVER_NAMESPACE
16- } :beaches&outputFormat=application/json&srsname=${ WSG84_PROJECTION } `
36+ } :beaches&outputFormat=application/json&srsname=${ WSG84_PROJECTION } &CQL_FILTER= ${ encodeURIComponent ( cqlFilter ) } `
1737 )
1838 . then ( response => {
1939 if ( response . status === OK ) {
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ export function SearchLocation() {
1616 const locateOnMap = useAppSelector ( state => state . map . locateOnMap )
1717 const [ searchedLocation , setSearchedLocation ] = useState < string | undefined > ( undefined )
1818 const results = useGooglePlacesAPI ( searchedLocation )
19- const { beaches, options : beachesOptions } = useBeaches ( )
19+ const { beaches, options : beachesOptions } = useBeaches ( searchedLocation )
2020
2121 const handleSelectLocation = async ( location : { id : string ; name : string } | undefined ) => {
2222 if ( ! location || ! location ?. id ) {
Original file line number Diff line number Diff line change @@ -6,12 +6,12 @@ type Options = {
66 value : string
77}
88
9- export const useBeaches = ( ) => {
9+ export const useBeaches = ( query : string | undefined ) => {
1010 const [ options , setOptions ] = useState < Options [ ] > ( [ ] )
1111 const [ beaches , setBeaches ] = useState < any [ ] > ( [ ] )
1212
1313 useEffect ( ( ) => {
14- getBeachesFromAPI ( )
14+ getBeachesFromAPI ( query )
1515 . then ( values => {
1616 setBeaches ( values )
1717 setOptions (
@@ -33,7 +33,7 @@ export const useBeaches = () => {
3333 . catch ( ( ) => {
3434 setOptions ( [ ] )
3535 } )
36- } , [ ] )
36+ } , [ query ] )
3737
3838 return { beaches, options }
3939}
You can’t perform that action at this time.
0 commit comments