1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-20 05:19:38 +02:00
AdventureLog/frontend/src/lib/components/CollectionCard.svelte

171 lines
5.3 KiB
Svelte
Raw Normal View History

2024-07-10 13:36:51 -04:00
<script lang="ts">
import { createEventDispatcher } from 'svelte';
2024-07-10 13:49:40 -04:00
import Launch from '~icons/mdi/launch';
import TrashCanOutline from '~icons/mdi/trash-can-outline';
2024-07-15 18:01:49 -04:00
import FileDocumentEdit from '~icons/mdi/file-document-edit';
2024-08-07 13:01:12 -04:00
import ArchiveArrowDown from '~icons/mdi/archive-arrow-down';
import ArchiveArrowUp from '~icons/mdi/archive-arrow-up';
2024-07-15 18:01:49 -04:00
2024-07-10 13:36:51 -04:00
import { goto } from '$app/navigation';
2024-07-15 09:36:07 -04:00
import type { Collection } from '$lib/types';
2024-07-15 12:09:20 -04:00
import { addToast } from '$lib/toasts';
2024-07-16 09:12:53 -04:00
import Plus from '~icons/mdi/plus';
import DotsHorizontal from '~icons/mdi/dots-horizontal';
import TrashCan from '~icons/mdi/trashcan';
2024-08-07 13:09:20 -04:00
import DeleteWarning from './DeleteWarning.svelte';
2024-09-08 14:29:27 -04:00
import ShareModal from './ShareModal.svelte';
2024-07-16 09:12:53 -04:00
2024-07-10 13:36:51 -04:00
const dispatch = createEventDispatcher();
2024-07-16 09:26:45 -04:00
export let type: String | undefined | null;
2024-09-08 14:29:27 -04:00
let isShareModalOpen: boolean = false;
2024-07-16 09:12:53 -04:00
2024-07-10 13:36:51 -04:00
// export let type: String;
2024-07-15 18:01:49 -04:00
function editAdventure() {
dispatch('edit', collection);
}
2024-08-07 13:01:12 -04:00
async function archiveCollection(is_archived: boolean) {
console.log(JSON.stringify({ is_archived: is_archived }));
let res = await fetch(`/api/collections/${collection.id}/`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ is_archived: is_archived })
});
if (res.ok) {
console.log(`Collection ${is_archived ? 'archived' : 'unarchived'}`);
addToast('info', `Collection ${is_archived ? 'archived' : 'unarchived'} successfully!`);
2024-08-07 13:01:12 -04:00
dispatch('delete', collection.id);
} else {
console.log('Error archiving collection');
2024-08-07 13:01:12 -04:00
}
}
2024-07-15 09:36:07 -04:00
export let collection: Collection;
2024-07-10 13:36:51 -04:00
2024-07-15 12:09:20 -04:00
async function deleteCollection() {
let res = await fetch(`/collections/${collection.id}?/delete`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
if (res.ok) {
console.log('Collection deleted');
2024-08-07 16:34:03 -04:00
addToast('info', 'Collection deleted successfully!');
2024-07-15 12:09:20 -04:00
dispatch('delete', collection.id);
} else {
2024-08-07 16:34:03 -04:00
console.log('Error deleting collection');
2024-07-15 12:09:20 -04:00
}
}
2024-08-07 13:09:20 -04:00
let isWarningModalOpen: boolean = false;
2024-07-10 13:36:51 -04:00
</script>
2024-08-07 13:09:20 -04:00
{#if isWarningModalOpen}
<DeleteWarning
title="Delete Collection"
button_text="Delete"
description="Are you sure you want to delete this collection? This will also delete all of the linked adventures. This action cannot be undone."
2024-08-07 13:09:20 -04:00
is_warning={true}
on:close={() => (isWarningModalOpen = false)}
on:confirm={deleteCollection}
/>
{/if}
2024-09-08 14:29:27 -04:00
{#if isShareModalOpen}
<ShareModal {collection} on:close={() => (isShareModalOpen = false)} />
{/if}
2024-07-10 13:36:51 -04:00
<div
class="card min-w-max lg:w-96 md:w-80 sm:w-60 xs:w-40 bg-neutral text-neutral-content shadow-xl"
2024-07-10 13:36:51 -04:00
>
<div class="card-body">
2024-08-08 22:12:06 -04:00
<div class="flex justify-between">
<button
on:click={() => goto(`/collections/${collection.id}`)}
class="text-2xl font-semibold -mt-2 break-words text-wrap hover:underline"
>
{collection.name}
</button>
</div>
<div class="inline-flex gap-2 mb-2">
2024-09-06 23:39:37 -04:00
<div class="badge badge-secondary">{collection.is_public ? 'Public' : 'Private'}</div>
2024-08-08 22:12:06 -04:00
{#if collection.is_archived}
<div class="badge badge-warning">Archived</div>
{/if}
</div>
2024-07-15 09:36:07 -04:00
<p>{collection.adventures.length} Adventures</p>
2024-07-27 14:26:15 -04:00
{#if collection.start_date && collection.end_date}
<p>
Dates: {new Date(collection.start_date).toLocaleDateString(undefined, { timeZone: 'UTC' })} -
{new Date(collection.end_date).toLocaleDateString(undefined, { timeZone: 'UTC' })}
2024-07-27 14:26:15 -04:00
</p>
<!-- display the duration in days -->
<p>
Duration: {Math.floor(
(new Date(collection.end_date).getTime() - new Date(collection.start_date).getTime()) /
(1000 * 60 * 60 * 24)
) + 1}{' '}
2024-07-27 14:26:15 -04:00
days
</p>{/if}
2024-08-08 22:12:06 -04:00
2024-07-10 13:36:51 -04:00
<div class="card-actions justify-end">
{#if type == 'link'}
<button class="btn btn-primary" on:click={() => dispatch('link', collection.id)}>
<Plus class="w-5 h-5 mr-1" />
</button>
{:else}
<div class="dropdown dropdown-end">
2024-09-07 17:39:47 -04:00
<div tabindex="0" role="button" class="btn btn-neutral-200">
<DotsHorizontal class="w-6 h-6" />
</div>
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
<ul
tabindex="0"
class="dropdown-content menu bg-base-100 rounded-box z-[1] w-52 p-2 shadow"
>
{#if type != 'link'}
<button
class="btn btn-neutral mb-2"
on:click={() => goto(`/collections/${collection.id}`)}
><Launch class="w-5 h-5 mr-1" />Open Details</button
>
{#if !collection.is_archived}
<button class="btn btn-neutral mb-2" on:click={editAdventure}>
<FileDocumentEdit class="w-6 h-6" />Edit Collection
</button>
2024-09-08 14:29:27 -04:00
<button class="btn btn-neutral mb-2" on:click={() => (isShareModalOpen = true)}>
<FileDocumentEdit class="w-6 h-6" />Share
</button>
{/if}
{#if collection.is_archived}
<button class="btn btn-neutral mb-2" on:click={() => archiveCollection(false)}>
<ArchiveArrowUp class="w-6 h-6 mr-1" />Unarchive
</button>
{:else}
<button class="btn btn-neutral mb-2" on:click={() => archiveCollection(true)}>
<ArchiveArrowDown class="w-6 h-6 mr" />Archive
</button>
{/if}
<button
id="delete_adventure"
data-umami-event="Delete Adventure"
class="btn btn-warning"
on:click={() => (isWarningModalOpen = true)}
><TrashCan class="w-6 h-6" />Delete</button
>
{/if}
</ul>
</div>
{/if}
2024-07-10 13:36:51 -04:00
</div>
</div>
</div>