DocSpace-client/packages/components/cron/Cron.tsx

140 lines
3.5 KiB
TypeScript
Raw Normal View History

2023-09-07 11:40:45 +00:00
import { useTranslation } from "react-i18next";
2023-08-16 12:05:37 +00:00
import React, { useState, useEffect, useRef, useMemo } from "react";
import { MonthDays, Months, Period, WeekDays, Hours, Minutes } from "./Field";
import { getCronStringFromValues, stringToArray } from "./part";
import { defaultCronString, defaultPeriod } from "./constants";
2023-09-07 11:40:45 +00:00
import { getPeriodFromCronParts, getUnits } from "./util";
2023-08-16 12:05:37 +00:00
2023-08-17 09:46:09 +00:00
import { CronWrapper, Suffix } from "./Cron.styled";
2023-08-16 12:05:37 +00:00
2023-09-07 11:40:45 +00:00
import type CronProps from "./Cron.props";
import type { PeriodType } from "./types";
2023-08-16 12:05:37 +00:00
function Cron({ value = defaultCronString, setValue, onError }: CronProps) {
2023-09-07 11:40:45 +00:00
const { t } = useTranslation("Common");
2023-08-16 12:05:37 +00:00
const valueRef = useRef<string>(value);
const [period, setPeriod] = useState<PeriodType>(defaultPeriod);
const [hours, setHours] = useState<number[]>([]);
const [months, setMonths] = useState<number[]>([]);
const [minutes, setMinutes] = useState<number[]>([]);
const [weekDays, setWeekDays] = useState<number[]>([]);
const [monthDays, setMonthDays] = useState<number[]>([]);
useEffect(() => {
2023-08-18 07:45:50 +00:00
onError?.(undefined); // reset error state
2023-08-16 12:05:37 +00:00
if (valueRef.current !== value) init();
}, [value]);
useEffect(() => {
try {
const cornString = getCronStringFromValues(
period,
months,
monthDays,
weekDays,
hours,
minutes
);
setValue(cornString);
valueRef.current = cornString;
onError?.(undefined);
} catch (error) {
onError?.(error);
}
}, [period, hours, months, minutes, weekDays, monthDays]);
useEffect(() => {
init();
}, []);
const init = () => {
try {
const cronParts = stringToArray(value);
const period = getPeriodFromCronParts(cronParts);
const [minutes, hours, monthDays, months, weekDays] = cronParts;
setMinutes(minutes);
setHours(hours);
setMonthDays(monthDays);
setMonths(months);
setWeekDays(weekDays);
setPeriod(period);
} catch (error) {
console.log(error);
onError?.(error);
}
};
const { isYear, isMonth, isWeek, isHour, isMinute } = useMemo(() => {
2023-08-16 13:11:13 +00:00
const isYear = period === "Year";
const isMonth = period === "Month";
const isWeek = period === "Week";
const isHour = period === "Hour";
const isMinute = period == "Minute";
2023-08-16 12:05:37 +00:00
return {
isYear,
isMonth,
isWeek,
isHour,
isMinute,
};
}, [period]);
const units = useMemo(() => getUnits(t), [t]);
2023-09-07 11:40:45 +00:00
2023-08-16 12:05:37 +00:00
return (
<CronWrapper>
2023-09-07 11:40:45 +00:00
<Period t={t} period={period} setPeriod={setPeriod} />
{isYear && (
<Months unit={units[3]} t={t} months={months} setMonths={setMonths} />
)}
2023-08-16 12:05:37 +00:00
{(isYear || isMonth) && (
<MonthDays
2023-09-07 11:40:45 +00:00
t={t}
unit={units[2]}
2023-08-16 12:05:37 +00:00
weekDays={weekDays}
monthDays={monthDays}
setMonthDays={setMonthDays}
/>
)}
{(isYear || isMonth || isWeek) && (
<WeekDays
2023-09-07 11:40:45 +00:00
t={t}
unit={units[4]}
2023-08-16 12:05:37 +00:00
isWeek={isWeek}
period={period}
monthDays={monthDays}
weekDays={weekDays}
setWeekDays={setWeekDays}
/>
)}
2023-09-07 11:40:45 +00:00
{!isHour && !isMinute && (
<Hours unit={units[1]} t={t} hours={hours} setHours={setHours} />
)}
2023-08-16 12:05:37 +00:00
{!isMinute && (
2023-09-07 11:40:45 +00:00
<Minutes
t={t}
unit={units[0]}
period={period}
minutes={minutes}
setMinutes={setMinutes}
/>
2023-08-16 12:05:37 +00:00
)}
2023-08-17 09:46:09 +00:00
<Suffix>UTC</Suffix>
2023-08-16 12:05:37 +00:00
</CronWrapper>
);
}
export default Cron;