2023-06-16 13:49:47 +02:00
|
|
|
import {
|
|
|
|
|
CalendarDate,
|
2023-07-12 22:12:31 +02:00
|
|
|
DateValue,
|
2023-06-16 13:49:47 +02:00
|
|
|
parseAbsoluteToLocal,
|
|
|
|
|
toCalendarDate,
|
2023-07-12 22:12:31 +02:00
|
|
|
ZonedDateTime,
|
|
|
|
|
toZoned,
|
|
|
|
|
getLocalTimeZone,
|
2023-06-16 13:49:47 +02:00
|
|
|
} from "@internationalized/date";
|
|
|
|
|
import { DateRange } from "react-aria";
|
|
|
|
|
import {
|
|
|
|
|
StringOrDate,
|
|
|
|
|
StringsOrDateRange,
|
|
|
|
|
} from ":/components/Forms/DatePicker/types";
|
|
|
|
|
import { DatePickerAuxSubProps } from ":/components/Forms/DatePicker/DatePickerAux";
|
|
|
|
|
|
|
|
|
|
export const parseCalendarDate = (
|
2023-07-18 15:43:56 +02:00
|
|
|
rawDate: StringOrDate | undefined,
|
2023-06-16 13:49:47 +02:00
|
|
|
): undefined | CalendarDate => {
|
|
|
|
|
if (!rawDate) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
const ISODateString = new Date(rawDate).toISOString();
|
|
|
|
|
return toCalendarDate(parseAbsoluteToLocal(ISODateString));
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw new Error(
|
2023-07-18 15:43:56 +02:00
|
|
|
"Invalid date format when initializing props on DatePicker component",
|
2023-06-16 13:49:47 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const parseRangeCalendarDate = (
|
2023-07-18 15:43:56 +02:00
|
|
|
rawRange: StringsOrDateRange | undefined,
|
2023-06-16 13:49:47 +02:00
|
|
|
): DateRange | undefined => {
|
|
|
|
|
if (!rawRange || !rawRange[0] || !rawRange[1]) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
start: parseCalendarDate(rawRange[0])!,
|
|
|
|
|
end: parseCalendarDate(rawRange[1])!,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-12 22:12:31 +02:00
|
|
|
export const convertDateValueToString = (date: DateValue | null): string => {
|
|
|
|
|
try {
|
|
|
|
|
const localTimezone = getLocalTimeZone();
|
|
|
|
|
// If timezone is already set, it would be kept, else the selection is set at midnight
|
|
|
|
|
// on the local timezone, then converted to a UTC offset.
|
|
|
|
|
return date ? toZoned(date, localTimezone).toAbsoluteString() : "";
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
"Invalid date format when converting date value on DatePicker component"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-16 13:49:47 +02:00
|
|
|
export const getDefaultPickerOptions = (props: DatePickerAuxSubProps): any => ({
|
|
|
|
|
minValue: parseCalendarDate(props.minValue),
|
|
|
|
|
maxValue: parseCalendarDate(props.maxValue),
|
|
|
|
|
shouldCloseOnSelect: true,
|
|
|
|
|
granularity: "day",
|
|
|
|
|
isDisabled: props.disabled,
|
|
|
|
|
label: props.label,
|
|
|
|
|
});
|