From 402ffce8954718030263d6d0374837cc0eb04493 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Fri, 6 Feb 2026 16:22:52 +0530 Subject: [PATCH] feat: add this month and this week to date picker --- .../components/ui/DatePicker/DatePicker.vue | 58 ++++++++++++++- .../components/CalendarDateRange.vue | 27 +++---- .../components/DatePickerButton.vue | 72 ++++++++++++++----- .../ui/DatePicker/helpers/DatePickerHelper.js | 72 ++++++++++++++++++- .../dashboard/i18n/locale/en/datePicker.json | 5 ++ 5 files changed, 200 insertions(+), 34 deletions(-) diff --git a/app/javascript/dashboard/components/ui/DatePicker/DatePicker.vue b/app/javascript/dashboard/components/ui/DatePicker/DatePicker.vue index 3ec7ceace..5392641bd 100644 --- a/app/javascript/dashboard/components/ui/DatePicker/DatePicker.vue +++ b/app/javascript/dashboard/components/ui/DatePicker/DatePicker.vue @@ -1,11 +1,14 @@ diff --git a/app/javascript/dashboard/components/ui/DatePicker/helpers/DatePickerHelper.js b/app/javascript/dashboard/components/ui/DatePicker/helpers/DatePickerHelper.js index 85e41603f..0fbbb2bc4 100644 --- a/app/javascript/dashboard/components/ui/DatePicker/helpers/DatePickerHelper.js +++ b/app/javascript/dashboard/components/ui/DatePicker/helpers/DatePickerHelper.js @@ -10,6 +10,8 @@ import { isSameMonth, format, startOfWeek, + endOfWeek, + addWeeks, addDays, eachDayOfInterval, endOfMonth, @@ -40,7 +42,20 @@ export const dateRanges = [ value: 'last6months', }, { label: 'DATE_PICKER.DATE_RANGE_OPTIONS.LAST_YEAR', value: 'lastYear' }, - { label: 'DATE_PICKER.DATE_RANGE_OPTIONS.CUSTOM_RANGE', value: 'custom' }, + { + label: 'DATE_PICKER.DATE_RANGE_OPTIONS.THIS_WEEK', + value: 'thisWeek', + separator: true, + }, + { + label: 'DATE_PICKER.DATE_RANGE_OPTIONS.MONTH_TO_DATE', + value: 'monthToDate', + }, + { + label: 'DATE_PICKER.DATE_RANGE_OPTIONS.CUSTOM_RANGE', + value: 'custom', + separator: true, + }, ]; export const DATE_RANGE_TYPES = { @@ -49,6 +64,8 @@ export const DATE_RANGE_TYPES = { LAST_3_MONTHS: 'last3months', LAST_6_MONTHS: 'last6months', LAST_YEAR: 'lastYear', + THIS_WEEK: 'thisWeek', + MONTH_TO_DATE: 'monthToDate', CUSTOM_RANGE: 'custom', }; @@ -210,6 +227,14 @@ export const getActiveDateRange = (range, currentDate) => { start: startOfDay(subMonths(currentDate, 12)), end: endOfDay(currentDate), }), + thisWeek: () => ({ + start: startOfDay(startOfWeek(currentDate, { weekStartsOn: 1 })), + end: endOfDay(currentDate), + }), + monthToDate: () => ({ + start: startOfDay(startOfMonth(currentDate)), + end: endOfDay(currentDate), + }), custom: () => ({ start: currentDate, end: currentDate }), }; @@ -217,3 +242,48 @@ export const getActiveDateRange = (range, currentDate) => { ranges[range] || (() => ({ start: currentDate, end: currentDate })) )(); }; + +export const isNavigableRange = rangeType => + rangeType === DATE_RANGE_TYPES.MONTH_TO_DATE || + rangeType === DATE_RANGE_TYPES.THIS_WEEK; + +const WEEK_START = 1; // Monday + +const getWeekRangeAtOffset = (offset, currentDate) => { + if (offset === 0) { + return { + start: startOfDay(startOfWeek(currentDate, { weekStartsOn: WEEK_START })), + end: endOfDay(currentDate), + }; + } + const targetWeek = addWeeks(currentDate, offset); + return { + start: startOfDay(startOfWeek(targetWeek, { weekStartsOn: WEEK_START })), + end: endOfDay(endOfWeek(targetWeek, { weekStartsOn: WEEK_START })), + }; +}; + +const getMonthRangeAtOffset = (offset, currentDate) => { + if (offset === 0) { + return { + start: startOfDay(startOfMonth(currentDate)), + end: endOfDay(currentDate), + }; + } + const targetMonth = addMonths(currentDate, offset); + return { + start: startOfDay(startOfMonth(targetMonth)), + end: endOfDay(endOfMonth(targetMonth)), + }; +}; + +export const getRangeAtOffset = ( + rangeType, + offset, + currentDate = new Date() +) => { + if (rangeType === DATE_RANGE_TYPES.THIS_WEEK) { + return getWeekRangeAtOffset(offset, currentDate); + } + return getMonthRangeAtOffset(offset, currentDate); +}; diff --git a/app/javascript/dashboard/i18n/locale/en/datePicker.json b/app/javascript/dashboard/i18n/locale/en/datePicker.json index c7ef06880..95d304cc6 100644 --- a/app/javascript/dashboard/i18n/locale/en/datePicker.json +++ b/app/javascript/dashboard/i18n/locale/en/datePicker.json @@ -1,5 +1,8 @@ { "DATE_PICKER": { + "PREVIOUS_PERIOD": "Previous period", + "NEXT_PERIOD": "Next period", + "WEEK_NUMBER": "Week #{weekNumber}", "APPLY_BUTTON": "Apply", "CLEAR_BUTTON": "Clear", "DATE_RANGE_INPUT": { @@ -13,6 +16,8 @@ "LAST_3_MONTHS": "Last 3 months", "LAST_6_MONTHS": "Last 6 months", "LAST_YEAR": "Last year", + "THIS_WEEK": "This week", + "MONTH_TO_DATE": "This month", "CUSTOM_RANGE": "Custom date range" } }