From 36f4ed5bc609d0264bbd23a73af08be2c643e8d2 Mon Sep 17 00:00:00 2001 From: Imen Chermiti <127976664+ImenOuidou@users.noreply.github.com> Date: Thu, 7 Nov 2024 17:46:14 +0100 Subject: [PATCH] fix: fix marche publiques sort (#356) --- .../NonBorderedTable/hooks.js | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/client/src/components/DataSheets/Sections/SharedComponents/NonBorderedTable/hooks.js b/src/client/src/components/DataSheets/Sections/SharedComponents/NonBorderedTable/hooks.js index f02743ae..f5798f24 100644 --- a/src/client/src/components/DataSheets/Sections/SharedComponents/NonBorderedTable/hooks.js +++ b/src/client/src/components/DataSheets/Sections/SharedComponents/NonBorderedTable/hooks.js @@ -4,7 +4,6 @@ import { formatSiret } from "../../../../../helpers/utils"; import { formatUpperCase } from "../../../../../utils/entreprise/entreprise"; import { getCodePostal } from "../../../../../utils/establishment/establishment"; -// Suppose this function is imported or defined export const getCity = (marche) => marche?.etablissement?.libellecommuneetablissement || marche?.etablissement?.libellecommune2etablissement; @@ -25,22 +24,39 @@ export const useSortableData = (items, config = null) => { if (sortConfig.key === "city") { aValue = getCodePostal(a?.etablissement); // Retrieve city using getCity bValue = getCodePostal(b?.etablissement); - console.log(bValue); } else if (sortConfig.key === "acheteur") { aValue = getAcheteur(a); bValue = getAcheteur(b); + } else if ( + sortConfig.key === "montant" || + sortConfig.key === "dureeMois" + ) { + // Convertir 'montant' en nombre + aValue = parseFloat(a[sortConfig.key]); + bValue = parseFloat(b[sortConfig.key]); } else { aValue = a[sortConfig.key]; bValue = b[sortConfig.key]; } - if (aValue < bValue) { - return sortConfig.direction === "ascending" ? -1 : 1; - } - if (aValue > bValue) { - return sortConfig.direction === "ascending" ? 1 : -1; + // Gérer les valeurs nulles ou indéfinies + if (aValue == null) return 1; + if (bValue == null) return -1; + + // Comparaison appropriée en fonction du type + if (typeof aValue === "number" && typeof bValue === "number") { + return sortConfig.direction === "ascending" + ? aValue - bValue + : bValue - aValue; + } else { + if (aValue < bValue) { + return sortConfig.direction === "ascending" ? -1 : 1; + } + if (aValue > bValue) { + return sortConfig.direction === "ascending" ? 1 : -1; + } + return 0; } - return 0; }); } return sortableItems;