From 3d09d5aaf638c718593812b818076fe159f7b2a9 Mon Sep 17 00:00:00 2001 From: Johan Girod Date: Wed, 6 Mar 2024 15:00:18 +0100 Subject: [PATCH] refactor: change following CR on #2905 --- .../mon-entreprise/secondary-pages.ts | 2 +- .../pages/statistiques/useStatistiques.tsx | 45 ++++++++++--------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/site/cypress/integration/mon-entreprise/secondary-pages.ts b/site/cypress/integration/mon-entreprise/secondary-pages.ts index 994da0d42..d07c7a563 100644 --- a/site/cypress/integration/mon-entreprise/secondary-pages.ts +++ b/site/cypress/integration/mon-entreprise/secondary-pages.ts @@ -10,7 +10,7 @@ describe('Secondary pages', function () { cy.contains('Statistiques') }) - it.only('Statistics page should be RGAA compliant', function () { + it('Statistics page should be RGAA compliant', function () { cy.visit('/stats') cy.contains('Activer le mode accessibilité').click() cy.get('table').should('be.visible') diff --git a/site/source/pages/statistiques/useStatistiques.tsx b/site/source/pages/statistiques/useStatistiques.tsx index cc11ea8bb..9f084f178 100644 --- a/site/source/pages/statistiques/useStatistiques.tsx +++ b/site/source/pages/statistiques/useStatistiques.tsx @@ -105,29 +105,30 @@ function filterIs(value: string, filter: Filter | ''): boolean { return typeof filter !== 'string' && filter.chapter2 === value } -function mergeVisites(...visites: Visites[]) { - const visitesObjects = visites.map((v) => - Object.fromEntries(v.map((v) => [v.date, v.nombre])) - ) - const dates = new Set(visites.flatMap((v) => v.map((v) => v.date))) +function mergeVisites(...listesDeVisites: Visites[]): Visites { + return Object.values( + listesDeVisites + .flat() + .reduce>((acc, visite) => { + const date = visite.date + acc[date] ??= { + date, + nombre: { + accueil: 0, + simulation_commencee: 0, + simulation_terminee: 0, + }, + } + acc[date].nombre.accueil += visite.nombre.accueil + acc[date].nombre.simulation_commencee += + visite.nombre.simulation_commencee + acc[date].nombre.simulation_terminee ??= + (acc[date].nombre.simulation_terminee || 0) + + (visite.nombre.simulation_terminee || 0) - return Array.from(dates).map((date) => ({ - date, - nombre: { - accueil: visitesObjects.reduce( - (acc, v) => acc + (v[date]?.accueil || 0), - 0 - ), - simulation_commencee: visitesObjects.reduce( - (acc, v) => acc + (v[date]?.simulation_commencee || 0), - 0 - ), - simulation_terminee: visitesObjects.reduce( - (acc, v) => acc + (v[date]?.simulation_terminee || 0), - 0 - ), - }, - })) + return acc + }, {}) + ).sort((a, b) => (a.date < b.date ? -1 : 1)) } const isPAM = (name: string | undefined) =>