frontend: fixed missing typing in utils

This commit is contained in:
Alex
2025-02-18 13:48:39 +01:00
parent e9d6b58f20
commit d688101378
2 changed files with 66 additions and 22 deletions

View File

@@ -1,4 +1,3 @@
// @ts-nocheck
import { quintOut } from 'svelte/easing'; import { quintOut } from 'svelte/easing';
import { crossfade } from 'svelte/transition'; import { crossfade } from 'svelte/transition';
@@ -67,12 +66,22 @@ export function isEmpty(obj) {
return true; return true;
} }
/**
*
* @param {string} dateString
* @returns string
*/
export function toRFC3339(dateString) { export function toRFC3339(dateString) {
if (!dateString) dateString = '0001-01-01T00:00:00.000Z'; if (!dateString) dateString = '0001-01-01T00:00:00.000Z';
const date = new Date(dateString); const date = new Date(dateString);
return date.toISOString(); return date.toISOString();
} }
/**
*
* @param {string} dateString
* @returns string
*/
export function fromRFC3339(dateString) { export function fromRFC3339(dateString) {
if (!dateString) dateString = '0001-01-01T00:00:00.000Z'; if (!dateString) dateString = '0001-01-01T00:00:00.000Z';
const date = new Date(dateString); 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) { export function userDatesFromRFC3339(user) {
if (user.dateofbirth) { if (user.dateofbirth) {
@@ -108,7 +117,7 @@ export function userDatesFromRFC3339(user) {
/** /**
* Formats dates in the user object to RFC3339 format * 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) { export function userDatesToRFC3339(user) {
if (user.dateofbirth) { if (user.dateofbirth) {
@@ -135,12 +144,16 @@ export function userDatesToRFC3339(user) {
/** /**
* *
* @param {object} obj - The error object to format * @param {string | Array<{field: string, key: string}> | Record<string, {key: string}>} obj - The error object to format
* @returns {array} The formatted error object * @returns {Array<{
* field: string,
* key: string,
* id: number
* }> } Array of formatted error objects
*/ */
export function formatError(obj) { export function formatError(obj) {
const errors = []; const errors = [];
if (typeof obj === 'object' && obj !== null) { if (typeof obj === 'object') {
if (Array.isArray(obj)) { if (Array.isArray(obj)) {
obj.forEach((error) => { obj.forEach((error) => {
errors.push({ errors.push({
@@ -171,21 +184,52 @@ export function formatError(obj) {
/** /**
* *
* @param {string | null} newToken - The new token for the cookie to set * @param {string | null} newToken - The new token for the cookie to set
* @param {import('RequestEvent<Partial<Record<string, string>>, string | null>')} event - The event object * @param {import('@sveltejs/kit').RequestEvent } event - The event object
*/ */
export function refreshCookie(newToken, event) { export function refreshCookie(newToken, event) {
if (newToken) { if (newToken) {
const match = newToken.match(/jwt=([^;]+)/); const match = newToken.match(/jwt=([^;]+)/);
if (match) { if (match) {
if (event) { event.cookies.set('jwt', match[1], {
event.cookies.set('jwt', match[1], { path: '/',
path: '/', httpOnly: true,
httpOnly: true, secure: process.env.NODE_ENV === 'production', // Secure in production
secure: process.env.NODE_ENV === 'production', // Secure in production sameSite: 'lax',
sameSite: 'lax', maxAge: 5 * 24 * 60 * 60 // 5 days in seconds
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
* <input use:debounce={300} on:debouncedinput={handleInput} />
*/
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);
}
};
}

View File

@@ -76,7 +76,7 @@ export function processUserFormData(rawData) {
email: String(rawData.object.email), email: String(rawData.object.email),
phone: String(rawData.object.phone || ''), phone: String(rawData.object.phone || ''),
company: String(rawData.object.company || ''), company: String(rawData.object.company || ''),
dateofbirth: toRFC3339(rawData.object.dateofbirth), dateofbirth: toRFC3339(String(rawData.object.dateofbirth)),
address: String(rawData.object.address || ''), address: String(rawData.object.address || ''),
zip_code: String(rawData.object.zip_code || ''), zip_code: String(rawData.object.zip_code || ''),
city: String(rawData.object.city || ''), city: String(rawData.object.city || ''),
@@ -86,8 +86,8 @@ export function processUserFormData(rawData) {
membership: { membership: {
id: Number(rawData.object.membership?.id) || 0, id: Number(rawData.object.membership?.id) || 0,
status: Number(rawData.object.membership?.status), status: Number(rawData.object.membership?.status),
start_date: toRFC3339(rawData.object.membership?.start_date), start_date: toRFC3339(String(rawData.object.membership?.start_date)),
end_date: toRFC3339(rawData.object.membership?.end_date), end_date: toRFC3339(String(rawData.object.membership?.end_date)),
parent_member_id: Number(rawData.object.membership?.parent_member_id) || 0, parent_member_id: Number(rawData.object.membership?.parent_member_id) || 0,
subscription_model: { subscription_model: {
id: Number(rawData.object.membership?.subscription_model?.id) || 0, id: Number(rawData.object.membership?.subscription_model?.id) || 0,
@@ -107,8 +107,8 @@ export function processUserFormData(rawData) {
id: Number(rawData.object.licence?.id) || 0, id: Number(rawData.object.licence?.id) || 0,
status: Number(rawData.object.licence?.status), status: Number(rawData.object.licence?.status),
number: String(rawData.object.licence?.number || ''), number: String(rawData.object.licence?.number || ''),
issued_date: toRFC3339(rawData.object.licence?.issued_date), issued_date: toRFC3339(String(rawData.object.licence?.issued_date)),
expiration_date: toRFC3339(rawData.object.licence?.expiration_date), expiration_date: toRFC3339(String(rawData.object.licence?.expiration_date)),
country: String(rawData.object.licence?.country || ''), country: String(rawData.object.licence?.country || ''),
categories: rawData.object.licence?.categories || [] categories: rawData.object.licence?.categories || []
}, },
@@ -120,7 +120,7 @@ export function processUserFormData(rawData) {
iban: String(rawData.object.bank_account?.iban || ''), iban: String(rawData.object.bank_account?.iban || ''),
bic: String(rawData.object.bank_account?.bic || ''), bic: String(rawData.object.bank_account?.bic || ''),
mandate_reference: String(rawData.object.bank_account?.mandate_reference || ''), 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))
} }
} }
}; };