From b7202656d13a3ad027930f28779dea0241d6ed02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o?= <43091603+glazk0@users.noreply.github.com> Date: Sun, 17 Sep 2023 23:04:28 +0200 Subject: [PATCH] feat: puzzle done --- .../chapters/[chapterId]/+page.svelte | 59 ++++++------- .../puzzle/[puzzleId]/+page.server.ts | 56 +++++-------- .../puzzle/[puzzleId]/+page.svelte | 82 ++++++++++++++++--- 3 files changed, 117 insertions(+), 80 deletions(-) diff --git a/src/routes/dashboard/chapters/[chapterId]/+page.svelte b/src/routes/dashboard/chapters/[chapterId]/+page.svelte index 787231c..d36dcae 100644 --- a/src/routes/dashboard/chapters/[chapterId]/+page.svelte +++ b/src/routes/dashboard/chapters/[chapterId]/+page.svelte @@ -1,49 +1,40 @@
-
+
-
-

{chapter.name}

- +
+

{data.chapter.name}

-
    - {#each chapter.puzzles as puzzle} +
      + {#each data.chapter.puzzles as puzzle} {/each} + {#if data.chapter.puzzles.length > 1} + + {/if}
    -
diff --git a/src/routes/dashboard/chapters/[chapterId]/puzzle/[puzzleId]/+page.server.ts b/src/routes/dashboard/chapters/[chapterId]/puzzle/[puzzleId]/+page.server.ts index edb0094..8c9f50f 100644 --- a/src/routes/dashboard/chapters/[chapterId]/puzzle/[puzzleId]/+page.server.ts +++ b/src/routes/dashboard/chapters/[chapterId]/puzzle/[puzzleId]/+page.server.ts @@ -1,10 +1,26 @@ import { API_URL } from '$env/static/private'; -import { error, redirect, type Actions } from '@sveltejs/kit'; +import { error, redirect, type Actions, fail } from '@sveltejs/kit'; import type { PageServerLoad } from './$types'; import type Puzzle from '$lib/components/Puzzle.svelte'; import type { Chapter } from '$lib/types'; +import { superValidate } from 'sveltekit-superforms/server'; +import { z } from 'zod'; + +const puzzleSchema = z.object({ + // answer: z.string().trim(), + // answer need to be filled + answer: z + .string({ + required_error: 'Réponse manquante' + }) + .refine((val) => val.trim() !== '', { + message: 'Réponse manquante' + }), + file: z.any().optional() +}); + export const load = (async ({ parent, fetch, cookies, params: { chapterId, puzzleId } }) => { await parent(); @@ -51,42 +67,14 @@ export const load = (async ({ parent, fetch, cookies, params: { chapterId, puzzl } return { - puzzle: puzzle as Puzzle + puzzle: puzzle as Puzzle, + url: `${API_URL}/puzzleResponse/${puzzleId}`, + session }; }) satisfies PageServerLoad; export const actions = { - default: async (event) => { - const { id } = event.params; - - const data = await event.request.formData(); - - const res = await fetch(`${API_URL}/puzzleResponse/${id}`, { - method: 'POST', - headers: { - Authorization: `Bearer ${event.cookies.get('session')}` - }, - body: data - }); - - return { - success: res.ok - }; - - // throw redirect(303, `/dashboard/puzzles/${id}`); - - // if (res.ok) { - // const token = res.headers.get('Authorization')?.split(' ')[1]; - - // if (!token) throw new Error('No token found'); - - // event.cookies.set('session', token, { - // path: '/' - // }); - - // throw redirect(303, '/dashboard'); - // } - - // throw redirect(303, '/sign-in'); + default: async ({ params, request, cookies }) => { + throw redirect(303, `/dashboard/chapters/${params.chapterId}/puzzle/${params.puzzleId}`); } } satisfies Actions; diff --git a/src/routes/dashboard/chapters/[chapterId]/puzzle/[puzzleId]/+page.svelte b/src/routes/dashboard/chapters/[chapterId]/puzzle/[puzzleId]/+page.svelte index 66d57d6..170c2f2 100644 --- a/src/routes/dashboard/chapters/[chapterId]/puzzle/[puzzleId]/+page.svelte +++ b/src/routes/dashboard/chapters/[chapterId]/puzzle/[puzzleId]/+page.svelte @@ -1,16 +1,33 @@