import React from 'react'; import type { Option } from 'react-select'; import Select from 'react-select'; import type { ICronTriggerConfigProps } from './cronConfig'; import { HOURS, MINUTES, WEEKDAYS } from './cronSelectOptions'; import { SystemTimezone } from '../../../../utils/SystemTimezone'; export interface ICronWeeklyState { weekly: string[]; hour: string; minute: string; } export class CronWeekly extends React.Component { constructor(props: ICronTriggerConfigProps) { super(props); this.state = { weekly: [], hour: '0', minute: '0', }; } public componentDidMount() { const regex = /^0 (\d+) (\d+) \? \* ((?:MON|TUE|WED|THU|FRI|SAT|SUN)(?:,(?:MON|TUE|WED|THU|FRI|SAT|SUN))*) \*$/g; const matches = regex.exec(this.props.trigger.cronExpression); if (matches) { this.setState({ minute: matches[1], hour: matches[2], weekly: matches[3].length > 0 ? matches[3].split(',') : [], }); } else { const { minute, hour, weekly } = this.state; this.props.triggerUpdated({ ...this.props.trigger, cronExpression: '0 ' + minute + ' ' + hour + ' ? *' + (weekly.length > 0 ? ' ' + weekly.join() : '') + ' *', }); } } private onUpdateWeeklyTrigger = (option: Option): void => { const weekly = option.map((o: Option) => o.value); const { hour, minute } = this.state; this.setState({ weekly }); this.props.triggerUpdated({ ...this.props.trigger, cronExpression: '0 ' + minute + ' ' + hour + ' ? *' + (weekly.length > 0 ? ' ' + weekly.join() : '') + ' *', }); }; private onUpdateHourTrigger = (option: Option) => { const hour = option.value; const { minute, weekly } = this.state; this.setState({ hour }); this.props.triggerUpdated({ ...this.props.trigger, cronExpression: '0 ' + minute + ' ' + hour + ' ? *' + (weekly.length > 0 ? ' ' + weekly.join() : '') + ' *', }); }; private onUpdateMinuteTrigger = (option: Option) => { const minute = option.value; const { hour, weekly } = this.state; this.setState({ minute }); this.props.triggerUpdated({ ...this.props.trigger, cronExpression: '0 ' + minute + ' ' + hour + ' ? *' + (weekly.length > 0 ? ' ' + weekly.join() : '') + ' *', }); }; public render() { const { hour, minute, weekly } = this.state; return (
{' '}