1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-22 06:19:38 +02:00

feat: Add type change functionality to AdventureCard component

This commit is contained in:
Sean Morley 2024-07-22 10:45:25 -04:00
parent e093be31af
commit fe05e5c6cf
4 changed files with 74 additions and 12 deletions

View file

@ -83,6 +83,7 @@
}); });
if (res.ok) { if (res.ok) {
console.log('Adventure type changed'); console.log('Adventure type changed');
dispatch('typeChange', adventure.id);
addToast('info', 'Adventure type changed successfully!'); addToast('info', 'Adventure type changed successfully!');
adventure.type = newType; adventure.type = newType;
} else { } else {

View file

@ -9,13 +9,18 @@
import AdventureCard from '$lib/components/AdventureCard.svelte'; import AdventureCard from '$lib/components/AdventureCard.svelte';
import AdventureLink from '$lib/components/AdventureLink.svelte'; import AdventureLink from '$lib/components/AdventureLink.svelte';
import EditAdventure from '$lib/components/EditAdventure.svelte'; import EditAdventure from '$lib/components/EditAdventure.svelte';
import NotFound from '$lib/components/NotFound.svelte';
export let data: PageData; export let data: PageData;
let collection: Collection; let collection: Collection;
let adventures: Adventure[] = []; let adventures: Adventure[] = [];
let numVisited: number = adventures.filter((a) => a.type == 'visited').length; let numVisited: number = 0;
$: {
numVisited = adventures.filter((a) => a.type === 'visited').length;
}
let notFound: boolean = false; let notFound: boolean = false;
let isShowingCreateModal: boolean = false; let isShowingCreateModal: boolean = false;
@ -56,6 +61,19 @@
} }
} }
function changeType(event: CustomEvent<number>) {
adventures = adventures.map((adventure) => {
if (adventure.id == event.detail) {
if (adventure.type == 'visited') {
adventure.type = 'planned';
} else {
adventure.type = 'visited';
}
}
return adventure;
});
}
let adventureToEdit: Adventure; let adventureToEdit: Adventure;
let isEditModalOpen: boolean = false; let isEditModalOpen: boolean = false;
@ -157,7 +175,7 @@
<div class="flex items-center justify-center mb-4"> <div class="flex items-center justify-center mb-4">
<div class="stats shadow bg-base-300"> <div class="stats shadow bg-base-300">
<div class="stat"> <div class="stat">
<div class="stat-title">Region Stats</div> <div class="stat-title">Collection Stats</div>
<div class="stat-value">{numVisited}/{adventures.length} Visited</div> <div class="stat-value">{numVisited}/{adventures.length} Visited</div>
{#if numVisited === adventures.length} {#if numVisited === adventures.length}
<div class="stat-desc">You've completed this collection! 🎉!</div> <div class="stat-desc">You've completed this collection! 🎉!</div>
@ -169,6 +187,9 @@
</div> </div>
{/if} {/if}
<h1 class="text-center font-semibold text-2xl mt-4 mb-2">Linked Adventures</h1> <h1 class="text-center font-semibold text-2xl mt-4 mb-2">Linked Adventures</h1>
{#if adventures.length == 0}
<NotFound error={undefined} />
{/if}
<div class="flex flex-wrap gap-4 mr-4 justify-center content-center"> <div class="flex flex-wrap gap-4 mr-4 justify-center content-center">
{#each adventures as adventure} {#each adventures as adventure}
<AdventureCard <AdventureCard
@ -177,6 +198,7 @@
on:delete={deleteAdventure} on:delete={deleteAdventure}
type={adventure.type} type={adventure.type}
{adventure} {adventure}
on:typeChange={changeType}
/> />
{/each} {/each}
</div> </div>

View file

@ -25,7 +25,8 @@ export const load = (async (event) => {
.map((adventure) => { .map((adventure) => {
return { return {
lngLat: [adventure.longitude, adventure.latitude] as [number, number], lngLat: [adventure.longitude, adventure.latitude] as [number, number],
name: adventure.name name: adventure.name,
type: adventure.type
}; };
}); });
return { return {

View file

@ -1,9 +1,11 @@
<script> <script>
// @ts-nocheck // @ts-nocheck
import { DefaultMarker, MapEvents, MapLibre, Popup } from 'svelte-maplibre'; import { DefaultMarker, MapEvents, MapLibre, Popup, Marker } from 'svelte-maplibre';
export let data; export let data;
let clickedName = '';
let markers = data.props.markers; let markers = data.props.markers;
console.log(markers); console.log(markers);
</script> </script>
@ -13,14 +15,50 @@
class="relative aspect-[9/16] max-h-[70vh] w-full sm:aspect-video sm:max-h-full" class="relative aspect-[9/16] max-h-[70vh] w-full sm:aspect-video sm:max-h-full"
standardControls standardControls
> >
{#each data.props.markers as { lngLat, name }} {#each data.props.markers as { lngLat, name, type }}
<!-- Unlike the custom marker example, default markers do not have mouse events, {#if type == 'visited'}
and popups only support the default openOn="click" behavior --> <Marker
<DefaultMarker {lngLat}> {lngLat}
<Popup offset={[0, -10]}> on:click={() => (clickedName = name)}
class="grid h-8 w-8 place-items-center rounded-full border border-gray-200 bg-red-300 text-black shadow-2xl focus:outline-2 focus:outline-black"
>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<circle cx="12" cy="12" r="10" stroke="red" stroke-width="2" fill="red" />
</svg>
<Popup openOn="click" offset={[0, -10]}>
<div class="text-lg font-bold">{name}</div> <div class="text-lg font-bold">{name}</div>
<p class="font-semibold text-md">Visited</p>
</Popup> </Popup>
</DefaultMarker> </Marker>
{/if}
{#if type == 'planned'}
<Marker
{lngLat}
on:click={() => (clickedName = name)}
class="grid h-8 w-8 place-items-center rounded-full border border-gray-200 bg-blue-300 text-black shadow-2xl focus:outline-2 focus:outline-black"
>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<circle cx="12" cy="12" r="10" stroke="blue" stroke-width="2" fill="blue" />
</svg>
<Popup openOn="click" offset={[0, -10]}>
<div class="text-lg font-bold">{name}</div>
<p class="font-semibold text-md">Planned</p>
</Popup>
</Marker>
{/if}
{/each} {/each}
</MapLibre> </MapLibre>