From b5c9690780b09b099299f21b688740c59341bb85 Mon Sep 17 00:00:00 2001 From: Guillermo Grau Date: Fri, 26 Aug 2022 10:36:46 +0200 Subject: [PATCH] Added yesterday option in date range selector. --- components/common/DateFilter.js | 4 ++++ lib/date.js | 37 ++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/components/common/DateFilter.js b/components/common/DateFilter.js index 8e50e95a..d568a889 100644 --- a/components/common/DateFilter.js +++ b/components/common/DateFilter.js @@ -18,6 +18,10 @@ export const filterOptions = [ ), value: '24hour', }, + { + label: , + value: '-1day', + }, { label: , value: '1week', diff --git a/lib/date.js b/lib/date.js index 6897df21..cae39ce4 100644 --- a/lib/date.js +++ b/lib/date.js @@ -7,6 +7,8 @@ import { addYears, subHours, subDays, + subMonths, + subYears, startOfMinute, startOfHour, startOfDay, @@ -39,7 +41,7 @@ export function getDateRange(value, locale = 'en-US') { const now = new Date(); const dateLocale = getDateLocale(locale); - const match = value.match(/^(?[0-9]+)(?hour|day|week|month|year)$/); + const match = value.match(/^(?[0-9-]+)(?hour|day|week|month|year)$/); if (!match) return; @@ -78,6 +80,39 @@ export function getDateRange(value, locale = 'en-US') { } } + if (+num === -1) { + switch (unit) { + case 'day': + return { + startDate: subDays(startOfDay(now), 1), + endDate: subDays(endOfDay(now), 1), + unit: 'hour', + value, + }; + case 'week': + return { + startDate: subDays(startOfWeek(now, { locale: dateLocale }), 7), + endDate: subDays(endOfWeek(now, { locale: dateLocale }), 1), + unit: 'day', + value, + }; + case 'month': + return { + startDate: subMonths(startOfMonth(now), 1), + endDate: subMonths(endOfMonth(now), 1), + unit: 'day', + value, + }; + case 'year': + return { + startDate: subYears(startOfYear(now), 1), + endDate: subYears(endOfYear(now), 1), + unit: 'month', + value, + }; + } + } + switch (unit) { case 'day': return {