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/AdventureLink.svelte

67 lines
1.9 KiB
Svelte
Raw Normal View History

2024-07-15 12:09:20 -04:00
<script lang="ts">
import { deserialize } from '$app/forms';
import type { Adventure } from '$lib/types';
import { createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher();
import type { ActionResult } from '@sveltejs/kit';
import { onMount } from 'svelte';
import AdventureCard from './AdventureCard.svelte';
let modal: HTMLDialogElement;
let adventures: Adventure[] = [];
onMount(async () => {
modal = document.getElementById('my_modal_1') as HTMLDialogElement;
if (modal) {
modal.showModal();
}
let formData = new FormData();
formData.append('include_collections', 'false');
let res = await fetch(`/adventures?/all`, {
method: 'POST',
body: formData
});
const result: ActionResult = deserialize(await res.text());
console.log(result);
if (result.type === 'success' && result.data) {
adventures = result.data.adventures as Adventure[];
}
});
function close() {
dispatch('close');
}
function add(event: CustomEvent<Adventure>) {
2024-07-15 18:51:05 -04:00
adventures = adventures.filter((a) => a.id !== event.detail.id);
2024-07-15 12:09:20 -04:00
dispatch('add', event.detail);
}
function handleKeydown(event: KeyboardEvent) {
if (event.key === 'Escape') {
dispatch('close');
}
}
</script>
<dialog id="my_modal_1" class="modal">
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
2024-07-15 18:51:05 -04:00
<div class="modal-box w-11/12 max-w-5xl" role="dialog" on:keydown={handleKeydown} tabindex="0">
2024-07-15 18:01:49 -04:00
<h1 class="text-center font-bold text-4xl mb-6">My Adventures</h1>
2024-07-15 12:09:20 -04:00
<div class="flex flex-wrap gap-4 mr-4 justify-center content-center">
{#each adventures as adventure}
<AdventureCard type="link" {adventure} on:link={add} />
{/each}
2024-07-15 18:51:05 -04:00
{#if adventures.length === 0}
<p class="text-center text-lg">
No adventures found that can be linked to this collection.
</p>
{/if}
2024-07-15 12:09:20 -04:00
</div>
<button class="btn btn-primary" on:click={close}>Close</button>
</div>
</dialog>