2024-07-15 12:09:20 -04:00
|
|
|
<script lang="ts">
|
2024-07-18 18:37:46 -04:00
|
|
|
import type { Adventure, User } from '$lib/types';
|
2024-07-15 12:09:20 -04:00
|
|
|
import { createEventDispatcher } from 'svelte';
|
|
|
|
const dispatch = createEventDispatcher();
|
2024-10-30 15:11:00 -04:00
|
|
|
import { t } from 'svelte-i18n';
|
2024-07-15 12:09:20 -04:00
|
|
|
import { onMount } from 'svelte';
|
|
|
|
import AdventureCard from './AdventureCard.svelte';
|
|
|
|
let modal: HTMLDialogElement;
|
|
|
|
|
|
|
|
let adventures: Adventure[] = [];
|
|
|
|
|
2024-08-11 13:34:32 -04:00
|
|
|
let isLoading: boolean = true;
|
|
|
|
|
2024-07-18 18:37:46 -04:00
|
|
|
export let user: User | null;
|
2025-06-12 15:54:01 -04:00
|
|
|
export let collectionId: string;
|
2024-07-18 18:37:46 -04:00
|
|
|
|
2024-07-15 12:09:20 -04:00
|
|
|
onMount(async () => {
|
|
|
|
modal = document.getElementById('my_modal_1') as HTMLDialogElement;
|
|
|
|
if (modal) {
|
|
|
|
modal.showModal();
|
|
|
|
}
|
2025-06-12 15:54:01 -04:00
|
|
|
let res = await fetch(`/api/adventures/all/?include_collections=true`, {
|
2024-08-11 13:34:32 -04:00
|
|
|
method: 'GET'
|
2024-07-15 12:09:20 -04:00
|
|
|
});
|
|
|
|
|
2024-08-11 13:34:32 -04:00
|
|
|
const newAdventures = await res.json();
|
2024-07-15 12:09:20 -04:00
|
|
|
|
2025-06-12 15:54:01 -04:00
|
|
|
// Filter out adventures that are already linked to the collections
|
|
|
|
// basically for each adventure, check if collections array contains the id of the current collection
|
|
|
|
if (collectionId) {
|
|
|
|
adventures = newAdventures.filter((adventure: Adventure) => {
|
|
|
|
// adventure.collections is an array of ids, collectionId is a single id
|
|
|
|
return !(adventure.collections ?? []).includes(collectionId);
|
|
|
|
});
|
|
|
|
} else {
|
2024-10-04 21:56:43 -04:00
|
|
|
adventures = newAdventures;
|
2024-07-15 12:09:20 -04:00
|
|
|
}
|
2025-06-12 15:54:01 -04:00
|
|
|
|
|
|
|
// No need to reassign adventures to newAdventures here, keep the filtered result
|
2024-08-11 13:34:32 -04:00
|
|
|
isLoading = false;
|
2024-07-15 12:09:20 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
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-10-30 15:11:00 -04:00
|
|
|
<h1 class="text-center font-bold text-4xl mb-6">{$t('adventures.my_adventures')}</h1>
|
2024-08-11 13:34:32 -04:00
|
|
|
{#if isLoading}
|
|
|
|
<div class="flex justify-center items-center w-full mt-16">
|
|
|
|
<span class="loading loading-spinner w-24 h-24"></span>
|
|
|
|
</div>
|
|
|
|
{/if}
|
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}
|
2024-12-04 12:38:01 -05:00
|
|
|
<AdventureCard {user} type="link" {adventure} on:link={add} />
|
2024-07-15 12:09:20 -04:00
|
|
|
{/each}
|
2024-08-11 13:34:32 -04:00
|
|
|
{#if adventures.length === 0 && !isLoading}
|
2024-07-15 18:51:05 -04:00
|
|
|
<p class="text-center text-lg">
|
2024-10-30 15:11:00 -04:00
|
|
|
{$t('adventures.no_linkable_adventures')}
|
2024-07-15 18:51:05 -04:00
|
|
|
</p>
|
|
|
|
{/if}
|
2024-07-15 12:09:20 -04:00
|
|
|
</div>
|
2024-10-30 15:11:00 -04:00
|
|
|
<button class="btn btn-primary" on:click={close}>{$t('about.close')}</button>
|
2024-07-15 12:09:20 -04:00
|
|
|
</div>
|
|
|
|
</dialog>
|