From 25adf07874a070e4b5d2f9fad1a520018924dee1 Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Fri, 3 May 2024 21:29:35 +0000 Subject: [PATCH] Refactor adventure page layout to display activity types and update server files --- src/lib/components/EditModal.svelte | 2 + src/routes/api/visits/+server.ts | 38 +++++++++++----- src/routes/log/+page.svelte | 68 +++++++++-------------------- 3 files changed, 51 insertions(+), 57 deletions(-) diff --git a/src/lib/components/EditModal.svelte b/src/lib/components/EditModal.svelte index 11e7814..a1f9a3e 100644 --- a/src/lib/components/EditModal.svelte +++ b/src/lib/components/EditModal.svelte @@ -6,6 +6,8 @@ import { onMount } from "svelte"; let modal: HTMLDialogElement; + console.log(adventureToEdit.id); + let originalName = adventureToEdit.name; onMount(() => { diff --git a/src/routes/api/visits/+server.ts b/src/routes/api/visits/+server.ts index 37cf393..ce2c654 100644 --- a/src/routes/api/visits/+server.ts +++ b/src/routes/api/visits/+server.ts @@ -86,7 +86,6 @@ export async function DELETE(event: RequestEvent): Promise { }); } -// add the adventure to the user's visited list export async function POST(event: RequestEvent): Promise { if (!event.locals.user) { return new Response(JSON.stringify({ error: "No user found" }), { @@ -97,9 +96,15 @@ export async function POST(event: RequestEvent): Promise { }); } - const { newAdventure } = await event.request.json(); - console.log(newAdventure); - const { name, location, date, description, activityTypes } = newAdventure; + const body = await event.request.json(); + if (!body.detailAdventure) { + return error(400, { + message: "No adventure data provided", + }); + } + + const { name, location, date, description, activityTypes } = + body.detailAdventure; if (!name) { return error(400, { @@ -127,10 +132,12 @@ export async function POST(event: RequestEvent): Promise { let insertedId = res[0].insertedId; console.log(insertedId); + body.detailAdventure.id = insertedId; + // return a response with the adventure object values return new Response( JSON.stringify({ - adventure: { name, location, date }, + adventure: body.detailAdventure, message: { message: "Adventure added" }, id: insertedId, }), @@ -154,10 +161,21 @@ export async function PUT(event: RequestEvent): Promise { }); } - // get properties from the body - const { newAdventure } = await event.request.json(); - console.log(newAdventure); - const { name, location, date, id, description, activityTypes } = newAdventure; + const body = await event.request.json(); + if (!body.detailAdventure) { + return error(400, { + message: "No adventure data provided", + }); + } + + const { name, location, date, description, activityTypes, id } = + body.detailAdventure; + + if (!name) { + return error(400, { + message: "Name field is required!", + }); + } // update the adventure in the user's visited list await db @@ -179,7 +197,7 @@ export async function PUT(event: RequestEvent): Promise { return new Response( JSON.stringify({ - adventure: { id, name, location, date, description }, + adventure: body.detailAdventure, message: { message: "Adventure updated" }, }), { diff --git a/src/routes/log/+page.svelte b/src/routes/log/+page.svelte index 1d21740..59c29f2 100644 --- a/src/routes/log/+page.svelte +++ b/src/routes/log/+page.svelte @@ -15,9 +15,6 @@ import { visitCount } from "$lib/utils/stores/visitCountStore"; import MoreFieldsInput from "$lib/components/CreateNewAdventure.svelte"; - let newName = ""; - let newLocation = ""; - let isShowingMoreFields = false; let adventureToEdit: Adventure | undefined; @@ -60,15 +57,9 @@ } const createNewAdventure = (event: { detail: Adventure }) => { - let newAdventure: Adventure = { - type: "mylog", - name: event.detail.name, - location: event.detail.location, - date: event.detail.date, - description: event.detail.description, - id: -1, - activityTypes: event.detail.activityTypes, - }; + isShowingMoreFields = false; + let detailAdventure = event.detail; + console.log("Event" + event.detail.name); fetch("/api/visits", { method: "POST", @@ -76,7 +67,7 @@ "Content-Type": "application/json", }, body: JSON.stringify({ - newAdventure, + detailAdventure, }), }) .then((response) => { @@ -90,21 +81,8 @@ return response.json(); }) .then((data) => { - let newId = data.id; // add to local array for instant view update - adventures = [ - ...adventures, - { - id: newId, - type: "mylog", - name: event.detail.name, - location: event.detail.location, - date: event.detail.date, - description: event.detail.description, - }, - ]; - newName = ""; // Reset newName and newLocation after adding adventure - newLocation = ""; + adventures = [...adventures, data.adventure]; showToast("Adventure added successfully!"); visitCount.update((n) => n + 1); }) @@ -115,35 +93,29 @@ }; function saveAdventure(event: { detail: Adventure }) { - console.log("Event" + event.detail); + console.log("Event", event.detail); + let detailAdventure = event.detail; - let newAdventure: Adventure = { - type: "mylog", - name: event.detail.name, - location: event.detail.location, - date: event.detail.date, - id: event.detail.id, - description: event.detail.description, - activityTypes: event.detail.activityTypes, - }; - - // put request to /api/visits with id and advneture data + // put request to /api/visits with id and adventure data fetch("/api/visits", { method: "PUT", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ - newAdventure, + detailAdventure, }), }) .then((response) => response.json()) .then((data) => { console.log("Success:", data); // update local array with new data - adventures = adventures.map((adventure) => - adventure.id === event.detail.id ? event.detail : adventure + const index = adventures.findIndex( + (adventure) => adventure.id === detailAdventure.id ); + if (index !== -1) { + adventures[index] = detailAdventure; + } adventureToEdit = undefined; showToast("Adventure edited successfully!"); }) @@ -163,6 +135,12 @@ function shareLink() { let key = generateRandomString(); + + // console log each adventure in the array + for (let i = 0; i < adventures.length; i++) { + console.log(adventures[i]); + } + let data = JSON.stringify(adventures); fetch("/api/share", { method: "POST", @@ -268,11 +246,7 @@ {/if} {#if isShowingMoreFields} - + {/if} {#if adventureToEdit && adventureToEdit.id != undefined}