From dd588135dccd42d553eecddf826726fa0320fffc Mon Sep 17 00:00:00 2001 From: Kworz Date: Tue, 7 May 2024 22:37:58 +0200 Subject: [PATCH] refactored user settings page --- src/lib/i18n/lang/en.json | 13 ++++- src/lib/i18n/lang/fr.json | 13 ++++- src/routes/app/(base)/me/+page.server.ts | 73 ++++++++++++++++++------ src/routes/app/(base)/me/+page.svelte | 59 +++++++++++-------- 4 files changed, 114 insertions(+), 44 deletions(-) diff --git a/src/lib/i18n/lang/en.json b/src/lib/i18n/lang/en.json index 6ba0ebb..1f9d040 100644 --- a/src/lib/i18n/lang/en.json +++ b/src/lib/i18n/lang/en.json @@ -9,7 +9,9 @@ "company_address_city": "Company city", "company_address_postal_code": "Company postal code", "company_address_country": "Company country", - "company_default_vat": "Company tax number" + "company_default_vat": "Company tax number", + "app_menu_left": "Menu on the left", + "app_pages_top_of_table": "Display pages on top of tables" }, "users_groups": { "admin_group_warning": "⚠️ This group has no restrictions as it is defined as a mink administrator.", @@ -120,7 +122,8 @@ "permission_zone": "Permission zone", "user": "User", "user_count": "Users count", - "generic": "Generic" + "generic": "Generic", + "lang": "Language" }, "time": { "hours": "Hours" @@ -186,6 +189,12 @@ "rcu": "Read, create and update", "rcud": "Read, create, update and delete", "is_admin": "Is administrator" + }, + "user": { + "update_personal_parameters": "Update my profile parameters", + "personal_parameters": "My profile parameters", + "mink_personal_parameters": "Personal mink parameters", + "updated_personal_parameters": "Updated settings" } }, "scm": { diff --git a/src/lib/i18n/lang/fr.json b/src/lib/i18n/lang/fr.json index 236de86..7e07fb6 100644 --- a/src/lib/i18n/lang/fr.json +++ b/src/lib/i18n/lang/fr.json @@ -9,7 +9,9 @@ "company_default_vat": "TVA Appliquée par défaut", "company_address_postal_code": "Code postal de la société", "company_address_country": "Pays de la société", - "company_name": "Nom de la société" + "company_name": "Nom de la société", + "app_menu_left": "Menu à gauche", + "app_pages_top_of_table": "Affichage des pages en haut des tables" }, "users_groups": { "lead": "Groupes d'utilisateurs", @@ -169,7 +171,8 @@ "permission_zone": "Zone de permission", "user": "Utilisateur", "user_count": "Nombre d'utilisateurs", - "generic": "Général" + "generic": "Général", + "lang": "Langue" }, "time": { "hours": "Heures" @@ -261,6 +264,12 @@ "rcu": "Lire, créer et modifier", "rcud": "Lire, créer, modifier et supprimer", "is_admin": "Est administrateur" + }, + "user": { + "update_personal_parameters": "Modifier les paramètres de mon profil", + "personal_parameters": "Paramètres de mon profil", + "mink_personal_parameters": "Paramètres personels de mink", + "updated_personal_parameters": "Paramètres mis à jour" } }, "scm": { diff --git a/src/routes/app/(base)/me/+page.server.ts b/src/routes/app/(base)/me/+page.server.ts index 9e417fd..d8b5932 100644 --- a/src/routes/app/(base)/me/+page.server.ts +++ b/src/routes/app/(base)/me/+page.server.ts @@ -1,32 +1,40 @@ -import type { user_settings_keys } from "@prisma/client"; import type { Actions } from "./$types"; - import { fail } from "@sveltejs/kit"; -import Prisma from "@prisma/client"; export const actions: Actions = { updateUserSettings: async ({ request, locals }) => { - const form = await request.formData(); + if(locals.user === null) + return fail(401, { updateUserSettings: { error: "errors.app.user_settings.update.not_authenticated" }}); - try - { - const key = form.get("key")?.toString(); - let value = form.get("value")?.toString(); + const form = await request.formData(); - if(form.has("checkbox")) - value = form.has("value") ? "true" : "false"; + const appLanguage = form.get("app_language")?.toString(); - if(key === undefined || value === undefined) - throw "errors.app.user_settings.update.missing_key_or_value"; + if(appLanguage === undefined || ["fr", "en"].includes(appLanguage) === false) + return fail(400, { updateUserSettings: { error: "errors.app.user_settings.update.no_app_language" }}); - if(Object.keys(Prisma.user_settings_keys).indexOf(key) === -1) - throw "errors.app.user_settings.update.invalid_key"; + const appMenuLeft = form.has("app_menu_left") ? "true" : "false"; + const appPagesTopOfTable = form.has("app_pages_top_of_table") ? "true" : "false"; + try + { await locals.prisma.user_settings.upsert({ - where: { user_id_key: { key: key as user_settings_keys, user_id: locals.user!.id }}, - create: { key: key as user_settings_keys, value, user_id: locals.user!.id }, - update: { value } + where: { user_id_key: { key: "app_language", user_id: locals.user!.id }}, + create: { key: "app_language", value: appLanguage, user_id: locals.user!.id }, + update: { value: appLanguage } + }); + + await locals.prisma.user_settings.upsert({ + where: { user_id_key: { key: "app_menu_left", user_id: locals.user!.id }}, + create: { key: "app_menu_left", value: appMenuLeft, user_id: locals.user!.id }, + update: { value: appMenuLeft } + }); + + await locals.prisma.user_settings.upsert({ + where: { user_id_key: { key: "app_pages_top_of_table", user_id: locals.user!.id }}, + create: { key: "app_pages_top_of_table", value: appPagesTopOfTable, user_id: locals.user!.id }, + update: { value: appPagesTopOfTable } }); return { updateUserSettings: { success: true }} @@ -36,5 +44,36 @@ export const actions: Actions = { console.log(ex); return fail(400, { updateUserSettings: { error: "errors.app.user_settings.update.failed" }}); } + }, + + updateUser: async ({ request, locals }) => { + + console.log("updateUser"); + + if(locals.user === null) + return fail(401, { updateUser: { error: "errors.app.user.update.not_authenticated" }}); + + const form = await request.formData(); + + const username = form.get("username")?.toString(); + const email = form.get("email")?.toString(); + + if(username === undefined || email === undefined) + return fail(400, { updateUser: { error: "errors.app.user.update.no_username_or_email" }}); + + try + { + await locals.prisma.user.update({ + where: { id: locals.user!.id }, + data: { username, email } + }); + + return { updateUser: { success: true }} + } + catch(ex) + { + console.log(ex); + return fail(400, { updateUser: { error: "errors.app.user.update.failed" }}); + } } } \ No newline at end of file diff --git a/src/routes/app/(base)/me/+page.svelte b/src/routes/app/(base)/me/+page.svelte index edc6294..377d662 100644 --- a/src/routes/app/(base)/me/+page.svelte +++ b/src/routes/app/(base)/me/+page.svelte @@ -1,34 +1,47 @@ -

Profil de {$page.data.user?.username}

-

Modifiez votre profil ici

- -

Réglages personel de mink

- -
- {#each Object.entries($page.data.userSettings ?? []) as [key, value]} -
- - {#if typeof value === "boolean"} - - - {:else} - - {/if} - - - {/each} -
\ No newline at end of file +{$page.data.user?.username} - {$_('app.generic.user')} - mink + +

{$_('app.generic.user')}: {$page.data.user?.username}

+ +

{$_('app.user.personal_parameters')}

+ +{#if form !== null && form.updateUser !== undefined} + {#if "error" in form.updateUser} +

{$_(form.updateUser.error)}

+ {:else} +

{$_('app.user.updated_personal_parameters')}

+ {/if} +{/if} + +
+ + + + + + +

{$_('app.user.mink_personal_parameters')}

+ +
+ + + + + + + + + \ No newline at end of file