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

225 lines
7 KiB
Svelte
Raw Normal View History

2024-07-08 11:44:39 -04:00
<script lang="ts">
import { createEventDispatcher } from 'svelte';
import { goto } from '$app/navigation';
2024-09-09 13:31:00 -04:00
import type { Adventure, Collection, User } from '$lib/types';
2024-07-08 11:44:39 -04:00
const dispatch = createEventDispatcher();
import Launch from '~icons/mdi/launch';
import FileDocumentEdit from '~icons/mdi/file-document-edit';
import TrashCan from '~icons/mdi/trash-can-outline';
import Calendar from '~icons/mdi/calendar';
import MapMarker from '~icons/mdi/map-marker';
import { addToast } from '$lib/toasts';
2024-07-15 12:09:20 -04:00
import Link from '~icons/mdi/link-variant';
2024-07-16 09:12:53 -04:00
import LinkVariantRemove from '~icons/mdi/link-variant-remove';
import Plus from '~icons/mdi/plus';
import CollectionLink from './CollectionLink.svelte';
2024-07-16 14:50:30 -04:00
import DotsHorizontal from '~icons/mdi/dots-horizontal';
import DeleteWarning from './DeleteWarning.svelte';
import CardCarousel from './CardCarousel.svelte';
2024-10-28 13:56:57 -04:00
import { t } from 'svelte-i18n';
2024-07-08 11:44:39 -04:00
export let type: string;
2024-07-18 18:37:46 -04:00
export let user: User | null;
2024-09-09 13:31:00 -04:00
export let collection: Collection | null = null;
2024-07-18 18:37:46 -04:00
2024-07-16 09:12:53 -04:00
let isCollectionModalOpen: boolean = false;
let isWarningModalOpen: boolean = false;
2024-07-16 09:12:53 -04:00
2024-07-08 11:44:39 -04:00
export let adventure: Adventure;
2024-07-18 22:12:47 -04:00
let activityTypes: string[] = [];
// makes it reactivty to changes so it updates automatically
$: {
if (adventure.activity_types) {
activityTypes = adventure.activity_types;
if (activityTypes.length > 3) {
activityTypes = activityTypes.slice(0, 3);
let remaining = adventure.activity_types.length - 3;
activityTypes.push('+' + remaining);
}
}
}
2024-07-08 11:44:39 -04:00
async function deleteAdventure() {
let res = await fetch(`/adventures/${adventure.id}?/delete`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
if (res.ok) {
2024-10-28 19:59:44 -04:00
addToast('info', $t('adventures.adventure_delete_success'));
2024-07-08 11:44:39 -04:00
dispatch('delete', adventure.id);
} else {
console.log('Error deleting adventure');
}
}
2024-07-16 09:12:53 -04:00
async function removeFromCollection() {
let res = await fetch(`/api/adventures/${adventure.id}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ collection: null })
});
if (res.ok) {
2024-10-28 13:56:57 -04:00
addToast('info', `${$t('adventures.collection_remove_success')}`);
2024-07-16 09:12:53 -04:00
dispatch('delete', adventure.id);
} else {
2024-10-28 13:56:57 -04:00
addToast('error', `${$t('adventures.collection_remove_error')}`);
2024-07-16 09:12:53 -04:00
}
}
async function linkCollection(event: CustomEvent<number>) {
let collectionId = event.detail;
let res = await fetch(`/api/adventures/${adventure.id}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ collection: collectionId })
});
if (res.ok) {
console.log('Adventure linked to collection');
2024-10-28 13:56:57 -04:00
addToast('info', `${$t('adventures.collection_link_success')}`);
2024-07-16 09:12:53 -04:00
isCollectionModalOpen = false;
dispatch('delete', adventure.id);
} else {
2024-10-28 13:56:57 -04:00
addToast('error', `${$t('adventures.collection_link_error')}`);
2024-07-16 09:12:53 -04:00
}
}
2024-07-08 11:44:39 -04:00
function editAdventure() {
dispatch('edit', adventure);
}
2024-07-15 12:09:20 -04:00
function link() {
dispatch('link', adventure);
}
2024-07-08 11:44:39 -04:00
</script>
2024-07-16 09:12:53 -04:00
{#if isCollectionModalOpen}
<CollectionLink on:link={linkCollection} on:close={() => (isCollectionModalOpen = false)} />
{/if}
{#if isWarningModalOpen}
<DeleteWarning
2024-10-28 19:59:44 -04:00
title={$t('adventures.delete_adventure')}
button_text="Delete"
2024-10-28 13:56:57 -04:00
description={$t('adventures.adventure_delete_confirm')}
is_warning={false}
on:close={() => (isWarningModalOpen = false)}
on:confirm={deleteAdventure}
/>
{/if}
2024-07-08 11:44:39 -04:00
<div
class="card w-full max-w-xs sm:max-w-sm md:max-w-md lg:max-w-md xl:max-w-md bg-neutral text-neutral-content shadow-xl"
2024-07-08 11:44:39 -04:00
>
<CardCarousel adventures={[adventure]} />
2024-07-08 11:44:39 -04:00
<div class="card-body">
2024-07-16 15:44:37 -04:00
<div class="flex justify-between">
2024-08-08 13:52:55 -04:00
<button
on:click={() => goto(`/adventures/${adventure.id}`)}
2024-08-13 11:09:49 -04:00
class="text-2xl font-semibold -mt-2 break-words text-wrap hover:underline text-left"
2024-08-08 13:52:55 -04:00
>
2024-07-16 15:44:37 -04:00
{adventure.name}
2024-08-08 13:52:55 -04:00
</button>
2024-07-18 22:12:47 -04:00
</div>
<div>
<div class="badge badge-primary">
{`${adventure.category.display_name} ${adventure.category.icon}`}
</div>
2024-10-28 19:59:44 -04:00
<div class="badge badge-success">
2024-10-31 09:51:04 -04:00
{adventure.is_visited ? $t('adventures.visited') : $t('adventures.planned')}
2024-10-28 19:59:44 -04:00
</div>
<div class="badge badge-secondary">
{adventure.is_public ? $t('adventures.public') : $t('adventures.private')}
</div>
2024-07-16 09:26:45 -04:00
</div>
2024-07-08 11:44:39 -04:00
{#if adventure.location && adventure.location !== ''}
<div class="inline-flex items-center">
<MapMarker class="w-5 h-5 mr-1" />
<p class="ml-.5">{adventure.location}</p>
</div>
{/if}
2024-09-23 18:46:04 -04:00
{#if adventure.visits.length > 0}
<!-- visited badge -->
<div class="flex items-center">
2024-07-08 11:44:39 -04:00
<Calendar class="w-5 h-5 mr-1" />
2024-09-23 18:46:04 -04:00
<p class="ml-.5">
{adventure.visits.length}
2024-10-28 19:59:44 -04:00
{adventure.visits.length > 1 ? $t('adventures.visits') : $t('adventures.visit')}
2024-08-18 12:30:12 -04:00
</p>
2024-07-08 11:44:39 -04:00
</div>
{/if}
{#if adventure.activity_types && adventure.activity_types.length > 0}
<ul class="flex flex-wrap">
2024-07-18 22:12:47 -04:00
{#each activityTypes as activity}
2024-07-08 11:44:39 -04:00
<div class="badge badge-primary mr-1 text-md font-semibold pb-2 pt-1 mb-1">
{activity}
</div>
{/each}
</ul>
{/if}
<div class="card-actions justify-end mt-2">
2024-07-16 14:50:30 -04:00
<!-- action options dropdown -->
2024-07-18 14:55:23 -04:00
{#if type != 'link'}
{#if adventure.user_id == user?.uuid || (collection && user && collection.shared_with.includes(user.uuid))}
2024-07-18 18:37:46 -04:00
<div class="dropdown dropdown-end">
2024-09-06 23:47:40 -04:00
<div tabindex="0" role="button" class="btn btn-neutral-200">
2024-07-18 18:37:46 -04:00
<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"
2024-07-16 14:50:30 -04:00
>
2024-07-18 18:37:46 -04:00
<button
class="btn btn-neutral mb-2"
on:click={() => goto(`/adventures/${adventure.id}`)}
2024-10-28 13:56:57 -04:00
><Launch class="w-6 h-6" />{$t('adventures.open_details')}</button
2024-07-18 14:55:23 -04:00
>
2024-07-18 18:37:46 -04:00
<button class="btn btn-neutral mb-2" on:click={editAdventure}>
2024-10-28 13:56:57 -04:00
<FileDocumentEdit class="w-6 h-6" />
{$t('adventures.edit_adventure')}
2024-07-18 18:37:46 -04:00
</button>
2024-10-28 13:56:57 -04:00
2024-09-30 18:03:10 -04:00
<!-- remove from collection -->
{#if adventure.collection && user?.uuid == adventure.user_id}
2024-07-18 18:37:46 -04:00
<button class="btn btn-neutral mb-2" on:click={removeFromCollection}
2024-10-28 13:56:57 -04:00
><LinkVariantRemove class="w-6 h-6" />{$t(
'adventures.remove_from_collection'
)}</button
2024-07-18 18:37:46 -04:00
>
{/if}
{#if !adventure.collection}
<button class="btn btn-neutral mb-2" on:click={() => (isCollectionModalOpen = true)}
2024-10-28 13:56:57 -04:00
><Plus class="w-6 h-6" />{$t('adventures.add_to_collection')}</button
2024-07-18 18:37:46 -04:00
>
{/if}
<button
id="delete_adventure"
data-umami-event="Delete Adventure"
class="btn btn-warning"
on:click={() => (isWarningModalOpen = true)}
2024-10-28 13:56:57 -04:00
><TrashCan class="w-6 h-6" />{$t('adventures.delete')}</button
2024-07-18 14:55:23 -04:00
>
2024-07-18 18:37:46 -04:00
</ul>
</div>
{:else}
2024-09-09 14:29:50 -04:00
<button
class="btn btn-neutral-200 mb-2"
on:click={() => goto(`/adventures/${adventure.id}`)}><Launch class="w-6 h-6" /></button
2024-07-18 18:37:46 -04:00
>
{/if}
2024-07-18 14:55:23 -04:00
{/if}
{#if type == 'link'}
<button class="btn btn-primary" on:click={link}><Link class="w-6 h-6" /></button>
{/if}
2024-07-08 11:44:39 -04:00
</div>
</div>
</div>