From 6ca741b2a3558bd4268d915f6a6f4f27590171a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Madet?= Date: Tue, 10 Dec 2024 18:59:49 +0100 Subject: [PATCH] Added options to the fancy graph: - add today's date or not - use first of month or last of month --- src/statistics/FancyGraphCard.tsx | 25 +---- src/statistics/components/DateRangePicker.tsx | 101 +++++++++++++++--- src/statistics/utils.tsx | 4 + 3 files changed, 94 insertions(+), 36 deletions(-) diff --git a/src/statistics/FancyGraphCard.tsx b/src/statistics/FancyGraphCard.tsx index a240225c..32cda775 100644 --- a/src/statistics/FancyGraphCard.tsx +++ b/src/statistics/FancyGraphCard.tsx @@ -68,27 +68,6 @@ const FancyGraphCard: React.FC = () => { setDateFrom(getFirstOfMonth(dateFromOnPageLoad)); }, []); - useEffect(() => { - if (!dateFrom || !dateTo) return; - - let currentDate = new Date(dateFrom); - const dates = []; - while (currentDate <= dateTo) { - dates.push(currentDate); - currentDate = new Date(currentDate); - currentDate.setDate(currentDate.getDate() + 32); - currentDate.setDate(1); - } - dates.push(currentDate); - - const tomorrow = new Date(); - tomorrow.setDate(tomorrow.getDate() + 1); - dates.push(tomorrow); - - setDates(dates); - setGraphLabels(dates.map((date) => formatDate(date))); - }, [dateFrom, dateTo]); - useEffect(() => { fillCachedData(); buildAndSetGraphData(); @@ -253,12 +232,14 @@ const FancyGraphCard: React.FC = () => {
{gettext("Graph")} {fetching && }
- + string; @@ -9,6 +10,8 @@ interface DateRangePickerProps { setDateFrom: (date: Date) => void; dateTo: Date; setDateTo: (date: Date) => void; + setDates: (dates: Date[]) => void; + setGraphLabels: (graphLabels: string[]) => void; } const DateRangePicker: React.FC = ({ @@ -16,20 +19,94 @@ const DateRangePicker: React.FC = ({ setDateFrom, dateTo, setDateTo, + setDates, + setGraphLabels, }) => { + const [includeToday, setIncludeToday] = useState(true); + const [startOfMonth, setStartOfMonth] = useState(true); + + useEffect(() => { + if (!dateFrom || !dateTo) return; + + let currentDate = new Date(dateFrom); + const dates = []; + while (currentDate <= dateTo) { + dates.push(currentDate); + currentDate = new Date(currentDate); + currentDate.setDate(currentDate.getDate() + (startOfMonth ? 32 : 1)); + currentDate.setDate(1); + currentDate = adaptDate(currentDate); + } + dates.push(currentDate); + + if (includeToday) { + const today = new Date(); + let todayAlreadyInArray = false; + for (const date of dates) { + if ( + date.getDate() === today.getDate() && + date.getMonth() === today.getMonth() && + date.getFullYear() === today.getFullYear() + ) { + todayAlreadyInArray = true; + break; + } + } + if (!todayAlreadyInArray) { + dates.push(today); + } + } + + dates.sort((date1, date2) => date1.getTime() - date2.getTime()); + + setDates(dates); + setGraphLabels(dates.map((date) => formatDate(date))); + }, [dateFrom, dateTo, includeToday, startOfMonth]); + + function adaptDate(date: Date) { + let dateAdapter = getFirstOfMonth; + if (!startOfMonth) { + dateAdapter = getLastOfMonth; + } + return dateAdapter(date); + } + + function getDateInputValue(date: Date) { + if (isNaN(date.getTime())) { + return undefined; + } + return date.toISOString().substring(0, 10); + } + return ( <> + + { + setIncludeToday(e.target.checked); + }} + label={"Include today"} + /> + + + { + setStartOfMonth(event.target.value === "startOfMonth"); + }} + > + + + + { - setDateFrom(getFirstOfMonth(new Date(event.target.value))); + setDateFrom(adaptDate(new Date(event.target.value))); }} /> @@ -38,13 +115,9 @@ const DateRangePicker: React.FC = ({ { - setDateTo(getFirstOfMonth(new Date(event.target.value))); + setDateTo(adaptDate(new Date(event.target.value))); }} /> diff --git a/src/statistics/utils.tsx b/src/statistics/utils.tsx index 2ed6af2b..02fcc07a 100644 --- a/src/statistics/utils.tsx +++ b/src/statistics/utils.tsx @@ -2,3 +2,7 @@ export function getFirstOfMonth(date: Date) { date.setDate(1); return date; } + +export function getLastOfMonth(date: Date) { + return new Date(date.getFullYear(), date.getMonth() + 1, 0); +}