|
1 | 1 | <script lang="ts"> |
| 2 | + import type { GeoApiResponse } from '$api/geo/+server' |
| 3 | + import Link from '$lib/components/Link.svelte' |
2 | 4 | import distance from '@turf/distance' |
3 | 5 | import { onMount } from 'svelte' |
4 | | - import Banner from './Banner.svelte' |
5 | | - import Link from '$lib/components/Link.svelte' |
6 | 6 | import type { CalendarResponse, Event } from '../../routes/api/calendar/+server' |
7 | | - import type { GeoApiResponse } from '$api/geo/+server' |
| 7 | + import Banner from './Banner.svelte' |
8 | 8 |
|
9 | 9 | export let contrast: boolean |
10 | 10 | export let eventFound = false |
11 | | - /** Geo data from Netlify for external use */ |
12 | 11 | export let geo: GeoApiResponse | null = null |
13 | 12 |
|
14 | 13 | const FORMAT = new Intl.DateTimeFormat('en', { day: 'numeric', month: 'long' }) |
15 | 14 | const MAX_DISTANCE_KM = 100 |
16 | 15 |
|
| 16 | + let events: CalendarResponse | null = null |
17 | 17 | let nearbyEvent: Event | null = null |
18 | 18 |
|
19 | 19 | $: eventFound = !!nearbyEvent |
20 | 20 |
|
21 | 21 | 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 | + } |
24 | 28 |
|
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 |
27 | 32 |
|
28 | 33 | const userCoords = [userLatitude, userLongitude] |
29 | 34 |
|
|
34 | 39 | return distance(userCoords, eventCoords, { units: 'kilometers' }) <= MAX_DISTANCE_KM |
35 | 40 | } |
36 | 41 |
|
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 |
42 | 43 | } |
43 | 44 |
|
44 | 45 | async function fetchLuma(): Promise<CalendarResponse> { |
|
0 commit comments