2023-06-16 13:49:47 +02:00
|
|
|
import {
|
2023-07-12 22:12:31 +02:00
|
|
|
DateValue,
|
2023-06-16 13:49:47 +02:00
|
|
|
parseAbsoluteToLocal,
|
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 { DatePickerAuxSubProps } from ":/components/Forms/DatePicker/DatePickerAux";
|
|
|
|
|
|
2023-07-12 22:51:32 +02:00
|
|
|
export const parseDateValue = (
|
|
|
|
|
rawDate: string | undefined,
|
|
|
|
|
): undefined | ZonedDateTime => {
|
2023-06-16 13:49:47 +02:00
|
|
|
if (!rawDate) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
try {
|
2023-07-12 22:51:32 +02:00
|
|
|
return parseAbsoluteToLocal(rawDate);
|
2023-06-16 13:49:47 +02:00
|
|
|
} 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
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-12 22:51:32 +02:00
|
|
|
export const parseRangeDateValue = (
|
|
|
|
|
rawRange: [string, string] | undefined,
|
2023-06-16 13:49:47 +02:00
|
|
|
): DateRange | undefined => {
|
|
|
|
|
if (!rawRange || !rawRange[0] || !rawRange[1]) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
return {
|
2023-07-12 22:51:32 +02:00
|
|
|
start: parseDateValue(rawRange[0])!,
|
|
|
|
|
end: parseDateValue(rawRange[1])!,
|
2023-06-16 13:49:47 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
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(
|
2023-07-12 22:51:32 +02:00
|
|
|
"Invalid date format when converting date value on DatePicker component",
|
2023-07-12 22:12:31 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-16 13:49:47 +02:00
|
|
|
export const getDefaultPickerOptions = (props: DatePickerAuxSubProps): any => ({
|
2023-07-12 22:51:32 +02:00
|
|
|
minValue: parseDateValue(props.minValue),
|
|
|
|
|
maxValue: parseDateValue(props.maxValue),
|
2023-06-16 13:49:47 +02:00
|
|
|
shouldCloseOnSelect: true,
|
|
|
|
|
granularity: "day",
|
|
|
|
|
isDisabled: props.disabled,
|
|
|
|
|
label: props.label,
|
|
|
|
|
});
|