From 2afe6a7cf1a5b0e880180d9efe3fae397df100ad Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Thu, 11 Jul 2024 09:33:52 -0400 Subject: [PATCH] more filtering options --- frontend/src/lib/components/NotFound.svelte | 20 ++++++ .../src/routes/adventures/+page.server.ts | 45 +++++++++++- frontend/src/routes/adventures/+page.svelte | 70 +++++++++++++++++++ frontend/src/routes/visited/+page.svelte | 21 +----- 4 files changed, 136 insertions(+), 20 deletions(-) create mode 100644 frontend/src/lib/components/NotFound.svelte create mode 100644 frontend/src/routes/adventures/+page.svelte diff --git a/frontend/src/lib/components/NotFound.svelte b/frontend/src/lib/components/NotFound.svelte new file mode 100644 index 0000000..e05ade2 --- /dev/null +++ b/frontend/src/lib/components/NotFound.svelte @@ -0,0 +1,20 @@ + + +
+
+
+ Lost +
+

+ No adventures found +

+

+ There are no adventures to display. Add some using the plus button at the bottom right or try + changing filters! +

+
+
diff --git a/frontend/src/routes/adventures/+page.server.ts b/frontend/src/routes/adventures/+page.server.ts index ddd3935..572e011 100644 --- a/frontend/src/routes/adventures/+page.server.ts +++ b/frontend/src/routes/adventures/+page.server.ts @@ -3,14 +3,35 @@ import type { PageServerLoad } from './$types'; const PUBLIC_SERVER_URL = process.env['PUBLIC_SERVER_URL']; import type { Adventure } from '$lib/types'; -const endpoint = PUBLIC_SERVER_URL || 'http://localhost:8000'; - import type { Actions } from '@sveltejs/kit'; import { fetchCSRFToken, tryRefreshToken } from '$lib/index.server'; import { checkLink } from '$lib'; const serverEndpoint = PUBLIC_SERVER_URL || 'http://localhost:8000'; +export const load = (async (event) => { + if (!event.locals.user) { + return redirect(302, '/login'); + } else { + let visitedFetch = await fetch(`${serverEndpoint}/api/adventures/`, { + headers: { + Cookie: `${event.cookies.get('auth')}` + } + }); + if (!visitedFetch.ok) { + console.error('Failed to fetch visited adventures'); + return redirect(302, '/login'); + } else { + let visited = (await visitedFetch.json()) as Adventure[]; + return { + props: { + visited + } + }; + } + } +}) satisfies PageServerLoad; + export const actions: Actions = { create: async (event) => { const formData = await event.request.formData(); @@ -316,5 +337,25 @@ export const actions: Actions = { let image_url = adventure.image; let link_url = adventure.link; return { image_url, link_url }; + }, + get: async (event) => { + const adventureId = event.params.adventureId; + + const res = await fetch(`${serverEndpoint}/api/adventures/${adventureId}/`, { + headers: { + Cookie: `${event.cookies.get('auth')}` + } + }); + + if (!res.ok) { + return { + status: res.status, + body: { error: 'Failed to fetch adventure' } + }; + } + + let adventure = await res.json(); + + return { adventure }; } }; diff --git a/frontend/src/routes/adventures/+page.svelte b/frontend/src/routes/adventures/+page.svelte new file mode 100644 index 0000000..ffcac5c --- /dev/null +++ b/frontend/src/routes/adventures/+page.svelte @@ -0,0 +1,70 @@ + + +
+ +
+ +

My Adventures

+ {#if adventures.length === 0} + + {/if} +
+ +
+ {#each adventures as adventure} + + {/each} +
+
+
+
+ + +
+
diff --git a/frontend/src/routes/visited/+page.svelte b/frontend/src/routes/visited/+page.svelte index 3e64a7b..3a8a8de 100644 --- a/frontend/src/routes/visited/+page.svelte +++ b/frontend/src/routes/visited/+page.svelte @@ -7,6 +7,7 @@ import EditAdventure from '$lib/components/EditAdventure.svelte'; import Lost from '$lib/assets/undraw_lost.svg'; + import NotFound from '$lib/components/NotFound.svelte'; export let data: PageData; console.log(data); @@ -84,6 +85,8 @@ {#if adventures.length > 0}

Visited Adventures

+{:else} + {/if}
@@ -91,21 +94,3 @@ {/each}
- -{#if adventures.length === 0} -
-
-
- Lost -
-

- No visited adventures found -

-

- There are no adventures to display. Add some using the plus button at the bottom right! -

-
-
-{/if}