mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-30 18:29:37 +02:00
Refactor adventure page layout to display activity types and update server files
This commit is contained in:
parent
2da4baf8a1
commit
25adf07874
3 changed files with 51 additions and 57 deletions
|
@ -6,6 +6,8 @@
|
|||
import { onMount } from "svelte";
|
||||
let modal: HTMLDialogElement;
|
||||
|
||||
console.log(adventureToEdit.id);
|
||||
|
||||
let originalName = adventureToEdit.name;
|
||||
|
||||
onMount(() => {
|
||||
|
|
|
@ -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> {
|
||||
if (!event.locals.user) {
|
||||
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();
|
||||
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<Response> {
|
|||
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<Response> {
|
|||
});
|
||||
}
|
||||
|
||||
// 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<Response> {
|
|||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
adventure: { id, name, location, date, description },
|
||||
adventure: body.detailAdventure,
|
||||
message: { message: "Adventure updated" },
|
||||
}),
|
||||
{
|
||||
|
|
|
@ -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}
|
||||
<MoreFieldsInput
|
||||
on:create={createNewAdventure}
|
||||
on:close={handleClose}
|
||||
on:submit={saveAdventure}
|
||||
/>
|
||||
<MoreFieldsInput on:create={createNewAdventure} on:close={handleClose} />
|
||||
{/if}
|
||||
|
||||
{#if adventureToEdit && adventureToEdit.id != undefined}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue