diff --git a/src/routes/planner/+page.svelte b/src/routes/planner/+page.svelte index c0acbd1..8e9092d 100644 --- a/src/routes/planner/+page.svelte +++ b/src/routes/planner/+page.svelte @@ -7,6 +7,7 @@ import { saveAdventure, removeAdventure, + addAdventure, } from "../../services/adventureService.js"; import SucessToast from "$lib/components/SucessToast.svelte"; import mapDrawing from "$lib/assets/adventure_map.svg"; @@ -76,40 +77,15 @@ } } - const createNewAdventure = (event: { detail: Adventure }) => { + const createNewAdventure = async (event: { detail: Adventure }) => { isShowingMoreFields = false; - let detailAdventure = event.detail; - console.log("Event" + event.detail.name); - - fetch("/api/planner", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - detailAdventure, - }), - }) - .then((response) => { - if (!response.ok) { - return response.json().then((data) => { - throw new Error( - data.error || `Failed to add adventure - ${data?.message}` - ); - }); - } - return response.json(); - }) - .then((data) => { - // add to local array for instant view update - plans = [...plans, data.adventure]; - // showToast("Adventure added successfully!"); - // visitCount.update((n) => n + 1); - }) - .catch((error) => { - console.error("Error:", error); - // showToast(error.message); - }); + let newArray = await addAdventure(event.detail, plans); + if (newArray.length > 0) { + plans = newArray; + showToast("Adventure added successfully!"); + } else { + showToast("Failed to add adventure"); + } }; diff --git a/src/services/adventureService.ts b/src/services/adventureService.ts index cf851b6..b39492d 100644 --- a/src/services/adventureService.ts +++ b/src/services/adventureService.ts @@ -1,6 +1,8 @@ import type { Adventure } from "$lib/utils/types"; -// json that maps the type to api routes +/** + * Object containing the API routes for the different types of adventures. + */ const apiRoutes: { [key: string]: string } = { planner: "/api/planner", mylog: "/api/visits", @@ -50,6 +52,12 @@ export async function saveAdventure( return adventureArray; } +/** + * Removes an adventure from the adventure array and sends a delete request to the server. + * @param adventure - The adventure to be removed. + * @param adventureArray - The array of adventures. + * @returns A promise that resolves to the updated adventure array. + */ export async function removeAdventure( adventure: Adventure, adventureArray: Adventure[] @@ -81,28 +89,33 @@ export async function removeAdventure( } /** - * function removeAdventure(event: { detail: number }) { - console.log("Event ID " + event.detail); - // send delete request to server at /api/visits - fetch("/api/visits", { - method: "DELETE", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ id: event.detail }), + * Adds an adventure to the adventure array and sends a POST request to the specified URL. + * @param {Adventure} adventure - The adventure to be added. + * @param {Adventure[]} adventureArray - The array of adventures. + * @returns {Promise} - A promise that resolves to the updated adventure array. + */ +export async function addAdventure( + adventure: Adventure, + adventureArray: Adventure[] +): Promise { + let url = apiRoutes[adventure.type]; + let detailAdventure = adventure; + // post request to /api/visits with adventure data + const response = await fetch(url, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ detailAdventure }), + }) + .then((response) => response.json()) + .then((data) => { + adventureArray.push(data.adventure); }) - .then((response) => response.json()) - .then((data) => { - console.log("Success:", data); - // remove adventure from array where id matches - plans = plans.filter((adventure) => adventure.id !== event.detail); - // showToast("Adventure removed successfully!"); - // visitCount.update((n) => n - 1); - }) - .catch((error) => { - console.error("Error:", error); - }); - } - * - * - */ + .catch((error) => { + console.error("Error:", error); + return []; + }); + + return adventureArray; +}