From 151d56825472545fd4302c10076327d7470f53f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C9=A7=CF=83=E2=84=93=CF=83?= Date: Fri, 13 Feb 2026 16:58:40 +0100 Subject: [PATCH] refactor: extract date formatting logic into reusable formatFrDate utility function --- src/components/AppartCard.jsx | 53 +++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/src/components/AppartCard.jsx b/src/components/AppartCard.jsx index f51f82b..779f395 100644 --- a/src/components/AppartCard.jsx +++ b/src/components/AppartCard.jsx @@ -51,6 +51,39 @@ function InfoChip({ icon: Icon, label, value, sub }) { ); } +const formatFrDate = (value) => { + if (!value) return null; + + const toDate = (input) => { + const date = new Date(input); + return Number.isNaN(date.getTime()) ? null : date; + }; + + let date = value instanceof Date ? value : null; + + if (!date && (typeof value === "number" || typeof value === "string")) { + date = toDate(value); + + if (!date && typeof value === "string") { + const match = value.trim().match(/^([0-9]{1,2})[\/\-]([0-9]{1,2})[\/\-]([0-9]{2,4})$/); + + if (match) { + const [, day, month, year] = match; + const normalizedYear = year.length === 2 ? `20${year}` : year; + date = toDate(`${normalizedYear.padStart(4, "0")}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`); + } + } + } + + if (!date) return null; + + return date.toLocaleDateString("fr-FR", { + day: "numeric", + month: "short", + year: "numeric", + }); +}; + export default function AppartCard({ annonce, onDelete, onUpdated }) { const [noteP, setNoteP] = useState(annonce.NOTE_P || 0); const [noteA, setNoteA] = useState(annonce.NOTE_A || 0); @@ -87,24 +120,8 @@ export default function AppartCard({ annonce, onDelete, onUpdated }) { const avgNote = noteP || noteA ? ((noteP + noteA) / (noteP && noteA ? 2 : 1)).toFixed(1) : null; - const dateDispo = annonce.DATE_DISPO && annonce.DATE_DISPO.trim() - ? (() => { - const date = new Date(annonce.DATE_DISPO); - return isNaN(date.getTime()) ? null : date.toLocaleDateString("fr-FR", { - day: "numeric", - month: "short", - year: "numeric", - }); - })() - : null; - - const dateAjout = annonce.createdAt - ? new Date(annonce.createdAt).toLocaleDateString("fr-FR", { - day: "numeric", - month: "short", - year: "numeric", - }) - : null; + const dateDispo = formatFrDate(annonce.DATE_DISPO); + const dateAjout = formatFrDate(annonce.createdAt); const prixM2 = annonce.SURFACE && annonce.PRIX ? ((annonce.PRIX) / annonce.SURFACE).toFixed(2)