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-07-24 22:02:55 +02:00
|
|
|
parseAbsolute,
|
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-24 22:02:55 +02:00
|
|
|
export const isValidTimeZone = (timezone: string) => {
|
|
|
|
|
try {
|
|
|
|
|
// Check if Intl is available and supports time zones
|
|
|
|
|
if (!Intl || !Intl.DateTimeFormat().resolvedOptions().timeZone) {
|
|
|
|
|
throw new Error("Time zones are not available in this environment");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test if the provided time zone is valid
|
|
|
|
|
Intl.DateTimeFormat(undefined, { timeZone: timezone });
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// If an error occurs, it could be due to an invalid time zone or lack of Intl support
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-12 22:51:32 +02:00
|
|
|
export const parseDateValue = (
|
|
|
|
|
rawDate: string | undefined,
|
2023-07-24 22:02:55 +02:00
|
|
|
timezone?: string,
|
2023-07-12 22:51:32 +02:00
|
|
|
): undefined | ZonedDateTime => {
|
2023-06-16 13:49:47 +02:00
|
|
|
if (!rawDate) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
try {
|
2023-07-24 22:02:55 +02:00
|
|
|
if (timezone && !isValidTimeZone(timezone)) {
|
|
|
|
|
throw new Error("Invalid timezone provided.");
|
|
|
|
|
}
|
|
|
|
|
return timezone
|
|
|
|
|
? parseAbsolute(rawDate, timezone)
|
|
|
|
|
: parseAbsoluteToLocal(rawDate);
|
2023-06-16 13:49:47 +02:00
|
|
|
} catch (e) {
|
2023-07-24 22:02:55 +02:00
|
|
|
const errorMessage = e instanceof Error ? ": " + e.message : ".";
|
|
|
|
|
throw new Error("Failed to parse date value" + errorMessage);
|
2023-06-16 13:49:47 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-12 22:51:32 +02:00
|
|
|
export const parseRangeDateValue = (
|
|
|
|
|
rawRange: [string, string] | undefined,
|
2023-07-24 22:02:55 +02:00
|
|
|
timezone?: string,
|
2023-06-16 13:49:47 +02:00
|
|
|
): DateRange | undefined => {
|
|
|
|
|
if (!rawRange || !rawRange[0] || !rawRange[1]) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
return {
|
2023-07-24 22:02:55 +02:00
|
|
|
start: parseDateValue(rawRange[0], timezone)!,
|
|
|
|
|
end: parseDateValue(rawRange[1], timezone)!,
|
2023-06-16 13:49:47 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-24 22:02:55 +02:00
|
|
|
export const convertDateValueToString = (
|
|
|
|
|
date: DateValue | null,
|
|
|
|
|
timezone?: string,
|
|
|
|
|
): string => {
|
2023-07-12 22:12:31 +02:00
|
|
|
try {
|
2023-07-24 22:02:55 +02:00
|
|
|
if (!date) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
const localTimezone = timezone || getLocalTimeZone();
|
|
|
|
|
if (!isValidTimeZone(localTimezone)) {
|
|
|
|
|
throw new Error("Invalid timezone provided.");
|
|
|
|
|
}
|
|
|
|
|
return toZoned(date, localTimezone).toAbsoluteString();
|
2023-07-12 22:12:31 +02:00
|
|
|
} catch (e) {
|
2023-07-24 22:02:55 +02:00
|
|
|
const errorMessage = e instanceof Error ? ": " + e.message : ".";
|
|
|
|
|
throw new Error("Failed to convert date value to string" + errorMessage);
|
2023-07-12 22:12:31 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-16 13:49:47 +02:00
|
|
|
export const getDefaultPickerOptions = (props: DatePickerAuxSubProps): any => ({
|
2023-07-24 22:02:55 +02:00
|
|
|
minValue: parseDateValue(props.minValue, props.timezone),
|
|
|
|
|
maxValue: parseDateValue(props.maxValue, props.timezone),
|
2023-06-16 13:49:47 +02:00
|
|
|
shouldCloseOnSelect: true,
|
|
|
|
|
granularity: "day",
|
|
|
|
|
isDisabled: props.disabled,
|
|
|
|
|
label: props.label,
|
|
|
|
|
});
|