refactor: extract date formatting logic into reusable formatFrDate utility function

This commit is contained in:
2026-02-13 16:58:40 +01:00
parent 0cb46014ff
commit 151d568254

View File

@@ -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)