Skip to content

Commit 8b14d26

Browse files
fix: add filter when retrieving beaches
1 parent dcb9d2d commit 8b14d26

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

frontend/src/api/beachesAPI.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff 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) {

frontend/src/features/LocateOnMap/SearchLocation.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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) {

frontend/src/features/LocateOnMap/hook/useBeaches.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)