From 2c7c821b391cfbac8126cb082b788bf96d6018c6 Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Sun, 17 Nov 2024 09:10:25 +0100 Subject: [PATCH 1/3] fix(frontend): date normalization --- frontend/src/components/inbox/components/Chat.tsx | 4 +++- .../src/components/inbox/components/ConversationsList.tsx | 3 ++- frontend/src/utils/date.ts | 6 ++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/frontend/src/components/inbox/components/Chat.tsx b/frontend/src/components/inbox/components/Chat.tsx index b6c50f22..f7e58603 100644 --- a/frontend/src/components/inbox/components/Chat.tsx +++ b/frontend/src/components/inbox/components/Chat.tsx @@ -22,6 +22,7 @@ import { useAuth } from "@/hooks/useAuth"; import { useConfig } from "@/hooks/useConfig"; import { useTranslate } from "@/hooks/useTranslate"; import { EntityType } from "@/services/types"; +import { normalizeDate } from "@/utils/date"; import { getAvatarSrc, @@ -115,8 +116,9 @@ export function Chat() { key={message.id} title={`${subscriber.first_name} ${ subscriber.last_name - } : ${message.createdAt.toLocaleString( + } : ${normalizeDate( i18n.language, + message.createdAt, )}`} src={getAvatarSrc( apiUrl, diff --git a/frontend/src/components/inbox/components/ConversationsList.tsx b/frontend/src/components/inbox/components/ConversationsList.tsx index f216f719..fc0f388e 100644 --- a/frontend/src/components/inbox/components/ConversationsList.tsx +++ b/frontend/src/components/inbox/components/ConversationsList.tsx @@ -20,6 +20,7 @@ import { useConfig } from "@/hooks/useConfig"; import { useTranslate } from "@/hooks/useTranslate"; import { Title } from "@/layout/content/Title"; import { EntityType, RouterType } from "@/services/types"; +import { normalizeDate } from "@/utils/date"; import { extractQueryParamsUrl } from "@/utils/URL"; import { getAvatarSrc } from "../helpers/mapMessages"; @@ -82,7 +83,7 @@ export const SubscribersList = (props: { {subscriber.first_name} {subscriber.last_name}
- {subscriber.lastvisit?.toLocaleString(i18n.language)} + {normalizeDate(i18n.language, subscriber.lastvisit)}
diff --git a/frontend/src/utils/date.ts b/frontend/src/utils/date.ts index 395ef513..80f877a3 100644 --- a/frontend/src/utils/date.ts +++ b/frontend/src/utils/date.ts @@ -14,3 +14,9 @@ export const getDateTimeFormatter = (date: Date) => ({ val: DATE_TIME_FORMAT, }, }); + +export const normalizeDate = (locale: string, dateField?: Date | string) => + (typeof dateField === "string" + ? new Date(dateField) + : dateField + )?.toLocaleString(locale); From 70e38504ea9e2bd3029c7dd9c1dc1889531878a6 Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Sun, 13 Apr 2025 22:52:39 +0100 Subject: [PATCH 2/3] fix: apply feedback --- frontend/src/utils/date.ts | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/frontend/src/utils/date.ts b/frontend/src/utils/date.ts index 80f877a3..461e76bb 100644 --- a/frontend/src/utils/date.ts +++ b/frontend/src/utils/date.ts @@ -15,8 +15,20 @@ export const getDateTimeFormatter = (date: Date) => ({ }, }); -export const normalizeDate = (locale: string, dateField?: Date | string) => - (typeof dateField === "string" - ? new Date(dateField) - : dateField - )?.toLocaleString(locale); +/** + * Normalizes and formats a date using the provided locale + * + * @param {string} locale - The locale to use for formatting (e.g., 'en-US', 'fr-FR') + * @param {Date | string} dateField - The date to format, either as Date object or string + * @returns {string | undefined} Formatted date string, or undefined if dateField is undefined + */ +export const normalizeDate = ( + locale: string = "en-US", + dateField?: Date | string, +) => { + if (!dateField) return undefined; + + const date = typeof dateField === "string" ? new Date(dateField) : dateField; + + return !isNaN(date.getTime()) ? date.toLocaleString(locale) : undefined; +}; From 62b77638bb0838ef39a260ed1adc37ae7070c6f8 Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Sun, 13 Apr 2025 22:57:37 +0100 Subject: [PATCH 3/3] fix: apply feedback --- frontend/src/utils/date.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/frontend/src/utils/date.ts b/frontend/src/utils/date.ts index 461e76bb..f008e3ee 100644 --- a/frontend/src/utils/date.ts +++ b/frontend/src/utils/date.ts @@ -20,15 +20,19 @@ export const getDateTimeFormatter = (date: Date) => ({ * * @param {string} locale - The locale to use for formatting (e.g., 'en-US', 'fr-FR') * @param {Date | string} dateField - The date to format, either as Date object or string + * @param {Intl.DateTimeFormatOptions} options - An object that contains one or more properties that specify comparison options * @returns {string | undefined} Formatted date string, or undefined if dateField is undefined */ export const normalizeDate = ( locale: string = "en-US", dateField?: Date | string, + options?: Intl.DateTimeFormatOptions, ) => { if (!dateField) return undefined; const date = typeof dateField === "string" ? new Date(dateField) : dateField; - return !isNaN(date.getTime()) ? date.toLocaleString(locale) : undefined; + return !isNaN(date.getTime()) + ? date.toLocaleString(locale, options) + : undefined; };