From 3c538499c87c36c26268fa79fbbd59c5f25ac8b9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Th=C3=A9o?= <43091603+glazk0@users.noreply.github.com>
Date: Fri, 22 Sep 2023 14:27:20 +0200
Subject: [PATCH] feat: settings user
---
.../puzzle/[puzzleId]/+page.server.ts | 18 +----
.../puzzle/[puzzleId]/+page.svelte | 22 ++++--
src/routes/dashboard/settings/+page.server.ts | 63 ++++++++++++-----
src/routes/dashboard/settings/+page.svelte | 67 +++++++++++++------
src/routes/forgot-password/+page.svelte | 13 +++-
src/routes/sign-in/+page.svelte | 28 +++++---
src/routes/sign-up/+page.svelte | 19 ++++--
7 files changed, 156 insertions(+), 74 deletions(-)
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 8c9f50f..beb07ec 100644
--- a/src/routes/dashboard/chapters/[chapterId]/puzzle/[puzzleId]/+page.server.ts
+++ b/src/routes/dashboard/chapters/[chapterId]/puzzle/[puzzleId]/+page.server.ts
@@ -1,26 +1,10 @@
import { API_URL } from '$env/static/private';
-import { error, redirect, type Actions, fail } from '@sveltejs/kit';
+import { error, redirect, type Actions } 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();
diff --git a/src/routes/dashboard/chapters/[chapterId]/puzzle/[puzzleId]/+page.svelte b/src/routes/dashboard/chapters/[chapterId]/puzzle/[puzzleId]/+page.svelte
index 54de7d2..d560af5 100644
--- a/src/routes/dashboard/chapters/[chapterId]/puzzle/[puzzleId]/+page.svelte
+++ b/src/routes/dashboard/chapters/[chapterId]/puzzle/[puzzleId]/+page.svelte
@@ -5,9 +5,10 @@
import { goto } from '$app/navigation';
import { page } from '$app/stores';
+ import { Loader2 } from 'lucide-svelte';
+
import { marked, type MarkedOptions } from 'marked';
- import { cn } from '$lib';
import { addToast } from '$lib/components/Toaster.svelte';
import Button from '$lib/components/ui/Button.svelte';
import Input from '$lib/components/ui/Input.svelte';
@@ -17,6 +18,8 @@
$: puzzle = data.puzzle;
$: chapterId = $page.params.chapterId;
+ let submitting = false;
+
const renderer = new marked.Renderer();
renderer.link = (href, title, text) => {
@@ -56,6 +59,8 @@
method="POST"
enctype="multipart/form-data"
use:enhance={async ({ formData, cancel }) => {
+ submitting = true;
+
if (formData.get('answer') === '') {
addToast({
data: {
@@ -112,6 +117,8 @@
});
}
+ submitting = false;
+
return async ({ result }) => {
if (result.type === 'redirect') {
goto(result.location, {
@@ -125,19 +132,22 @@
-
+
-
+
{:else}
diff --git a/src/routes/dashboard/settings/+page.server.ts b/src/routes/dashboard/settings/+page.server.ts
index b0a2001..ac26762 100644
--- a/src/routes/dashboard/settings/+page.server.ts
+++ b/src/routes/dashboard/settings/+page.server.ts
@@ -1,31 +1,62 @@
-import type { Actions } from '@sveltejs/kit';
+import { fail, type Actions } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
+import { z } from 'zod';
+import { superValidate } from 'sveltekit-superforms/server';
+import { API_URL } from '$env/static/private';
-export const load = (async ({ parent }) => {
+const settingSchema = z.object({
+ firstname: z.string({
+ required_error: 'Prénom manquant'
+ }),
+ lastname: z.string({
+ required_error: 'Nom manquant'
+ }),
+ pseudo: z.string({
+ required_error: 'Pseudo manquant'
+ })
+});
+
+export const load = (async ({ parent, locals: { user } }) => {
await parent();
+
+ const form = await superValidate(user, settingSchema);
+
+ return {
+ form
+ };
}) satisfies PageServerLoad;
export const actions = {
- default: async (event) => {
- return {
- success: true
- };
+ default: async ({ request, cookies }) => {
+ const session = cookies.get('session');
- // throw redirect(303, `/dashboard/puzzles/${id}`);
+ const form = await superValidate(request, settingSchema);
- // if (res.ok) {
- // const token = res.headers.get('Authorization')?.split(' ')[1];
+ if (!form.valid) {
+ return fail(400, { form });
+ }
- // if (!token) throw new Error('No token found');
+ const res = await fetch(`${API_URL}/user/settings`, {
+ method: 'POST',
+ headers: {
+ Authorization: `Bearer ${session}`
+ },
+ body: JSON.stringify(form.data)
+ });
- // event.cookies.set('session', token, {
- // path: '/'
- // });
+ if (res.ok) {
+ return {
+ form
+ };
+ }
- // throw redirect(303, '/dashboard');
- // }
+ if (res.status === 400) {
+ form.errors.pseudo = ['Le pseudo est déjà utilisé'];
- // throw redirect(303, '/sign-in');
+ return fail(400, { form });
+ }
+
+ return fail(500, { form });
}
} satisfies Actions;
diff --git a/src/routes/dashboard/settings/+page.svelte b/src/routes/dashboard/settings/+page.svelte
index 23d1bbc..c7a1a01 100644
--- a/src/routes/dashboard/settings/+page.svelte
+++ b/src/routes/dashboard/settings/+page.svelte
@@ -1,17 +1,26 @@
@@ -22,28 +31,45 @@
name="email"
type="email"
placeholder="philipzcwbarlow@peerat.dev"
- value={user?.email}
+ value={data.user?.email}
disabled
/>
-
+
+ {#if $errors.firstname}
+
{$errors.firstname}
+ {/if}
-
+
+ {#if $errors.lastname}
+
{$errors.lastname}
+ {/if}
-
-
-
-
-
+ {#if $errors.pseudo}
+
{$errors.pseudo}
+ {/if}
@@ -51,17 +77,20 @@
class="h-4 w-4"
name="optout"
type="checkbox"
- value={optedOut}
+ bind:value={optedOut}
on:change={() => plausible.set(!optedOut)}
checked={optedOut}
/>
+
Nous utilisons Plausible pour analyser l'utilisation de notre site web de manière anonyme.
-
{/if}
-
-
+
+ {#if submitting}
+
+ {/if}
{confirmation ? 'Modifier' : 'Envoyer le mail'}
diff --git a/src/routes/sign-in/+page.svelte b/src/routes/sign-in/+page.svelte
index 0410a80..85eb140 100644
--- a/src/routes/sign-in/+page.svelte
+++ b/src/routes/sign-in/+page.svelte
@@ -1,8 +1,7 @@
@@ -32,19 +40,21 @@
/>
{#if $errors.passwd}
{$errors.passwd}{/if}
-
-
+
+ {#if submitting}
+
+ {/if}
Se connecter
diff --git a/src/routes/sign-up/+page.svelte b/src/routes/sign-up/+page.svelte
index 9712866..66ccc46 100644
--- a/src/routes/sign-up/+page.svelte
+++ b/src/routes/sign-up/+page.svelte
@@ -6,10 +6,14 @@
import Button from '$lib/components/ui/Button.svelte';
import Input from '$lib/components/ui/Input.svelte';
+ import { Loader2 } from 'lucide-svelte';
export let data: PageData;
const { form, errors, enhance } = superForm(data.form, {
+ onSubmit() {
+ submitting = true;
+ },
onResult({ result }) {
switch (result.type) {
case 'success':
@@ -19,9 +23,12 @@
confirmation = false;
break;
}
+
+ submitting = false;
}
});
+ let submitting = false;
let confirmation = false;
export const snapshot: Snapshot = {
@@ -103,8 +110,10 @@
{/if}
-
-
+
+ {#if submitting}
+
+ {/if}
{confirmation ? "S'inscrire" : 'Continuer'}
@@ -114,9 +123,9 @@
{#if confirmation}
- Pas reçu ?
+
+ Pas reçu ?
+
{/if}