From 49986ffd79e8e5d8d4a508bfda47d1cc12335943 Mon Sep 17 00:00:00 2001 From: "$(pass /github/name)" <$(pass /github/email)> Date: Fri, 20 Sep 2024 08:37:58 +0200 Subject: [PATCH] frontend:chg:generalized form field serialization --- .../routes/auth/about/[id]/+page.server.js | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/frontend/src/routes/auth/about/[id]/+page.server.js b/frontend/src/routes/auth/about/[id]/+page.server.js index 0a9ab36..454a509 100644 --- a/frontend/src/routes/auth/about/[id]/+page.server.js +++ b/frontend/src/routes/auth/about/[id]/+page.server.js @@ -22,10 +22,15 @@ export const actions = { */ updateUser: async ({ request, fetch, cookies, locals }) => { const formData = await request.formData(); - const firstName = String(formData.get("first_name")); - const lastName = String(formData.get("last_name")); - const phone = String(formData.get("phone")); - const birthDate = String(formData.get("birth_date")); + /** @type {Record} */ + const updateData = {}; + + // Convert FormData to a plain object + formData.forEach((value, key) => { + if (typeof value === "string" && value !== "") { + updateData[key] = value; + } + }); const apiURL = `${BASE_API_URI}/backend/users/update/`; @@ -36,12 +41,7 @@ export const actions = { "Content-Type": "application/json", Cookie: `jwt=${cookies.get("jwt")}`, }, - body: JSON.stringify({ - first_name: firstName, - last_name: lastName, - phone: phone, - birth_date: birthDate, - }), + body: JSON.stringify(updateData), }); if (!res.ok) { @@ -57,7 +57,14 @@ export const actions = { if (locals.user.date_of_birth) { locals.user.date_of_birth = response["date_of_birth"].split("T")[0]; } - + if (locals.user.membership?.start_date) { + locals.user.membership.start_date = + locals.user.membership.start_date.split("T")[0]; + } + if (locals.user.membership?.end_date) { + locals.user.membership.end_date = + locals.user.membership.end_date.split("T")[0]; + } throw redirect(303, `/auth/about/${response.id}`); }, /**