1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-20 21:39:37 +02:00
AdventureLog/frontend/src/lib/components/EditAdventure.svelte

507 lines
14 KiB
Svelte
Raw Normal View History

2024-07-08 11:44:39 -04:00
<script lang="ts">
import { createEventDispatcher } from 'svelte';
2024-08-17 11:12:15 -04:00
import type { Adventure, OpenStreetMapPlace, Point } from '$lib/types';
2024-07-08 11:44:39 -04:00
import { onMount } from 'svelte';
2024-08-17 11:12:15 -04:00
import { enhance } from '$app/forms';
2024-07-08 11:44:39 -04:00
import { addToast } from '$lib/toasts';
2024-08-17 11:12:15 -04:00
export let longitude: number | null = null;
export let latitude: number | null = null;
import { DefaultMarker, MapEvents, MapLibre } from 'svelte-maplibre';
let markers: Point[] = [];
let query: string = '';
let places: OpenStreetMapPlace[] = [];
let images: { id: string; image: string }[] = [];
import Earth from '~icons/mdi/earth';
import ActivityComplete from './ActivityComplete.svelte';
import { appVersion } from '$lib/config';
2024-08-17 13:30:00 -04:00
import NewAdventure from './NewAdventure.svelte';
2024-07-08 11:44:39 -04:00
export let startDate: string | null = null;
export let endDate: string | null = null;
2024-08-17 11:12:15 -04:00
export let adventureToEdit: Adventure;
images = adventureToEdit.images || [];
2024-07-08 11:44:39 -04:00
2024-08-17 11:12:15 -04:00
if (longitude && latitude) {
adventureToEdit.latitude = latitude;
adventureToEdit.longitude = longitude;
reverseGeocode();
}
2024-07-08 11:44:39 -04:00
2024-08-17 11:12:15 -04:00
$: {
if (!adventureToEdit.rating) {
adventureToEdit.rating = NaN;
}
}
2024-08-17 13:30:00 -04:00
async function removeImage(id: string) {
2024-08-17 13:59:49 -04:00
let res = await fetch(`/api/images/${id}/image_delete`, {
method: 'POST'
2024-08-17 13:30:00 -04:00
});
2024-08-17 13:59:49 -04:00
if (res.status === 204) {
2024-08-17 13:30:00 -04:00
images = images.filter((image) => image.id !== id);
2024-08-17 13:59:49 -04:00
adventureToEdit.images = images;
console.log(images);
2024-08-17 13:30:00 -04:00
addToast('success', 'Image removed');
} else {
addToast('error', 'Failed to remove image');
}
}
2024-08-17 11:12:15 -04:00
let isDetails: boolean = true;
function saveAndClose() {
dispatch('saveEdit', adventureToEdit);
close();
}
$: if (markers.length > 0) {
adventureToEdit.latitude = Math.round(markers[0].lngLat.lat * 1e6) / 1e6;
adventureToEdit.longitude = Math.round(markers[0].lngLat.lng * 1e6) / 1e6;
if (!adventureToEdit.location) {
adventureToEdit.location = markers[0].location;
}
if (!adventureToEdit.name) {
adventureToEdit.name = markers[0].name;
}
}
async function geocode(e: Event | null) {
if (e) {
e.preventDefault();
}
if (!query) {
alert('Please enter a location');
return;
}
let res = await fetch(`https://nominatim.openstreetmap.org/search?q=${query}&format=jsonv2`, {
headers: {
'User-Agent': `AdventureLog / ${appVersion} `
}
});
console.log(res);
let data = (await res.json()) as OpenStreetMapPlace[];
places = data;
}
async function reverseGeocode() {
let res = await fetch(
`https://nominatim.openstreetmap.org/search?q=${adventureToEdit.latitude},${adventureToEdit.longitude}&format=jsonv2`,
{
headers: {
'User-Agent': `AdventureLog / ${appVersion} `
}
}
);
let data = (await res.json()) as OpenStreetMapPlace[];
if (data.length > 0) {
adventureToEdit.name = data[0]?.name || '';
adventureToEdit.activity_types?.push(data[0]?.type || '');
adventureToEdit.location = data[0]?.display_name || '';
}
console.log(data);
}
2024-07-16 15:38:07 -04:00
let fileInput: HTMLInputElement;
2024-07-08 11:44:39 -04:00
2024-08-17 11:12:15 -04:00
const dispatch = createEventDispatcher();
let modal: HTMLDialogElement;
2024-07-08 11:44:39 -04:00
onMount(async () => {
modal = document.getElementById('my_modal_1') as HTMLDialogElement;
if (modal) {
modal.showModal();
}
});
function close() {
dispatch('close');
}
function handleKeydown(event: KeyboardEvent) {
if (event.key === 'Escape') {
close();
}
}
2024-08-17 11:12:15 -04:00
// async function generateDesc() {
// let res = await fetch(`/api/generate/desc/?name=${adventureToEdit.name}`);
// let data = await res.json();
// if (data.extract) {
// adventureToEdit.description = data.extract;
// }
// }
2024-07-08 11:44:39 -04:00
2024-08-17 11:12:15 -04:00
function addMarker(e: CustomEvent<any>) {
markers = [];
markers = [...markers, { lngLat: e.detail.lngLat, name: '', location: '', activity_type: '' }];
console.log(markers);
}
2024-07-08 11:44:39 -04:00
2024-08-17 11:12:15 -04:00
function imageSubmit() {
return async ({ result }: any) => {
if (result.type === 'success') {
if (result.data.id && result.data.image) {
adventureToEdit.images = [...adventureToEdit.images, result.data];
images = [...images, result.data];
addToast('success', 'Image uploaded');
2024-07-08 11:44:39 -04:00
2024-08-17 11:12:15 -04:00
fileInput.value = '';
console.log(adventureToEdit);
} else {
addToast('error', result.data.error || 'Failed to upload image');
2024-07-08 11:44:39 -04:00
}
}
2024-08-17 11:12:15 -04:00
};
2024-07-08 11:44:39 -04:00
}
2024-07-16 15:38:07 -04:00
2024-08-17 11:12:15 -04:00
async function handleSubmit(event: Event) {
event.preventDefault();
console.log(adventureToEdit);
let res = await fetch(`/api/adventures/${adventureToEdit.id}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(adventureToEdit)
});
let data = await res.json();
if (data.id) {
adventureToEdit = data as Adventure;
isDetails = false;
addToast('success', 'Adventure updated');
} else {
addToast('error', 'Failed to update adventure');
2024-07-16 15:38:07 -04:00
}
2024-07-08 11:44:39 -04:00
}
</script>
2024-08-17 11:12:15 -04:00
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
2024-07-08 11:44:39 -04:00
<dialog id="my_modal_1" class="modal">
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
2024-08-17 11:12:15 -04:00
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
2024-08-17 14:13:23 -04:00
<div class="modal-box w-11/12 max-w-2xl" role="dialog" on:keydown={handleKeydown} tabindex="0">
2024-08-17 13:30:00 -04:00
<h3 class="font-bold text-lg">Edit {adventureToEdit.type} Adventure</h3>
2024-08-17 11:12:15 -04:00
{#if adventureToEdit.id === '' || isDetails}
<div class="modal-action items-center">
<form method="post" style="width: 100%;" on:submit={handleSubmit}>
<!-- Grid layout for form fields -->
<h2 class="text-2xl font-semibold mb-2">Basic Information</h2>
2024-08-17 14:13:23 -04:00
<!-- <div class="grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-3"> -->
<div>
<label for="name">Name</label><br />
<input
type="text"
id="name"
name="name"
bind:value={adventureToEdit.name}
class="input input-bordered w-full"
required
/>
</div>
<div>
<div class="form-control">
<label class="label cursor-pointer">
<span class="label-text">Visited</span>
2024-08-17 13:30:00 -04:00
<input
2024-08-17 14:13:23 -04:00
type="radio"
name="radio-10"
class="radio checked:bg-red-500"
on:click={() => (adventureToEdit.type = 'visited')}
checked={adventureToEdit.type == 'visited'}
2024-08-17 13:30:00 -04:00
/>
2024-08-17 14:13:23 -04:00
</label>
2024-08-17 13:30:00 -04:00
</div>
2024-08-17 14:13:23 -04:00
<div class="form-control">
<label class="label cursor-pointer">
<span class="label-text">Planned</span>
<input
type="radio"
name="radio-10"
class="radio checked:bg-blue-500"
on:click={() => (adventureToEdit.type = 'planned')}
checked={adventureToEdit.type == 'planned'}
/>
</label>
2024-08-17 11:12:15 -04:00
</div>
2024-08-17 14:13:23 -04:00
</div>
<div>
<label for="date">Date</label><br />
<input
type="date"
id="date"
name="date"
min={startDate || ''}
max={endDate || ''}
bind:value={adventureToEdit.date}
class="input input-bordered w-full"
/>
</div>
<div>
<!-- link -->
2024-08-17 11:12:15 -04:00
<div>
2024-08-17 14:13:23 -04:00
<label for="link">Link</label><br />
2024-08-17 11:12:15 -04:00
<input
type="text"
2024-08-17 14:13:23 -04:00
id="link"
name="link"
bind:value={adventureToEdit.link}
2024-08-17 11:12:15 -04:00
class="input input-bordered w-full"
/>
</div>
2024-08-17 14:13:23 -04:00
</div>
<div>
<label for="description">Description</label><br />
<textarea
id="description"
name="description"
bind:value={adventureToEdit.description}
class="textarea textarea-bordered w-full h-32"
></textarea>
</div>
<div>
<label for="activity_types">Activity Types</label><br />
<input
type="text"
id="activity_types"
name="activity_types"
hidden
bind:value={adventureToEdit.activity_types}
class="input input-bordered w-full"
/>
<ActivityComplete bind:activities={adventureToEdit.activity_types} />
</div>
<div>
<label for="rating"
>Rating <iconify-icon icon="mdi:star" class="text-xl -mb-1"></iconify-icon></label
><br />
<input
type="number"
min="0"
max="5"
hidden
bind:value={adventureToEdit.rating}
id="rating"
name="rating"
class="input input-bordered w-full max-w-xs mt-1"
/>
<div class="rating -ml-3 mt-1">
2024-08-15 21:29:36 -04:00
<input
2024-08-17 14:13:23 -04:00
type="radio"
name="rating-2"
class="rating-hidden"
checked={Number.isNaN(adventureToEdit.rating)}
2024-08-15 21:29:36 -04:00
/>
2024-08-17 14:13:23 -04:00
<input
type="radio"
name="rating-2"
class="mask mask-star-2 bg-orange-400"
on:click={() => (adventureToEdit.rating = 1)}
checked={adventureToEdit.rating === 1}
/>
<input
type="radio"
name="rating-2"
class="mask mask-star-2 bg-orange-400"
on:click={() => (adventureToEdit.rating = 2)}
checked={adventureToEdit.rating === 2}
/>
<input
type="radio"
name="rating-2"
class="mask mask-star-2 bg-orange-400"
on:click={() => (adventureToEdit.rating = 3)}
checked={adventureToEdit.rating === 3}
/>
<input
type="radio"
name="rating-2"
class="mask mask-star-2 bg-orange-400"
on:click={() => (adventureToEdit.rating = 4)}
checked={adventureToEdit.rating === 4}
/>
<input
type="radio"
name="rating-2"
class="mask mask-star-2 bg-orange-400"
on:click={() => (adventureToEdit.rating = 5)}
checked={adventureToEdit.rating === 5}
/>
{#if adventureToEdit.rating}
<button
type="button"
class="btn btn-sm btn-error ml-2"
on:click={() => (adventureToEdit.rating = NaN)}
>
Remove
</button>
{/if}
2024-08-15 21:29:36 -04:00
</div>
2024-08-17 14:13:23 -04:00
<!-- </div> -->
2024-08-17 11:12:15 -04:00
</div>
<div class="divider"></div>
<h2 class="text-2xl font-semibold mb-2 mt-4">Location Information</h2>
2024-08-17 14:13:23 -04:00
<!-- <div class="grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-3"> -->
<div>
<label for="latitude">Location</label><br />
<input
type="text"
id="location"
name="location"
bind:value={adventureToEdit.location}
class="input input-bordered w-full"
/>
</div>
<div>
<form on:submit={geocode} class="mt-2">
2024-08-17 11:12:15 -04:00
<input
type="text"
2024-08-17 14:13:23 -04:00
placeholder="Seach for a location"
class="input input-bordered w-full max-w-xs"
id="search"
name="search"
bind:value={query}
2024-08-17 11:12:15 -04:00
/>
2024-08-17 14:13:23 -04:00
<button class="btn btn-neutral -mt-1" type="submit">Search</button>
</form>
2024-08-17 11:12:15 -04:00
</div>
2024-08-17 14:13:23 -04:00
{#if places.length > 0}
<div class="mt-4 max-w-full">
<h3 class="font-bold text-lg mb-4">Search Results</h3>
<div class="flex flex-wrap">
{#each places as place}
<button
type="button"
class="btn btn-neutral mb-2 mr-2 max-w-full break-words whitespace-normal text-left"
on:click={() => {
markers = [
{
lngLat: { lng: Number(place.lon), lat: Number(place.lat) },
location: place.display_name,
name: place.name,
activity_type: place.type
}
];
}}
>
{place.display_name}
</button>
{/each}
</div>
</div>
{:else}
<p class="text-error text-lg">No results found</p>
{/if}
<!-- </div> -->
2024-08-17 11:12:15 -04:00
<div>
<MapLibre
style="https://basemaps.cartocdn.com/gl/positron-gl-style/style.json"
class="relative aspect-[9/16] max-h-[70vh] w-full sm:aspect-video sm:max-h-full"
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>
</div>
2024-08-15 21:29:36 -04:00
2024-08-17 13:30:00 -04:00
<div>
<div>
<div>
<label for="is_public"
>Public <Earth class="inline-block -mt-1 mb-1 w-6 h-6" /></label
><br />
<input
type="checkbox"
class="toggle toggle-primary"
id="is_public"
name="is_public"
bind:checked={adventureToEdit.is_public}
/>
</div>
</div>
</div>
2024-08-17 11:12:15 -04:00
<div class="mt-4">
<button type="submit" class="btn btn-primary">Save & Next</button>
<button type="button" class="btn" on:click={close}>Close</button>
</div>
2024-08-15 21:29:36 -04:00
{#if adventureToEdit.is_public}
2024-08-17 11:12:15 -04:00
<div class="bg-neutral p-4 mt-2 rounded-md shadow-sm">
2024-08-15 21:29:36 -04:00
<p class=" font-semibold">Share this Adventure!</p>
<div class="flex items-center justify-between">
<p class="text-card-foreground font-mono">
{window.location.origin}/adventures/{adventureToEdit.id}
</p>
<button
type="button"
on:click={() => {
navigator.clipboard.writeText(
`${window.location.origin}/adventures/${adventureToEdit.id}`
);
}}
class="inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 h-10 px-4 py-2"
>
Copy Link
</button>
</div>
</div>
{/if}
2024-08-17 11:12:15 -04:00
</form>
</div>
{:else}
<p>Upload images here</p>
<p>{adventureToEdit.id}</p>
<div class="mb-2">
<label for="image">Image </label><br />
<div class="flex">
<form
method="POST"
action="adventures?/image"
use:enhance={imageSubmit}
enctype="multipart/form-data"
2024-08-15 21:29:36 -04:00
>
2024-08-17 11:12:15 -04:00
<input
type="file"
name="image"
class="file-input file-input-bordered w-full max-w-xs"
bind:this={fileInput}
accept="image/*"
id="image"
/>
<input type="hidden" name="adventure" value={adventureToEdit.id} id="adventure" />
<button class="btn btn-neutral mt-2 mb-2" type="submit">Upload Image</button>
</form>
</div>
<div class=" inline-flex gap-2">
{#each images as image}
2024-08-17 13:30:00 -04:00
<!-- remove button -->
<button
type="button"
class="btn btn-error"
on:click={() => {
removeImage(image.id);
}}
/>
2024-08-17 11:12:15 -04:00
<img src={image.image} alt={image.id} class="w-32 h-32" />
{/each}
2024-08-15 21:29:36 -04:00
</div>
2024-08-17 11:12:15 -04:00
</div>
<div class="mt-4">
<button type="button" class="btn btn-primary" on:click={saveAndClose}>Close</button>
</div>
{/if}
2024-07-08 11:44:39 -04:00
</div>
</dialog>