Skip to content

Commit 3269c4b

Browse files
committed
Avoid passing unrelated data upwards
1 parent 70686ee commit 3269c4b

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

src/lib/components/NearbyEvent.svelte

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
<script lang="ts">
2+
import type { GeoApiResponse } from '$api/geo/+server'
3+
import Link from '$lib/components/Link.svelte'
24
import distance from '@turf/distance'
35
import { onMount } from 'svelte'
4-
import Banner from './Banner.svelte'
5-
import Link from '$lib/components/Link.svelte'
66
import type { CalendarResponse, Event } from '../../routes/api/calendar/+server'
7-
import type { GeoApiResponse } from '$api/geo/+server'
7+
import Banner from './Banner.svelte'
88
99
export let contrast: boolean
1010
export let eventFound = false
11-
/** Geo data from Netlify for external use */
1211
export let geo: GeoApiResponse | null = null
1312
1413
const FORMAT = new Intl.DateTimeFormat('en', { day: 'numeric', month: 'long' })
1514
const MAX_DISTANCE_KM = 100
1615
16+
let events: CalendarResponse | null = null
1717
let nearbyEvent: Event | null = null
1818
1919
$: eventFound = !!nearbyEvent
2020
2121
onMount(async () => {
22-
const [geoResult, events] = await Promise.all([fetchGeo(), fetchLuma()])
23-
geo = geoResult
22+
events = await fetchLuma()
23+
})
24+
25+
$: if (geo && events) {
26+
nearbyEvent = findNearbyEvent(geo, events)
27+
}
2428
25-
const { latitude: userLatitude, longitude: userLongitude } = geoResult
26-
if (!userLatitude || !userLongitude) return
29+
function findNearbyEvent(geo: GeoApiResponse, events: CalendarResponse) {
30+
const { latitude: userLatitude, longitude: userLongitude } = geo
31+
if (!userLatitude || !userLongitude) return null
2732
2833
const userCoords = [userLatitude, userLongitude]
2934
@@ -34,11 +39,7 @@
3439
return distance(userCoords, eventCoords, { units: 'kilometers' }) <= MAX_DISTANCE_KM
3540
}
3641
37-
nearbyEvent = events.entries.map((entry) => entry.event).find(isNearby) ?? null
38-
})
39-
40-
async function fetchGeo(): Promise<GeoApiResponse> {
41-
return fetch('/api/geo').then((res) => res.json())
42+
return events.entries.map((entry) => entry.event).find(isNearby) ?? null
4243
}
4344
4445
async function fetchLuma(): Promise<CalendarResponse> {

src/routes/+layout.svelte

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import '@fontsource/saira-condensed/700.css'
1616
import sairaCondensedLatin700 from '@fontsource/saira-condensed/files/saira-condensed-latin-700-normal.woff2'
1717
import { ProgressBar } from '@prgm/sveltekit-progress-bar'
18+
import { onMount } from 'svelte'
1819
import { Toaster } from 'svelte-french-toast'
1920
import '../styles/print.css'
2021
import '../styles/styles.css'
@@ -28,9 +29,14 @@
2829
// This prevents "undefined" issues during navigation
2930
3031
let eventFound: boolean
31-
let geo: GeoApiResponse | null
32+
let geo: GeoApiResponse | null = null
3233
// Show the hero on the homepage, but nowhere else
3334
$: hero = deLocalizeHref($page.url.pathname) === '/'
35+
36+
onMount(async () => {
37+
const response = await fetch('/api/geo')
38+
geo = await response.json()
39+
})
3440
</script>
3541

3642
<PreloadFonts urls={[robotoSlabLatin300, sairaCondensedLatin700]} />
@@ -49,7 +55,7 @@
4955
{@html data.localeAlert.message}
5056
</Banner>
5157
{:else}
52-
<NearbyEvent contrast={hero} bind:eventFound bind:geo />
58+
<NearbyEvent contrast={hero} bind:eventFound {geo} />
5359
{#if !eventFound}
5460
{#if geo?.country?.code === 'US' && false}
5561
<Banner contrast={hero}>

0 commit comments

Comments
 (0)