= {
+ render: () => ,
name: "timerangepicker of hours ",
};
-export const TimerangepickerOfHoursWithHistogram: StoryObj = {
- render: () => {
- const [value, setValue] = useState<{
- start: Date | number;
- end: Date | number;
- }>();
- const monday = previousMonday(new Date());
-
- return (
- <>
-
-
-
- Current Value:{JSON.stringify(value)}
-
- >
- );
- },
+function TimerangepickerOfHoursWithKeepSelectedWeekdayComponent() {
+ const [value, setValue] = useState();
+
+ return (
+ <>
+
+
+ Current Value:{JSON.stringify(value)}
+ >
+ );
+}
+
+export const TimerangepickerOfHoursWithKeepSelectedWeekday: StoryObj<
+ typeof TimeRangePicker
+> = {
+ render: () => ,
+ name: "timerangepicker of hours with preserved day",
+};
+
+function TimerangepickerOfHoursWithHistogramComponent() {
+ const [value, setValue] = useState();
+ const monday = previousMonday(new Date());
+
+ return (
+ <>
+
+
+
+ Current Value:{JSON.stringify(value)}
+
+ >
+ );
+}
+export const TimerangepickerOfHoursWithHistogram: StoryObj<
+ typeof TimeRangePicker
+> = {
+ render: () => ,
name: "timerangepicker of hours with histogram",
};
diff --git a/src/components/timeRangePicker/TimeRangePicker.tsx b/src/components/timeRangePicker/TimeRangePicker.tsx
index 63713731..ee31a8b4 100644
--- a/src/components/timeRangePicker/TimeRangePicker.tsx
+++ b/src/components/timeRangePicker/TimeRangePicker.tsx
@@ -4,6 +4,7 @@ import { DatesPicker, DateHourPicker } from "./components";
type Props = Omit, "initTimeRange"> & {
initTimeRange?: Interval;
type?: "Days" | "Hours";
+ keepSelectedWeekday?: boolean;
};
const DEFAULT_TIME_RANGE: Interval = {
@@ -17,6 +18,7 @@ export function TimeRangePicker({
weekCounts,
countsLoading = false,
disabledDays,
+ keepSelectedWeekday = false,
onChange,
onWeekChange,
}: Props) {
@@ -35,6 +37,7 @@ export function TimeRangePicker({
weekCounts={weekCounts}
countsLoading={countsLoading}
disabledDays={disabledDays}
+ keepSelectedWeekday={keepSelectedWeekday}
onChange={onChange}
onWeekChange={onWeekChange}
/>
diff --git a/src/components/timeRangePicker/components/DateHourPicker/DateHourPicker.tsx b/src/components/timeRangePicker/components/DateHourPicker/DateHourPicker.tsx
index c50170ec..8dc72192 100644
--- a/src/components/timeRangePicker/components/DateHourPicker/DateHourPicker.tsx
+++ b/src/components/timeRangePicker/components/DateHourPicker/DateHourPicker.tsx
@@ -15,12 +15,14 @@ import {
getMonday,
getTimeRangeDate,
isSameTimeRange,
+ getNewSelectedDate,
} from "../utils";
import { DateSelector, HourSelector } from "./components";
type Props = DatesPickerProps & {
weekCounts?: DailyCount[];
countsLoading?: boolean;
+ keepSelectedWeekday?: boolean;
onWeekChange?: (monday: Date) => void;
};
@@ -29,11 +31,12 @@ export function DateHourPicker({
weekCounts,
countsLoading = false,
disabledDays,
+ keepSelectedWeekday = false,
onChange,
onWeekChange,
}: Props) {
const [selectedDate, setSelectedDate] = useState(
- getTimeRangeDate(initTimeRange)
+ getTimeRangeDate(initTimeRange, "Hours")
);
const [dateOptions, setDateOptions] = useState(
getDateOptions(getMonday(initTimeRange.start))
@@ -93,6 +96,15 @@ export function DateHourPicker({
if (onWeekChange) {
onWeekChange(newMonday);
}
+ if (keepSelectedWeekday) {
+ const newSeletedDate = getNewSelectedDate(
+ selectedDate,
+ currentMonday,
+ newMonday
+ );
+ setSelectedDate(newSeletedDate);
+ submitDateHour(newSeletedDate, selectedTimeRange);
+ }
};
// Function to update time range options
diff --git a/src/components/timeRangePicker/components/utils.ts b/src/components/timeRangePicker/components/utils.ts
index 340600ca..46e973be 100644
--- a/src/components/timeRangePicker/components/utils.ts
+++ b/src/components/timeRangePicker/components/utils.ts
@@ -9,6 +9,10 @@ import {
addMonths,
addYears,
getHours,
+ differenceInCalendarDays,
+ startOfDay,
+ set,
+ subMilliseconds,
} from "date-fns";
import { RANGE_TYPE, DateOption, TimeRangeOption } from "../types";
import {
@@ -23,8 +27,16 @@ export const getMonday = (date: Date | number): Date => {
return isMonday(currentDate) ? currentDate : previousMonday(currentDate);
};
-export const getTimeRangeDate = (initRange: Interval): Date => {
- return isSameDay(initRange.start, initRange.end)
+export const getTimeRangeDate = (
+ initRange: Interval,
+ timePickerType?: "Days" | "Hours"
+): Date => {
+ // 18-24 time slot technically ends in different day
+ const end =
+ timePickerType === "Hours"
+ ? subMilliseconds(initRange.end, 1)
+ : initRange.end;
+ return isSameDay(initRange.start, end)
? new Date(new Date(initRange.start).setSeconds(0, 0))
: new Date();
};
@@ -139,3 +151,23 @@ export const isSameTimeRange = (
getHours(interval.end) === optionEnd
);
};
+
+export const getNewSelectedDate = (
+ selectedDate: Date,
+ currentMonday: Date,
+ newMonday: Date
+) => {
+ const dayOffset = differenceInCalendarDays(
+ startOfDay(selectedDate),
+ startOfDay(currentMonday)
+ );
+
+ const baseDate = addDays(startOfDay(newMonday), dayOffset);
+
+ return set(baseDate, {
+ hours: selectedDate.getHours(),
+ minutes: selectedDate.getMinutes(),
+ seconds: selectedDate.getSeconds(),
+ milliseconds: selectedDate.getMilliseconds(),
+ });
+};