mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-08-05 21:25:19 +02:00
Refactor CreateNewAdventure component to add activity types and update server files
This commit is contained in:
parent
895522b2ac
commit
296659ea27
4 changed files with 51 additions and 5 deletions
|
@ -7,6 +7,7 @@
|
||||||
name: "",
|
name: "",
|
||||||
location: "",
|
location: "",
|
||||||
date: "",
|
date: "",
|
||||||
|
activityTypes: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
import { createEventDispatcher } from "svelte";
|
import { createEventDispatcher } from "svelte";
|
||||||
|
@ -23,6 +24,7 @@
|
||||||
});
|
});
|
||||||
|
|
||||||
function create() {
|
function create() {
|
||||||
|
addActivityType();
|
||||||
dispatch("create", newAdventure);
|
dispatch("create", newAdventure);
|
||||||
console.log(newAdventure);
|
console.log(newAdventure);
|
||||||
}
|
}
|
||||||
|
@ -36,6 +38,22 @@
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let activityInput: string = "";
|
||||||
|
|
||||||
|
function addActivityType() {
|
||||||
|
if (activityInput.trim() !== "") {
|
||||||
|
const activities = activityInput
|
||||||
|
.split(",")
|
||||||
|
.map((activity) => activity.trim());
|
||||||
|
newAdventure.activityTypes = [
|
||||||
|
...(newAdventure.activityTypes || []),
|
||||||
|
...activities,
|
||||||
|
];
|
||||||
|
activityInput = "";
|
||||||
|
}
|
||||||
|
console.log(newAdventure.activityTypes);
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<dialog id="my_modal_1" class="modal">
|
<dialog id="my_modal_1" class="modal">
|
||||||
|
@ -85,8 +103,19 @@
|
||||||
class="input input-bordered w-full max-w-xs"
|
class="input input-bordered w-full max-w-xs"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<button class="btn btn-primary mr-4 mt-4" on:click={create}
|
<div>
|
||||||
>Create</button
|
<label for="date">Activity Types</label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
id="activityTypes"
|
||||||
|
bind:value={activityInput}
|
||||||
|
class="input input-bordered w-full max-w-xs"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
class="btn btn-primary mr-4 mt-4"
|
||||||
|
on:click={create}>Create</button
|
||||||
>
|
>
|
||||||
<!-- if there is a button in form, it will close the modal -->
|
<!-- if there is a button in form, it will close the modal -->
|
||||||
<button class="btn mt-4" on:click={close}>Close</button>
|
<button class="btn mt-4" on:click={close}>Close</button>
|
||||||
|
|
|
@ -22,7 +22,20 @@ export async function GET(event: RequestEvent): Promise<Response> {
|
||||||
.execute();
|
.execute();
|
||||||
return new Response(
|
return new Response(
|
||||||
// turn the result into an Adventure object array
|
// turn the result into an Adventure object array
|
||||||
JSON.stringify(result.map((r) => r as Adventure)),
|
JSON.stringify(
|
||||||
|
result.map((r) => {
|
||||||
|
const adventure: Adventure = r as Adventure;
|
||||||
|
if (typeof adventure.activityTypes === "string") {
|
||||||
|
try {
|
||||||
|
adventure.activityTypes = JSON.parse(adventure.activityTypes);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error parsing activityTypes:", error);
|
||||||
|
adventure.activityTypes = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return adventure;
|
||||||
|
})
|
||||||
|
),
|
||||||
{
|
{
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: {
|
headers: {
|
||||||
|
@ -86,7 +99,7 @@ export async function POST(event: RequestEvent): Promise<Response> {
|
||||||
|
|
||||||
const { newAdventure } = await event.request.json();
|
const { newAdventure } = await event.request.json();
|
||||||
console.log(newAdventure);
|
console.log(newAdventure);
|
||||||
const { name, location, date, description } = newAdventure;
|
const { name, location, date, description, activityTypes } = newAdventure;
|
||||||
|
|
||||||
if (!name) {
|
if (!name) {
|
||||||
return error(400, {
|
return error(400, {
|
||||||
|
@ -94,6 +107,8 @@ export async function POST(event: RequestEvent): Promise<Response> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(activityTypes);
|
||||||
|
|
||||||
// insert the adventure to the user's visited list
|
// insert the adventure to the user's visited list
|
||||||
let res = await db
|
let res = await db
|
||||||
.insert(adventureTable)
|
.insert(adventureTable)
|
||||||
|
@ -104,6 +119,7 @@ export async function POST(event: RequestEvent): Promise<Response> {
|
||||||
location: location || null,
|
location: location || null,
|
||||||
date: date || null,
|
date: date || null,
|
||||||
description: description || null,
|
description: description || null,
|
||||||
|
activityTypes: JSON.stringify(activityTypes) || null,
|
||||||
})
|
})
|
||||||
.returning({ insertedId: adventureTable.id })
|
.returning({ insertedId: adventureTable.id })
|
||||||
.execute();
|
.execute();
|
||||||
|
|
|
@ -8,7 +8,6 @@ export const load: PageServerLoad = async (event) => {
|
||||||
}
|
}
|
||||||
const response = await event.fetch("/api/visits");
|
const response = await event.fetch("/api/visits");
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
// let array = result.adventures as Adventure[];
|
|
||||||
return {
|
return {
|
||||||
result,
|
result,
|
||||||
};
|
};
|
||||||
|
|
|
@ -67,6 +67,7 @@
|
||||||
date: event.detail.date,
|
date: event.detail.date,
|
||||||
description: event.detail.description,
|
description: event.detail.description,
|
||||||
id: -1,
|
id: -1,
|
||||||
|
activityTypes: event.detail.activityTypes,
|
||||||
};
|
};
|
||||||
|
|
||||||
fetch("/api/visits", {
|
fetch("/api/visits", {
|
||||||
|
@ -123,6 +124,7 @@
|
||||||
date: event.detail.date,
|
date: event.detail.date,
|
||||||
id: event.detail.id,
|
id: event.detail.id,
|
||||||
description: event.detail.description,
|
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 advneture data
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue