1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-08-01 11:15:17 +02:00

Refactor adventure page layout to display activity types and update server files

This commit is contained in:
Sean Morley 2024-05-03 21:29:35 +00:00
parent 2da4baf8a1
commit 25adf07874
3 changed files with 51 additions and 57 deletions

View file

@ -6,6 +6,8 @@
import { onMount } from "svelte"; import { onMount } from "svelte";
let modal: HTMLDialogElement; let modal: HTMLDialogElement;
console.log(adventureToEdit.id);
let originalName = adventureToEdit.name; let originalName = adventureToEdit.name;
onMount(() => { onMount(() => {

View file

@ -86,7 +86,6 @@ export async function DELETE(event: RequestEvent): Promise<Response> {
}); });
} }
// add the adventure to the user's visited list
export async function POST(event: RequestEvent): Promise<Response> { export async function POST(event: RequestEvent): Promise<Response> {
if (!event.locals.user) { if (!event.locals.user) {
return new Response(JSON.stringify({ error: "No user found" }), { return new Response(JSON.stringify({ error: "No user found" }), {
@ -97,9 +96,15 @@ export async function POST(event: RequestEvent): Promise<Response> {
}); });
} }
const { newAdventure } = await event.request.json(); const body = await event.request.json();
console.log(newAdventure); if (!body.detailAdventure) {
const { name, location, date, description, activityTypes } = newAdventure; return error(400, {
message: "No adventure data provided",
});
}
const { name, location, date, description, activityTypes } =
body.detailAdventure;
if (!name) { if (!name) {
return error(400, { return error(400, {
@ -127,10 +132,12 @@ export async function POST(event: RequestEvent): Promise<Response> {
let insertedId = res[0].insertedId; let insertedId = res[0].insertedId;
console.log(insertedId); console.log(insertedId);
body.detailAdventure.id = insertedId;
// return a response with the adventure object values // return a response with the adventure object values
return new Response( return new Response(
JSON.stringify({ JSON.stringify({
adventure: { name, location, date }, adventure: body.detailAdventure,
message: { message: "Adventure added" }, message: { message: "Adventure added" },
id: insertedId, id: insertedId,
}), }),
@ -154,10 +161,21 @@ export async function PUT(event: RequestEvent): Promise<Response> {
}); });
} }
// get properties from the body const body = await event.request.json();
const { newAdventure } = await event.request.json(); if (!body.detailAdventure) {
console.log(newAdventure); return error(400, {
const { name, location, date, id, description, activityTypes } = newAdventure; 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 // update the adventure in the user's visited list
await db await db
@ -179,7 +197,7 @@ export async function PUT(event: RequestEvent): Promise<Response> {
return new Response( return new Response(
JSON.stringify({ JSON.stringify({
adventure: { id, name, location, date, description }, adventure: body.detailAdventure,
message: { message: "Adventure updated" }, message: { message: "Adventure updated" },
}), }),
{ {

View file

@ -15,9 +15,6 @@
import { visitCount } from "$lib/utils/stores/visitCountStore"; import { visitCount } from "$lib/utils/stores/visitCountStore";
import MoreFieldsInput from "$lib/components/CreateNewAdventure.svelte"; import MoreFieldsInput from "$lib/components/CreateNewAdventure.svelte";
let newName = "";
let newLocation = "";
let isShowingMoreFields = false; let isShowingMoreFields = false;
let adventureToEdit: Adventure | undefined; let adventureToEdit: Adventure | undefined;
@ -60,15 +57,9 @@
} }
const createNewAdventure = (event: { detail: Adventure }) => { const createNewAdventure = (event: { detail: Adventure }) => {
let newAdventure: Adventure = { isShowingMoreFields = false;
type: "mylog", let detailAdventure = event.detail;
name: event.detail.name, console.log("Event" + event.detail.name);
location: event.detail.location,
date: event.detail.date,
description: event.detail.description,
id: -1,
activityTypes: event.detail.activityTypes,
};
fetch("/api/visits", { fetch("/api/visits", {
method: "POST", method: "POST",
@ -76,7 +67,7 @@
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify({ body: JSON.stringify({
newAdventure, detailAdventure,
}), }),
}) })
.then((response) => { .then((response) => {
@ -90,21 +81,8 @@
return response.json(); return response.json();
}) })
.then((data) => { .then((data) => {
let newId = data.id;
// add to local array for instant view update // add to local array for instant view update
adventures = [ adventures = [...adventures, data.adventure];
...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 = "";
showToast("Adventure added successfully!"); showToast("Adventure added successfully!");
visitCount.update((n) => n + 1); visitCount.update((n) => n + 1);
}) })
@ -115,35 +93,29 @@
}; };
function saveAdventure(event: { detail: Adventure }) { function saveAdventure(event: { detail: Adventure }) {
console.log("Event" + event.detail); console.log("Event", event.detail);
let detailAdventure = event.detail;
let newAdventure: Adventure = { // put request to /api/visits with id and adventure data
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
fetch("/api/visits", { fetch("/api/visits", {
method: "PUT", method: "PUT",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify({ body: JSON.stringify({
newAdventure, detailAdventure,
}), }),
}) })
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
console.log("Success:", data); console.log("Success:", data);
// update local array with new data // update local array with new data
adventures = adventures.map((adventure) => const index = adventures.findIndex(
adventure.id === event.detail.id ? event.detail : adventure (adventure) => adventure.id === detailAdventure.id
); );
if (index !== -1) {
adventures[index] = detailAdventure;
}
adventureToEdit = undefined; adventureToEdit = undefined;
showToast("Adventure edited successfully!"); showToast("Adventure edited successfully!");
}) })
@ -163,6 +135,12 @@
function shareLink() { function shareLink() {
let key = generateRandomString(); 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); let data = JSON.stringify(adventures);
fetch("/api/share", { fetch("/api/share", {
method: "POST", method: "POST",
@ -268,11 +246,7 @@
{/if} {/if}
{#if isShowingMoreFields} {#if isShowingMoreFields}
<MoreFieldsInput <MoreFieldsInput on:create={createNewAdventure} on:close={handleClose} />
on:create={createNewAdventure}
on:close={handleClose}
on:submit={saveAdventure}
/>
{/if} {/if}
{#if adventureToEdit && adventureToEdit.id != undefined} {#if adventureToEdit && adventureToEdit.id != undefined}