From d6881013780855683dc69af7158feaa0fd2dd8c8 Mon Sep 17 00:00:00 2001 From: Alex <$(pass /github/email)> Date: Tue, 18 Feb 2025 13:48:39 +0100 Subject: [PATCH] frontend: fixed missing typing in utils --- frontend/src/lib/utils/helpers.js | 76 ++++++++++++++++++++++------ frontend/src/lib/utils/processing.js | 12 ++--- 2 files changed, 66 insertions(+), 22 deletions(-) diff --git a/frontend/src/lib/utils/helpers.js b/frontend/src/lib/utils/helpers.js index 9c9dfc0..f4b82b8 100644 --- a/frontend/src/lib/utils/helpers.js +++ b/frontend/src/lib/utils/helpers.js @@ -1,4 +1,3 @@ -// @ts-nocheck import { quintOut } from 'svelte/easing'; import { crossfade } from 'svelte/transition'; @@ -67,12 +66,22 @@ export function isEmpty(obj) { return true; } +/** + * + * @param {string} dateString + * @returns string + */ export function toRFC3339(dateString) { if (!dateString) dateString = '0001-01-01T00:00:00.000Z'; const date = new Date(dateString); return date.toISOString(); } +/** + * + * @param {string} dateString + * @returns string + */ export function fromRFC3339(dateString) { if (!dateString) dateString = '0001-01-01T00:00:00.000Z'; const date = new Date(dateString); @@ -81,7 +90,7 @@ export function fromRFC3339(dateString) { /** * - * @param {App.Locals.User} user - The user object to format + * @param {App.Locals['user']} user - The user object to format */ export function userDatesFromRFC3339(user) { if (user.dateofbirth) { @@ -108,7 +117,7 @@ export function userDatesFromRFC3339(user) { /** * Formats dates in the user object to RFC3339 format - * @param {App.Locals.User} user - The user object to format + * @param {App.Locals['user']} user - The user object to format */ export function userDatesToRFC3339(user) { if (user.dateofbirth) { @@ -135,12 +144,16 @@ export function userDatesToRFC3339(user) { /** * - * @param {object} obj - The error object to format - * @returns {array} The formatted error object + * @param {string | Array<{field: string, key: string}> | Record} obj - The error object to format + * @returns {Array<{ + * field: string, + * key: string, + * id: number + * }> } Array of formatted error objects */ export function formatError(obj) { const errors = []; - if (typeof obj === 'object' && obj !== null) { + if (typeof obj === 'object') { if (Array.isArray(obj)) { obj.forEach((error) => { errors.push({ @@ -171,21 +184,52 @@ export function formatError(obj) { /** * * @param {string | null} newToken - The new token for the cookie to set - * @param {import('RequestEvent>, string | null>')} event - The event object + * @param {import('@sveltejs/kit').RequestEvent } event - The event object */ export function refreshCookie(newToken, event) { if (newToken) { const match = newToken.match(/jwt=([^;]+)/); if (match) { - if (event) { - event.cookies.set('jwt', match[1], { - path: '/', - httpOnly: true, - secure: process.env.NODE_ENV === 'production', // Secure in production - sameSite: 'lax', - maxAge: 5 * 24 * 60 * 60 // 5 days in seconds - }); - } + event.cookies.set('jwt', match[1], { + path: '/', + httpOnly: true, + secure: process.env.NODE_ENV === 'production', // Secure in production + sameSite: 'lax', + maxAge: 5 * 24 * 60 * 60 // 5 days in seconds + }); } } } + +/** + * Creates a debounced version of an input event handler. + * + * @param {HTMLElement} element - The HTML element to attach the debounced event to. + * @param {number} duration - The delay in milliseconds before the event is triggered after the last input. + * @returns {Object} - An object with a `destroy` method to clean up the event listener. + * + * @example + * + */ +export function debounce(element, duration) { + /** @type{NodeJS.Timeout} */ + let timer; + + /** + * + * @param {CustomEventInit} e + */ + function input(e) { + clearTimeout(timer); + timer = setTimeout(() => { + element.dispatchEvent(new CustomEvent('debouncedinput', e)); + }, duration); + } + + element.addEventListener('input', input); + return { + destroy() { + element.removeEventListener('input', input); + } + }; +} diff --git a/frontend/src/lib/utils/processing.js b/frontend/src/lib/utils/processing.js index 61e231c..5652d26 100644 --- a/frontend/src/lib/utils/processing.js +++ b/frontend/src/lib/utils/processing.js @@ -76,7 +76,7 @@ export function processUserFormData(rawData) { email: String(rawData.object.email), phone: String(rawData.object.phone || ''), company: String(rawData.object.company || ''), - dateofbirth: toRFC3339(rawData.object.dateofbirth), + dateofbirth: toRFC3339(String(rawData.object.dateofbirth)), address: String(rawData.object.address || ''), zip_code: String(rawData.object.zip_code || ''), city: String(rawData.object.city || ''), @@ -86,8 +86,8 @@ export function processUserFormData(rawData) { membership: { id: Number(rawData.object.membership?.id) || 0, status: Number(rawData.object.membership?.status), - start_date: toRFC3339(rawData.object.membership?.start_date), - end_date: toRFC3339(rawData.object.membership?.end_date), + start_date: toRFC3339(String(rawData.object.membership?.start_date)), + end_date: toRFC3339(String(rawData.object.membership?.end_date)), parent_member_id: Number(rawData.object.membership?.parent_member_id) || 0, subscription_model: { id: Number(rawData.object.membership?.subscription_model?.id) || 0, @@ -107,8 +107,8 @@ export function processUserFormData(rawData) { id: Number(rawData.object.licence?.id) || 0, status: Number(rawData.object.licence?.status), number: String(rawData.object.licence?.number || ''), - issued_date: toRFC3339(rawData.object.licence?.issued_date), - expiration_date: toRFC3339(rawData.object.licence?.expiration_date), + issued_date: toRFC3339(String(rawData.object.licence?.issued_date)), + expiration_date: toRFC3339(String(rawData.object.licence?.expiration_date)), country: String(rawData.object.licence?.country || ''), categories: rawData.object.licence?.categories || [] }, @@ -120,7 +120,7 @@ export function processUserFormData(rawData) { iban: String(rawData.object.bank_account?.iban || ''), bic: String(rawData.object.bank_account?.bic || ''), mandate_reference: String(rawData.object.bank_account?.mandate_reference || ''), - mandate_date_signed: toRFC3339(rawData.object.bank_account?.mandate_date_signed) + mandate_date_signed: toRFC3339(String(rawData.object.bank_account?.mandate_date_signed)) } } };