From 798ce1e3e51f1c1a5cbbb94f299403bc5b7ec7fe Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Sat, 4 May 2024 15:18:20 +0000 Subject: [PATCH] feat: Add saveAdventure function to adventureService The code changes include adding a new function called saveAdventure to the adventureService module. This function is responsible for sending a PUT request to the corresponding API endpoint to save an adventure. If the request is successful, the local adventure array is updated with the new data. If the request fails, an empty array is returned to allow for handling errors. --- src/routes/planner/+page.svelte | 58 +++++++++++----------- src/services/adventureService.ts | 84 ++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 30 deletions(-) diff --git a/src/routes/planner/+page.svelte b/src/routes/planner/+page.svelte index d7b81ba..c1de223 100644 --- a/src/routes/planner/+page.svelte +++ b/src/routes/planner/+page.svelte @@ -4,6 +4,8 @@ import AdventureCard from "$lib/components/AdventureCard.svelte"; import EditModal from "$lib/components/EditModal.svelte"; import MoreFieldsInput from "$lib/components/CreateNewAdventure.svelte"; + import { saveAdventure } from "../../services/adventureService.js"; + import SucessToast from "$lib/components/SucessToast.svelte"; export let data; let plans: Adventure[] = []; let isLoading = true; @@ -16,10 +18,23 @@ let isShowingMoreFields: boolean = false; + let isShowingToast: boolean = false; + let toastAction: string = ""; + let adventureToEdit: Adventure | undefined; console.log(data); + function showToast(action: string) { + toastAction = action; + isShowingToast = true; + + setTimeout(() => { + isShowingToast = false; + toastAction = ""; + }, 3000); + } + function editPlan(event: { detail: number }) { const adventure = plans.find((adventure) => adventure.id === event.detail); if (adventure) { @@ -32,36 +47,15 @@ isShowingMoreFields = false; } - function savePlan(event: { detail: Adventure }) { - console.log("Event", event.detail); - let detailAdventure = event.detail; - - // put request to /api/visits with id and adventure data - fetch("/api/planner", { - method: "PUT", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - detailAdventure, - }), - }) - .then((response) => response.json()) - .then((data) => { - console.log("Success:", data); - // update local array with new data - const index = plans.findIndex( - (adventure) => adventure.id === detailAdventure.id - ); - if (index !== -1) { - plans[index] = detailAdventure; - } - adventureToEdit = undefined; - // showToast("Adventure edited successfully!"); - }) - .catch((error) => { - console.error("Error:", error); - }); + async function savePlan(event: { detail: Adventure }) { + let newArray = await saveAdventure(event.detail, plans); + if (newArray.length > 0) { + plans = newArray; + showToast("Adventure updated successfully!"); + } else { + showToast("Failed to update adventure"); + } + adventureToEdit = undefined; } function removeAdventure(event: { detail: number }) { @@ -124,6 +118,10 @@ }; +{#if isShowingToast} + +{/if} +