1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-21 22:09:36 +02:00

Refactor AdventureCard component and add CardCarousel

- Refactor AdventureCard component to remove unused variable and add meta tags for shared collections page
- Add CardCarousel component to display a carousel of adventure images in AdventureCard
This commit is contained in:
Sean Morley 2024-10-13 23:23:32 -04:00
parent 5b7aaa6aad
commit 3ee9625fe8
6 changed files with 94 additions and 57 deletions

View file

@ -20,6 +20,7 @@
import DeleteWarning from './DeleteWarning.svelte';
import ImageDisplayModal from './ImageDisplayModal.svelte';
import { isAdventureVisited, typeToString } from '$lib';
import CardCarousel from './CardCarousel.svelte';
export let type: string;
export let user: User | null;
@ -28,7 +29,6 @@
let isCollectionModalOpen: boolean = false;
let isWarningModalOpen: boolean = false;
let image_url: string | null = null;
export let adventure: Adventure;
let activityTypes: string[] = [];
@ -120,12 +120,6 @@
dispatch('edit', adventure);
}
let currentSlide = 0;
function goToSlide(index: number) {
currentSlide = index;
}
function link() {
dispatch('link', adventure);
}
@ -146,48 +140,10 @@
/>
{/if}
{#if image_url}
<ImageDisplayModal image={image_url} on:close={() => (image_url = null)} {adventure} />
{/if}
<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"
>
<figure>
{#if adventure.images && adventure.images.length > 0}
<div class="carousel w-full">
{#each adventure.images as image, i}
<div
class="carousel-item w-full"
style="display: {i === currentSlide ? 'block' : 'none'}"
>
<!-- svelte-ignore a11y-invalid-attribute -->
<!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-no-static-element-interactions -->
<!-- svelte-ignore a11y-missing-attribute -->
<a on:click={() => (image_url = image.image)}
><img src={image.image} class="w-full h-48 object-cover" alt={adventure.name} /></a
>
<div class="flex justify-center w-full py-2 gap-2">
{#each adventure.images as _, i}
<button
on:click={() => goToSlide(i)}
class="btn btn-xs {i === currentSlide ? 'btn-active' : ''}">{i + 1}</button
>
{/each}
</div>
</div>
{/each}
</div>
{:else}
<!-- svelte-ignore a11y-img-redundant-alt -->
<img
src={'https://placehold.co/300?text=No%20Image%20Found&font=roboto'}
alt="No image available"
class="w-full h-48 object-cover"
/>
{/if}
</figure>
<CardCarousel adventures={[adventure]} />
<div class="card-body">
<div class="flex justify-between">

View file

@ -0,0 +1,83 @@
<script lang="ts">
import type { Adventure } from '$lib/types';
import ImageDisplayModal from './ImageDisplayModal.svelte';
export let adventures: Adventure[] = [];
let adventure_images: string[] = [];
let currentSlide = 0; // Declare as a regular variable
let adventure: Adventure | null = null;
let image_url: string | null = null;
adventures.forEach((adventure) => {
adventure_images = [...adventure_images, ...adventure.images.map((image) => image.image)];
});
// Reactive statement to log when currentSlide changes
$: console.log('Current slide:', currentSlide);
function changeSlide(direction: string) {
if (direction === 'next' && currentSlide < adventure_images.length - 1) {
currentSlide = currentSlide + 1; // Use direct assignment
} else if (direction === 'prev' && currentSlide > 0) {
currentSlide = currentSlide - 1; // Use direct assignment
}
}
</script>
{#if image_url}
<ImageDisplayModal image={image_url} on:close={() => (image_url = null)} />
{/if}
<figure>
{#if adventure_images && adventure_images.length > 0}
<div class="carousel w-full relative">
<!-- svelte-ignore a11y-click-events-have-key-events -->
<div class="carousel-item w-full block">
<!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-no-static-element-interactions -->
<!-- svelte-ignore a11y-missing-attribute -->
<a
on:click|stopPropagation={() => (image_url = adventure_images[currentSlide])}
class="cursor-pointer"
>
<img
src={adventure_images[currentSlide]}
class="w-full h-48 object-cover"
alt={adventure_images[currentSlide]}
/>
</a>
{#if adventure_images.length > 1}
<div class="absolute inset-0 flex items-center justify-between pointer-events-none">
{#if currentSlide > 0}
<button
on:click|stopPropagation={() => changeSlide('prev')}
class="btn btn-circle btn-sm ml-2 pointer-events-auto"></button
>
{:else}
<div class="w-12"></div>
{/if}
{#if currentSlide < adventure_images.length - 1}
<button
on:click|stopPropagation={() => changeSlide('next')}
class="btn btn-circle mr-2 btn-sm pointer-events-auto"></button
>
{:else}
<div class="w-12"></div>
{/if}
</div>
{/if}
</div>
</div>
{:else}
<!-- svelte-ignore a11y-img-redundant-alt -->
<img
src={'https://placehold.co/300?text=No%20Image%20Found&font=roboto'}
alt="No image available"
class="w-full h-48 object-cover"
/>
{/if}
</figure>

View file

@ -9,7 +9,7 @@
import ArchiveArrowUp from '~icons/mdi/archive-arrow-up';
import { goto } from '$app/navigation';
import type { Collection } from '$lib/types';
import type { Adventure, Collection } from '$lib/types';
import { addToast } from '$lib/toasts';
import Plus from '~icons/mdi/plus';
@ -17,14 +17,15 @@
import TrashCan from '~icons/mdi/trashcan';
import DeleteWarning from './DeleteWarning.svelte';
import ShareModal from './ShareModal.svelte';
import ImageDisplayModal from './ImageDisplayModal.svelte';
import CardCarousel from './CardCarousel.svelte';
const dispatch = createEventDispatcher();
export let type: String | undefined | null;
export let adventures: Adventure[] = [];
let isShareModalOpen: boolean = false;
// export let type: String;
function editAdventure() {
dispatch('edit', collection);
}
@ -86,6 +87,7 @@
<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"
>
<CardCarousel {adventures} />
<div class="card-body">
<div class="flex justify-between">
<button

View file

@ -6,7 +6,6 @@
import type { Adventure } from '$lib/types';
export let image: string;
export let adventure: Adventure;
onMount(() => {
modal = document.getElementById('my_modal_1') as HTMLDialogElement;
@ -43,7 +42,7 @@
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
<div class="modal-box w-11/12 max-w-5xl" role="dialog" on:keydown={handleKeydown} tabindex="0">
<div class="modal-header flex justify-between items-center mb-4">
<h3 class="font-bold text-2xl">{adventure.name}</h3>
<h3 class="font-bold text-2xl">Image Preview</h3>
<button class="btn btn-circle btn-neutral" on:click={close}>
<svg
xmlns="http://www.w3.org/2000/svg"
@ -67,7 +66,7 @@
>
<img
src={image}
alt={adventure.name}
alt="My Adventure"
style="max-width: 100%; max-height: 75vh; object-fit: contain;"
/>
</div>

View file

@ -57,11 +57,7 @@
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 overflow-hidden"
>
<div class="card-body">
{#if region.name_en && region.name !== region.name_en}
<h2 class="card-title overflow-ellipsis">{region.name} ({region.name_en})</h2>
{:else}
<h2 class="card-title overflow-ellipsis">{region.name}</h2>
{/if}
<p>{region.id}</p>
<div class="card-actions justify-end">
<!-- <button class="btn btn-info" on:click={moreInfo}>More Info</button> -->

View file

@ -180,6 +180,7 @@
{collection}
on:delete={deleteCollection}
on:edit={editCollection}
adventures={collection.adventures}
/>
{/each}
</div>