From 2ef06f4d818c0fe6eb0ed252696a37de1f1529f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Fri, 10 Nov 2023 16:30:25 +0100 Subject: [PATCH 1/3] fix: prevent user from typing in date field --- webapp-next/components/filters/Dates.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/webapp-next/components/filters/Dates.tsx b/webapp-next/components/filters/Dates.tsx index 8d8619d..d0f12ae 100644 --- a/webapp-next/components/filters/Dates.tsx +++ b/webapp-next/components/filters/Dates.tsx @@ -46,6 +46,9 @@ export function FilterDates() { setStartDate(start); setEndDate(end); }} + onKeyDown={e => { + e.preventDefault(); + }} className={startDate && endDate ? 'has-value' : ''} wrapperClassName={startDate && endDate ? 'has-value' : ''} startDate={startDate} From 298125b5ccceb256f75e1ae90affcd1100bab51a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Fri, 10 Nov 2023 17:53:57 +0100 Subject: [PATCH 2/3] fix: out of bounds range date in line chart --- webapp-next/components/charts/line/Line.tsx | 12 +++++++++-- webapp-next/utils/tools.ts | 23 ++++++++++++++++----- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/webapp-next/components/charts/line/Line.tsx b/webapp-next/components/charts/line/Line.tsx index 377d4c3..0f1b226 100644 --- a/webapp-next/components/charts/line/Line.tsx +++ b/webapp-next/components/charts/line/Line.tsx @@ -24,7 +24,7 @@ export const ChartLine = (props: Props) => { throw new Error('Menu must be used within a Cm2dProvider'); } - const { saveAggregateX } = context; + const { filters, saveAggregateX } = context; const [displayDatasets, setDisplayDatasets] = useState([]); @@ -75,9 +75,17 @@ export const ChartLine = (props: Props) => { ); }, [datasets]); - if (!datasets.length) return <>; + if (!datasets.length || !filters.start_date || !filters.end_date) + return <>; + const min = new Date(filters.start_date); + const max = new Date(filters.end_date); const xValues = datasets[0].hits.map((item: any) => { + const currentDate = new Date(item.key_as_string); + + if (currentDate.getTime() < min.getTime()) return dateToWeekYear(min); + if (currentDate.getTime() > max.getTime()) return dateToWeekYear(max); + return dateToWeekYear(new Date(item.key_as_string)); }); diff --git a/webapp-next/utils/tools.ts b/webapp-next/utils/tools.ts index 435f3e1..3d6b0c1 100644 --- a/webapp-next/utils/tools.ts +++ b/webapp-next/utils/tools.ts @@ -1,6 +1,6 @@ -import { Filters, SearchCategory, View } from './cm2d-provider'; import { format } from 'date-fns'; import moment from 'moment'; +import { Filters, SearchCategory, View } from './cm2d-provider'; export const viewRefs: { label: string; value: View }[] = [ { label: 'Vue courbe', value: 'line' }, @@ -357,10 +357,23 @@ export function dateToMonthYear(date: Date): string { return formattedDate; } -export function dateToWeekYear(date: Date): string { - const weekNumber: string = format(date, 'w'); - const year: string = format(date, 'yyyy'); - return `S${weekNumber} ${year}`; +function getWeekNumber(date: Date): number { + const startOfYear = new Date(date.getFullYear(), 0, 1); + const millisecondsPerDay = 24 * 60 * 60 * 1000; + const dayOfYear = Math.floor( + (date.getTime() - startOfYear.getTime()) / millisecondsPerDay + ); + const weekNumber = Math.ceil((dayOfYear + startOfYear.getDay() + 1) / 7); + + return weekNumber; +} + +export function dateToWeekYear(inputDate: Date): string { + const year = inputDate.getFullYear(); + const weekNumber = getWeekNumber(inputDate); + const formattedString = `S${weekNumber.toString().padStart(2, '0')} ${year}`; + + return formattedString; } export function getLastDayOfMonth(date: Date): Date { From 7e4c10e4a829e7c028ae591bcb67a8a827b460a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Fri, 10 Nov 2023 18:04:33 +0100 Subject: [PATCH 3/3] fix: get the dataset with most hits to get the xValues on distributed lines --- webapp-next/components/charts/line/Line.tsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/webapp-next/components/charts/line/Line.tsx b/webapp-next/components/charts/line/Line.tsx index 0f1b226..1261224 100644 --- a/webapp-next/components/charts/line/Line.tsx +++ b/webapp-next/components/charts/line/Line.tsx @@ -80,7 +80,12 @@ export const ChartLine = (props: Props) => { const min = new Date(filters.start_date); const max = new Date(filters.end_date); - const xValues = datasets[0].hits.map((item: any) => { + + const datasetWithMostHits = datasets.reduce((prev, current) => { + return prev.hits.length > current.hits.length ? prev : current; + }, datasets[0]); + + const xValues = datasetWithMostHits.hits.map((item: any) => { const currentDate = new Date(item.key_as_string); if (currentDate.getTime() < min.getTime()) return dateToWeekYear(min);