From 6737a568efdf12a058a617b753b1e2777aacedea Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Wed, 15 May 2024 23:51:27 +0000 Subject: [PATCH 1/2] Allow for editing adventures in a trip --- src/lib/components/AdventureCard.svelte | 14 +++++++ src/routes/trip/[id]/+page.svelte | 56 ++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/src/lib/components/AdventureCard.svelte b/src/lib/components/AdventureCard.svelte index 0cdd8a1a..ceaf5e42 100644 --- a/src/lib/components/AdventureCard.svelte +++ b/src/lib/components/AdventureCard.svelte @@ -104,6 +104,20 @@ > {/if} + {#if type == "trip"} + + + + {/if} diff --git a/src/routes/trip/[id]/+page.svelte b/src/routes/trip/[id]/+page.svelte index f615c692..9568f089 100644 --- a/src/routes/trip/[id]/+page.svelte +++ b/src/routes/trip/[id]/+page.svelte @@ -4,8 +4,13 @@ import type { PageData } from "./$types"; import { goto } from "$app/navigation"; import CreateNewAdventure from "$lib/components/CreateNewAdventure.svelte"; - import { addAdventure } from "../../../services/adventureService"; + import { + addAdventure, + saveAdventure, + } from "../../../services/adventureService"; import AdventureCard from "$lib/components/AdventureCard.svelte"; + import EditModal from "$lib/components/EditModal.svelte"; + import SucessToast from "$lib/components/SucessToast.svelte"; export let data: PageData; @@ -14,6 +19,7 @@ let isCreateModalOpen: boolean = false; let adventuresPlans: Adventure[] = []; + let adventureToEdit: Adventure | undefined; onMount(() => { if (data.trip.trip) { @@ -25,6 +31,11 @@ } }); + function handleClose() { + adventureToEdit = undefined; + isCreateModalOpen = false; + } + const newAdventure = async (event: { detail: Adventure }) => { isCreateModalOpen = false; let detailAdventure = event.detail; @@ -37,8 +48,45 @@ // showToast("Failed to add adventure"); } }; + + let isShowingToast: boolean = false; + let toastAction: string = ""; + + function showToast(action: string) { + toastAction = action; + isShowingToast = true; + + setTimeout(() => { + isShowingToast = false; + toastAction = ""; + }, 3000); + } + + async function savePlan(event: { detail: Adventure }) { + let newArray = await saveAdventure(event.detail, adventuresPlans); + if (newArray.length > 0) { + adventuresPlans = newArray; + showToast("Adventure updated successfully!"); + } else { + showToast("Failed to update adventure"); + } + adventureToEdit = undefined; + } + + function edit(event: { detail: number }) { + const adventure = adventuresPlans.find( + (adventure) => adventure.id === event.detail + ); + if (adventure) { + adventureToEdit = adventure; + } + } +{#if isShowingToast} + +{/if} +
{#if trip && trip.name}

{trip.name}

@@ -60,7 +108,7 @@ class="grid xl:grid-cols-3 lg:grid-cols-3 md:grid-cols-2 sm:grid-cols-1 gap-4 mt-4 content-center auto-cols-auto ml-6 mr-6" > {#each adventuresPlans as adventure (adventure.id)} - + {/each} @@ -98,3 +146,7 @@ on:create={newAdventure} /> {/if} + +{#if adventureToEdit && adventureToEdit.id != undefined} + +{/if} From 5ee4d914732bd5bf18096e6adb2d72e28b783d3c Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Thu, 16 May 2024 00:12:09 +0000 Subject: [PATCH 2/2] Add more admin stats and fix icon set issues --- src/routes/settings/+page.server.ts | 94 ++++++++++++----------- src/routes/settings/+page.svelte | 2 + src/routes/settings/admin/+page.server.ts | 21 +++++ src/routes/settings/admin/+page.svelte | 21 ++++- src/routes/trip/[id]/+page.svelte | 23 +++++- 5 files changed, 109 insertions(+), 52 deletions(-) diff --git a/src/routes/settings/+page.server.ts b/src/routes/settings/+page.server.ts index c2cbb5b6..57dfaa5b 100644 --- a/src/routes/settings/+page.server.ts +++ b/src/routes/settings/+page.server.ts @@ -11,11 +11,11 @@ export const load: PageServerLoad = async (event) => { return { user: event.locals.user, }; - return redirect(302, "/login"); + return redirect(302, "/login"); }; export const actions: Actions = { - default: async (event: { request: { formData: () => any; }; }) => { + default: async (event: { request: { formData: () => any } }) => { const formData = await event.request.formData(); let userId = formData.get("user_id"); let username = formData.get("username"); @@ -26,22 +26,22 @@ export const actions: Actions = { let password = formData.get("password"); if (!userId) { - return { - status: 400, - body: { - message: "User ID is required" - } - }; + return { + status: 400, + body: { + message: "User ID is required", + }, + }; } - if (icon.length > 1) { - return { - status: 400, - body: { - message: "Icon must be a single character" - } - }; - } + // if (icon.length > 1) { + // return { + // status: 400, + // body: { + // message: "Icon must be a single character", + // }, + // }; + // } const usernameTaken = await db .select() @@ -50,40 +50,42 @@ export const actions: Actions = { .limit(1) .then((results) => results[0] as unknown as DatabaseUser | undefined); - if (usernameTaken) { - return { - status: 400, - body: { - message: "Username taken!" - } - }; + if (usernameTaken && usernameTaken.id !== userId) { + return { + status: 400, + body: { + message: "Username taken!", + }, + }; } if (password) { - let hashedPassword = await new Argon2id().hash(password); - console.log(hashedPassword) - await db.update(userTable) - .set({ - hashed_password: hashedPassword - }) - .where(eq(userTable.id, userId)); - } - - await db.update(userTable) + let hashedPassword = await new Argon2id().hash(password); + console.log(hashedPassword); + await db + .update(userTable) .set({ - username: username, - first_name: firstName, - last_name: lastName, - icon: icon + hashed_password: hashedPassword, }) .where(eq(userTable.id, userId)); - - return { - status: 200, - body: { - message: "User updated" - - } - }; } -}; \ No newline at end of file + + await db + .update(userTable) + .set({ + username: username, + first_name: firstName, + last_name: lastName, + icon: icon, + }) + .where(eq(userTable.id, userId)); + + // return a page refresh + return { + status: 303, + headers: { + location: "/settings", + }, + }; + }, +}; diff --git a/src/routes/settings/+page.svelte b/src/routes/settings/+page.svelte index 634faedb..81a94e44 100644 --- a/src/routes/settings/+page.svelte +++ b/src/routes/settings/+page.svelte @@ -10,6 +10,8 @@ let icon = data.user?.icon; let signup_date = data.user?.signup_date; let role = data.user?.role; + + // the submit function shoud just reload the page

Settings Page

diff --git a/src/routes/settings/admin/+page.server.ts b/src/routes/settings/admin/+page.server.ts index 08f3037f..78a53769 100644 --- a/src/routes/settings/admin/+page.server.ts +++ b/src/routes/settings/admin/+page.server.ts @@ -4,6 +4,7 @@ import { db } from "$lib/db/db.server"; import { adventureTable, sessionTable, + userPlannedTrips, userTable, userVisitedWorldTravel, } from "$lib/db/schema"; @@ -14,6 +15,9 @@ export const load: PageServerLoad = async (event) => { let users: DatabaseUser[] = []; let visitCount: number = NaN; let userCount: number = NaN; + let planCount: number = NaN; + let tripCount: number = NaN; + let featuredCount: number = NaN; let regionCount: number = NaN; if (!event.locals.user) { return redirect(302, "/login"); @@ -36,12 +40,29 @@ export const load: PageServerLoad = async (event) => { .select({ count: count() }) .from(userVisitedWorldTravel) .execute()) as unknown as number; + planCount = (await db + .select({ count: count() }) + .from(adventureTable) + .where(eq(adventureTable.type, "planner")) + .execute()) as unknown as number; + tripCount = (await db + .select({ count: count() }) + .from(userPlannedTrips) + .execute()) as unknown as number; + featuredCount = (await db + .select({ count: count() }) + .from(adventureTable) + .where(eq(adventureTable.type, "featured")) + .execute()) as unknown as number; } return { users, visitCount, userCount, regionCount, + planCount, + tripCount, + featuredCount, }; }; diff --git a/src/routes/settings/admin/+page.svelte b/src/routes/settings/admin/+page.svelte index dfc9c8c7..db97cb6a 100644 --- a/src/routes/settings/admin/+page.svelte +++ b/src/routes/settings/admin/+page.svelte @@ -62,6 +62,9 @@ let visitCount = $page.data.visitCount[0].count; let userCount = $page.data.userCount[0].count; let regionCount = $page.data.regionCount[0].count; + let tripCount = $page.data.tripCount[0].count; + let planCount = $page.data.planCount[0].count; + let featuredCount = $page.data.featuredCount[0].count;

Admin Settings

@@ -153,17 +156,29 @@
Total Visits
-
{visitCount}
+
{visitCount}
Total Users
-
{userCount}
+
{userCount}
Visited Regions
-
{regionCount}
+
{regionCount}
+
+
+
Total Trips
+
{tripCount}
+
+
+
Total Plans
+
{planCount}
+
+
+
Featured Adventures
+
{featuredCount}
diff --git a/src/routes/trip/[id]/+page.svelte b/src/routes/trip/[id]/+page.svelte index 9568f089..223dd150 100644 --- a/src/routes/trip/[id]/+page.svelte +++ b/src/routes/trip/[id]/+page.svelte @@ -6,6 +6,7 @@ import CreateNewAdventure from "$lib/components/CreateNewAdventure.svelte"; import { addAdventure, + removeAdventure, saveAdventure, } from "../../../services/adventureService"; import AdventureCard from "$lib/components/AdventureCard.svelte"; @@ -43,9 +44,9 @@ let newArray = await addAdventure(detailAdventure, adventuresPlans); if (newArray.length > 0) { adventuresPlans = newArray; - // showToast("Adventure added successfully!"); + showToast("Adventure added successfully!"); } else { - // showToast("Failed to add adventure"); + showToast("Failed to add adventure"); } }; @@ -62,6 +63,22 @@ }, 3000); } + async function remove(event: { detail: number }) { + let initialLength: number = adventuresPlans.length; + let theAdventure = adventuresPlans.find( + (adventure) => adventure.id === event.detail + ); + if (theAdventure) { + let newArray = await removeAdventure(theAdventure, adventuresPlans); + if (newArray.length === initialLength - 1) { + adventuresPlans = newArray; + showToast("Adventure removed successfully!"); + } else { + showToast("Failed to remove adventure"); + } + } + } + async function savePlan(event: { detail: Adventure }) { let newArray = await saveAdventure(event.detail, adventuresPlans); if (newArray.length > 0) { @@ -108,7 +125,7 @@ class="grid xl:grid-cols-3 lg:grid-cols-3 md:grid-cols-2 sm:grid-cols-1 gap-4 mt-4 content-center auto-cols-auto ml-6 mr-6" > {#each adventuresPlans as adventure (adventure.id)} - + {/each}