From 707a60f03cfe52a19d973e2139c8d3d7e34c51fd Mon Sep 17 00:00:00 2001 From: Xavier Jp Date: Tue, 28 May 2024 11:32:21 +0200 Subject: [PATCH] fix: only display marker when coords exist (#1074) --- components/map/check-lat-lng.ts | 27 ++++++++++++ components/map/map-etablissement.tsx | 26 +---------- components/map/map-results.tsx | 65 ++++++++++++++++------------ 3 files changed, 67 insertions(+), 51 deletions(-) create mode 100644 components/map/check-lat-lng.ts diff --git a/components/map/check-lat-lng.ts b/components/map/check-lat-lng.ts new file mode 100644 index 000000000..0db1cb7bb --- /dev/null +++ b/components/map/check-lat-lng.ts @@ -0,0 +1,27 @@ +import { LngLatLike } from 'maplibre-gl'; + +export function checkLatLng( + latitude: string, + longitude: string +): LngLatLike | null { + try { + const lat = parseFloat(latitude); + const lng = parseFloat(longitude); + if ( + isNaN(lat) || + isNaN(lng) || + lat < -90 || + lat > 90 || + lng < -180 || + lng > 180 + ) { + throw new Error('Invalid coords'); + } + + return { + lat, + lng, + }; + } catch {} + return null; +} diff --git a/components/map/map-etablissement.tsx b/components/map/map-etablissement.tsx index a856a7ecf..72aadfc26 100644 --- a/components/map/map-etablissement.tsx +++ b/components/map/map-etablissement.tsx @@ -4,34 +4,12 @@ import constants from '#models/constants'; import { IEtablissement } from '#models/core/types'; import { formatSiret } from '#utils/helpers'; -import maplibregl, { LngLatLike, Map } from 'maplibre-gl'; +import maplibregl, { Map } from 'maplibre-gl'; import 'maplibre-gl/dist/maplibre-gl.css'; import { useEffect, useRef } from 'react'; +import { checkLatLng } from './check-lat-lng'; import './map.css'; -function checkLatLng(latitude: string, longitude: string): LngLatLike | null { - try { - const lat = parseFloat(latitude); - const lng = parseFloat(longitude); - if ( - isNaN(lat) || - isNaN(lng) || - lat < -90 || - lat > 90 || - lng < -180 || - lng > 180 - ) { - throw new Error('Invalid coords'); - } - - return { - lat, - lng, - }; - } catch {} - return null; -} - export function MapEtablissement({ etablissement, }: { diff --git a/components/map/map-results.tsx b/components/map/map-results.tsx index d6eefc636..07d96622e 100644 --- a/components/map/map-results.tsx +++ b/components/map/map-results.tsx @@ -7,6 +7,7 @@ import { formatIntFr, formatSiret } from '#utils/helpers'; import maplibregl, { Map } from 'maplibre-gl'; import 'maplibre-gl/dist/maplibre-gl.css'; import { useEffect, useRef } from 'react'; +import { checkLatLng } from './check-lat-lng'; import './map.css'; export default function MapWithResults({ @@ -35,42 +36,52 @@ export default function MapWithResults({ }); results.results.forEach((result) => { - var popup = new maplibregl.Popup({ offset: 25 }).setHTML( - `
${formatIntFr( - result.siren - )}
${result.nomComplet}
📍${ - result.siege.adresse - }
` + const coordsSiege = checkLatLng( + result.siege.latitude, + result.siege.longitude ); - new maplibregl.Marker({ color: constants.colors.frBlue }) - //@ts-ignore - .setLngLat([result.siege.longitude, result.siege.latitude]) - .setPopup(popup) - //@ts-ignore - .addTo(map.current); + if (coordsSiege) { + const popup = new maplibregl.Popup({ offset: 25 }).setHTML( + `
${formatIntFr( + result.siren + )}
${result.nomComplet}
📍${ + result.siege.adresse + }
` + ); + + new maplibregl.Marker({ color: constants.colors.frBlue }) + //@ts-ignore + .setLngLat([result.siege.longitude, result.siege.latitude]) + .setPopup(popup) + //@ts-ignore + .addTo(map.current); + } result.matchingEtablissements.forEach((match) => { if (match.estSiege) { return null; } - var popup = new maplibregl.Popup({ offset: 25 }).setHTML( - `
${formatSiret( - match.siret - )}
Etablissement secondaire de ${result.nomComplet}
📍${match.adresse}
` - ); + const coordsEtab = checkLatLng(match.latitude, match.longitude); + if (coordsEtab) { + var popup = new maplibregl.Popup({ offset: 25 }).setHTML( + `
${formatSiret( + match.siret + )}
Etablissement secondaire de ${result.nomComplet}
📍${match.adresse}
` + ); - new maplibregl.Marker({ - color: shouldColorZipCode ? 'yellow' : constants.colors.pastelBlue, - }) - //@ts-ignore - .setLngLat([match.longitude, match.latitude]) - .setPopup(popup) - //@ts-ignore - .addTo(map.current); + new maplibregl.Marker({ + color: shouldColorZipCode ? 'yellow' : constants.colors.pastelBlue, + }) + //@ts-ignore + .setLngLat([match.longitude, match.latitude]) + .setPopup(popup) + //@ts-ignore + .addTo(map.current); + } }); }); }, [results, shouldColorZipCode]);