1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-22 14:29:36 +02:00
AdventureLog/frontend/src/lib/components/PointSelectionModal.svelte

161 lines
4.2 KiB
Svelte
Raw Normal View History

2024-07-08 11:44:39 -04:00
<script lang="ts">
// @ts-nocheck
2024-07-27 09:11:21 -04:00
import type { Adventure, OpenStreetMapPlace, Point } from '$lib/types';
2024-07-08 11:44:39 -04:00
import { createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher();
import { onMount } from 'svelte';
let modal: HTMLDialogElement;
2024-07-22 10:56:18 -04:00
import { appVersion } from '$lib/config';
2024-07-08 11:44:39 -04:00
import { DefaultMarker, MapEvents, MapLibre, Popup } from 'svelte-maplibre';
let markers: Point[] = [];
2024-07-19 09:05:47 -04:00
export let query: string | null = null;
2024-07-27 09:11:21 -04:00
export let adventure: Adventure;
2024-07-19 09:05:47 -04:00
if (query) {
geocode();
}
2024-07-18 15:22:51 -04:00
2024-07-08 11:44:39 -04:00
function addMarker(e: CustomEvent<MouseEvent>) {
markers = [];
2024-07-27 09:11:21 -04:00
markers = [...markers, { lngLat: e.detail.lngLat, name: '' }];
2024-07-08 11:44:39 -04:00
console.log(markers);
}
onMount(() => {
modal = document.getElementById('my_modal_1') as HTMLDialogElement;
if (modal) {
modal.showModal();
}
2024-07-27 09:29:47 -04:00
if (adventure.longitude && adventure.latitude) {
2024-07-27 09:11:21 -04:00
markers = [
{
2024-07-27 09:29:47 -04:00
lngLat: { lng: adventure.longitude, lat: adventure.latitude },
2024-07-27 09:11:21 -04:00
name: adventure.name,
location: adventure.location
}
];
2024-07-08 11:44:39 -04:00
}
});
function close() {
dispatch('close');
}
function handleKeydown(event: KeyboardEvent) {
if (event.key === 'Escape') {
dispatch('close');
}
}
2024-07-18 15:22:51 -04:00
let places: OpenStreetMapPlace[] = [];
2024-07-19 09:05:47 -04:00
async function geocode(e: Event | null) {
if (e) {
e.preventDefault();
}
2024-07-18 15:22:51 -04:00
if (!query) {
alert('Please enter a location');
return;
}
2024-07-22 10:56:18 -04:00
let res = await fetch(`https://nominatim.openstreetmap.org/search?q=${query}&format=jsonv2`, {
headers: {
'User-Agent': `AdventureLog / ${appVersion} `
}
});
2024-07-18 15:22:51 -04:00
console.log(res);
let data = (await res.json()) as OpenStreetMapPlace[];
places = data;
}
2024-07-08 11:44:39 -04:00
function submit() {
if (markers.length === 0) {
alert('Please select a point on the map');
return;
}
2024-07-27 09:11:21 -04:00
console.log(markers[0]);
2024-07-27 09:29:47 -04:00
adventure.longitude = markers[0].lngLat.lng;
adventure.latitude = markers[0].lngLat.lat;
2024-07-27 09:11:21 -04:00
if (!adventure.location) {
adventure.location = markers[0].location;
}
if (!adventure.name) {
adventure.name = markers[0].name;
}
2024-07-27 18:42:52 -04:00
if (adventure.type == 'visited' || adventure.type == 'planned') {
adventure.activity_types = [...adventure.activity_types, markers[0].activity_type];
}
2024-07-27 09:11:21 -04:00
dispatch('submit', adventure);
close();
2024-07-08 11:44:39 -04:00
}
</script>
<dialog id="my_modal_1" class="modal">
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
2024-07-18 15:22:51 -04:00
<div class="modal-box w-11/12 max-w-4xl" role="dialog" on:keydown={handleKeydown} tabindex="0">
<form on:submit={geocode}>
<input
type="text"
placeholder="Seach for a location"
class="input input-bordered w-full max-w-xs"
id="search"
name="search"
bind:value={query}
/>
<button type="submit">Search</button>
</form>
2024-07-08 11:44:39 -04:00
<h3 class="font-bold text-lg mb-4">Choose a Point</h3>
<MapLibre
2024-10-17 15:14:15 -04:00
style="https://basemaps.cartocdn.com/gl/voyager-gl-style/style.json"
class="relative aspect-[9/16] max-h-[70vh] w-full sm:aspect-video sm:max-h-full rounded-lg"
2024-07-08 11:44:39 -04:00
standardControls
>
<!-- MapEvents gives you access to map events even from other components inside the map,
where you might not have access to the top-level `MapLibre` component. In this case
it would also work to just use on:click on the MapLibre component itself. -->
<MapEvents on:click={addMarker} />
{#each markers as marker}
<DefaultMarker lngLat={marker.lngLat} />
{/each}
</MapLibre>
2024-07-18 15:22:51 -04:00
{#if places.length > 0}
<div class="mt-4">
<h3 class="font-bold text-lg mb-4">Search Results</h3>
<ul>
{#each places as place}
<li>
<button
class="btn btn-neutral mb-2"
on:click={() => {
markers = [
{
lngLat: { lng: Number(place.lon), lat: Number(place.lat) },
2024-07-27 09:11:21 -04:00
location: place.display_name,
name: place.name,
activity_type: place.type
2024-07-18 15:22:51 -04:00
}
];
}}
>
{place.display_name}
</button>
</li>
{/each}
</ul>
</div>
{:else}
<p class="text-error text-lg">No results found</p>
{/if}
2024-07-08 11:44:39 -04:00
<div class="mb-4 mt-4"></div>
<button class="btn btn-primary" on:click={submit}>Submit</button>
<button class="btn btn-neutral" on:click={close}>Close</button>
</div>
</dialog>