frontend: fixed missing typing in utils
This commit is contained in:
@@ -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<string, {key: string}>} 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,13 +184,12 @@ export function formatError(obj) {
|
||||
/**
|
||||
*
|
||||
* @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) {
|
||||
if (newToken) {
|
||||
const match = newToken.match(/jwt=([^;]+)/);
|
||||
if (match) {
|
||||
if (event) {
|
||||
event.cookies.set('jwt', match[1], {
|
||||
path: '/',
|
||||
httpOnly: true,
|
||||
@@ -187,5 +199,37 @@ export function refreshCookie(newToken, event) {
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user