Skip to content

Commit 4ed9d40

Browse files
authored
Merge pull request #2 from Vasie1337/main - stage?
Handle Missing Coordinates and Prevent Invalid LngLat Errors - stage?
2 parents 1da7343 + a0fc4c5 commit 4ed9d40

File tree

1 file changed

+54
-20
lines changed

1 file changed

+54
-20
lines changed

src/app/page.tsx

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ import {
1414
ChevronRightIcon,
1515
ClockIcon,
1616
ServerIcon,
17-
ArrowPathIcon
17+
ArrowPathIcon,
18+
ExclamationCircleIcon
1819
} from "@heroicons/react/24/outline"
1920
import Image from "next/image"
2021

2122
interface ResultItem {
2223
api: string
2324
data: any
2425
time: string
25-
lat: number
26-
lng: number
26+
lat: number | null
27+
lng: number | null
2728
}
2829

2930
import LogoRect from "@/assets/rectangle.svg"
@@ -76,15 +77,22 @@ export default function Home() {
7677
return () => m.remove()
7778
}, [])
7879

79-
function goToLocation(lat: number, lng: number) {
80-
if (map) {
81-
map.flyTo({ center: [lng, lat], zoom: 9, speed: 1.2, curve: 1.4 })
82-
}
80+
function goToLocation(lat: number | null, lng: number | null) {
81+
if (lat === null || lng === null || !map) return;
82+
83+
map.flyTo({
84+
center: [lng, lat],
85+
zoom: 8,
86+
speed: 1.5,
87+
curve: 1.5
88+
})
8389
}
8490

85-
function toggleCard(index: number, lat: number, lng: number) {
86-
goToLocation(lat, lng)
91+
function toggleCard(index: number, lat: number | null, lng: number | null) {
8792
setExpandedCard(expandedCard === index ? null : index)
93+
if (lat !== null && lng !== null) {
94+
goToLocation(lat, lng)
95+
}
8896
}
8997

9098
function getLocationInfo(data: any) {
@@ -138,19 +146,26 @@ export default function Home() {
138146
data?.location?.lat ||
139147
data?.location?.latitude ||
140148
data?.data?.location?.latitude ||
141-
(data.loc ? data.loc.split(",")[0] : 0)
149+
(data.loc ? data.loc.split(",")[0] : null)
142150
const lng =
143151
data.longitude ||
144152
data.lon ||
145153
data?.location?.lng ||
146154
data?.location?.longitude ||
147155
data?.data?.location?.longitude ||
148-
(data.loc ? data.loc.split(",")[1] : 0)
149-
if (lat && lng) {
156+
(data.loc ? data.loc.split(",")[1] : null)
157+
158+
// Ensure both lat and lng are valid numbers before using them
159+
const validLat = lat !== null && lat !== undefined && !isNaN(Number(lat))
160+
const validLng = lng !== null && lng !== undefined && !isNaN(Number(lng))
161+
162+
if (validLat && validLng) {
163+
const numLat = Number(lat)
164+
const numLng = Number(lng)
150165
foundResults = true
151166
if (firstCenter) {
152167
map.flyTo({
153-
center: [+lng, +lat],
168+
center: [numLng, numLat],
154169
zoom: 8,
155170
speed: 1.5,
156171
curve: 1.5
@@ -171,7 +186,7 @@ export default function Home() {
171186
</div>
172187
`
173188
new maplibregl.Marker({ element: el })
174-
.setLngLat([+lng, +lat])
189+
.setLngLat([numLng, numLat])
175190
.setPopup(
176191
new maplibregl.Popup({ offset: 25, className: "custom-popup" }).setHTML(`
177192
<div class="text-slate-800">
@@ -187,8 +202,21 @@ export default function Home() {
187202
api,
188203
data,
189204
time: (tEnd - tStart).toFixed(0),
190-
lat: +lat,
191-
lng: +lng
205+
lat: numLat,
206+
lng: numLng
207+
}
208+
])
209+
} else {
210+
console.log(`No valid coordinates found for ${api}:`, { lat, lng })
211+
// Add the result to the list but mark it as invalid location
212+
setResults((prev) => [
213+
...prev,
214+
{
215+
api,
216+
data,
217+
time: (tEnd - tStart).toFixed(0),
218+
lat: null,
219+
lng: null
192220
}
193221
])
194222
}
@@ -420,8 +448,9 @@ export default function Home() {
420448
</span>
421449
</div>
422450
) : (
423-
<div className="text-slate-400 text-xs">
424-
Location data available
451+
<div className="flex items-center text-red-500 text-xs">
452+
<ExclamationCircleIcon className="w-3.5 h-3.5 mr-1 flex-shrink-0" />
453+
<span>No location data available</span>
425454
</div>
426455
)}
427456
</div>
@@ -441,8 +470,13 @@ export default function Home() {
441470
e.stopPropagation()
442471
goToLocation(r.lat, r.lng)
443472
}}
444-
className="mr-3 flex items-center justify-center w-9 h-9 rounded-lg bg-slate-50 hover:bg-primary-50 border border-slate-200 hover:border-primary-200 transition-colors shadow-sm"
445-
title="Center on map"
473+
disabled={!r.lat || !r.lng}
474+
className={`mr-3 flex items-center justify-center w-9 h-9 rounded-lg ${
475+
r.lat && r.lng
476+
? "bg-slate-50 hover:bg-primary-50 border border-slate-200 hover:border-primary-200"
477+
: "bg-slate-50 border border-slate-200 opacity-50 cursor-not-allowed"
478+
} transition-colors shadow-sm`}
479+
title={r.lat && r.lng ? "Center on map" : "No location data"}
446480
>
447481
<MapPinIcon className="h-4 w-4 text-primary-500" />
448482
</motion.button>

0 commit comments

Comments
 (0)