From ca7592989aea27dfb89b7a757a84e03a4feb4719 Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Sun, 5 May 2024 19:06:23 +0000 Subject: [PATCH] feat: Add markVisited function to AdventureCard.svelte and +page.svelte The code changes include adding a new function called markVisited to the AdventureCard.svelte and +page.svelte files. This function is responsible for marking an adventure as visited and dispatching an event to update the adventure's status. This enhancement allows users to mark adventures as visited in the planner page and triggers the corresponding API request to update the adventure's status in the database. --- src/lib/components/AdventureCard.svelte | 8 ++++ src/routes/api/visits/+server.ts | 9 +++- src/routes/planner/+page.svelte | 63 +++++++++++++++++++++++++ src/services/adventureService.ts | 31 ++++++++++++ 4 files changed, 110 insertions(+), 1 deletion(-) diff --git a/src/lib/components/AdventureCard.svelte b/src/lib/components/AdventureCard.svelte index e5bed98..0cdd8a1 100644 --- a/src/lib/components/AdventureCard.svelte +++ b/src/lib/components/AdventureCard.svelte @@ -31,6 +31,10 @@ console.log(adventure.id); goto(`/adventure/${adventure.id}`); } + function markVisited() { + console.log(adventure.id); + dispatch("markVisited", adventure); + }
{/if} {#if type == "planner"} + { }); } - const { name, location, date, description, activityTypes, id, rating } = + const { name, location, date, description, activityTypes, id, rating, type } = body.detailAdventure; if (!name) { @@ -189,11 +189,18 @@ export async function PUT(event: RequestEvent): Promise { }); } + if (type == "featured") { + return error(400, { + message: "Featured adventures cannot be created at the moment", + }); + } + // update the adventure in the user's visited list await db .update(adventureTable) .set({ name: name, + type: type, location: location, date: date, description: description, diff --git a/src/routes/planner/+page.svelte b/src/routes/planner/+page.svelte index 8e9092d..409f3d1 100644 --- a/src/routes/planner/+page.svelte +++ b/src/routes/planner/+page.svelte @@ -8,6 +8,7 @@ saveAdventure, removeAdventure, addAdventure, + changeType, } from "../../services/adventureService.js"; import SucessToast from "$lib/components/SucessToast.svelte"; import mapDrawing from "$lib/assets/adventure_map.svg"; @@ -87,6 +88,18 @@ showToast("Failed to add adventure"); } }; + + async function markVisited(event: { detail: Adventure }) { + let initialLength: number = plans.length; + let newArray = await changeType(event.detail, "mylog", plans); + if (newArray.length + 1 == initialLength) { + plans = newArray; + showToast("Adventure moved to visit log!"); + } else { + showToast("Failed to moves adventure"); + } + adventureToEdit = undefined; + } {#if isShowingToast} @@ -131,6 +144,7 @@ type="planner" on:edit={editPlan} on:remove={remove} + on:markVisited={markVisited} /> {/each}
@@ -153,3 +167,52 @@ type="planner" /> {/if} + + + + + + + My Plans | AdventureLog + + diff --git a/src/services/adventureService.ts b/src/services/adventureService.ts index 8559702..c8405dc 100644 --- a/src/services/adventureService.ts +++ b/src/services/adventureService.ts @@ -124,6 +124,37 @@ export async function addAdventure( return adventureArray; } +export async function changeType( + newAdventure: Adventure, + newType: string, + adventureArray: Adventure[] +) { + newAdventure.type = newType; + let detailAdventure = newAdventure; + + const response = await fetch("/api/visits", { + method: "PUT", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ detailAdventure }), + }); + + if (response.ok) { + // remove adventure from array where id matches + adventureArray = adventureArray.filter( + (existingAdventure) => existingAdventure.id !== newAdventure.id + ); + // showToast("Adventure removed successfully!"); + } else { + console.error("Error:", response.statusText); + adventureArray = []; + } + + console.log(adventureArray); + + return adventureArray; +} /** * Increments the visit count by the specified amount. * @param {number} amount - The amount to increment the visit count by.