Skip to content

Commit 2a52691

Browse files
author
Zabilsya
committed
[DOP-22993] rewrite cronService
1 parent 892c232 commit 2a52691

File tree

8 files changed

+73
-81
lines changed

8 files changed

+73
-81
lines changed

src/shared/services/cronService/cronService.ts

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import { getOrdinalNumber } from '@shared/utils';
22

33
import { CRON_VALUE_DEFAULT, DAYS_OF_WEEK } from './constants';
4-
import { CronSegmentKey, CronSegmentValue } from './types';
4+
import { CronSegmentKey, CronSegmentValue, Period } from './types';
55

66
/** Class for convenient handling cron settings */
77
export class CronService {
88
private initialValueLength = 5;
99

10+
private period: Period;
11+
1012
private value: Map<CronSegmentKey, CronSegmentValue>;
1113

1214
constructor(initialValue?: string) {
1315
this.value = this.transformInitialValueToMap(initialValue);
16+
this.period = this.initPeriod();
1417
}
1518

1619
private transformInitialValueToMap(initialValue?: string) {
@@ -35,6 +38,20 @@ export class CronService {
3538
]);
3639
}
3740

41+
private initPeriod() {
42+
if (this.getMonthDay() === null && this.getWeekDay() === null) {
43+
return Period.DAY;
44+
}
45+
if (this.getMonthDay()) {
46+
return Period.MONTH;
47+
}
48+
return Period.WEEK;
49+
}
50+
51+
getPeriod() {
52+
return this.period;
53+
}
54+
3855
getMinute(): number {
3956
return this.value.get('minute')!;
4057
}
@@ -43,6 +60,10 @@ export class CronService {
4360
return this.value.get('hour')!;
4461
}
4562

63+
getTime() {
64+
return `${this.getHour()}:${this.getMinute()}`;
65+
}
66+
4667
getMonthDay(): CronSegmentValue {
4768
return this.value.get('date') ?? null;
4869
}
@@ -51,24 +72,40 @@ export class CronService {
5172
return this.value.get('day') ?? null;
5273
}
5374

54-
getTime() {
55-
return `${this.getHour()}:${this.getMinute()}`;
75+
setPeriod(period: Period) {
76+
this.period = period;
77+
switch (period) {
78+
case Period.DAY:
79+
this.setMonthDay(null);
80+
this.setWeekDay(null);
81+
break;
82+
case Period.WEEK:
83+
this.setWeekDay(new Date().getDay());
84+
this.setMonthDay(null);
85+
break;
86+
case Period.MONTH:
87+
this.setWeekDay(null);
88+
this.setMonthDay(new Date().getDate());
89+
}
5690
}
5791

5892
setMinute(value: number) {
5993
if (value < 0 || value > 59) {
60-
this.value.set('minute', new Date().getMinutes());
61-
} else {
62-
this.value.set('minute', value);
94+
throw new Error('Invalid value');
6395
}
96+
this.value.set('minute', value);
6497
}
6598

6699
setHour(value: number) {
67100
if (value < 0 || value > 23) {
68-
this.value.set('hour', new Date().getHours());
69-
} else {
70-
this.value.set('hour', value);
101+
throw new Error('Invalid value');
71102
}
103+
this.value.set('hour', value);
104+
}
105+
106+
setTime(hour?: number, minute?: number) {
107+
this.setHour(hour ?? new Date().getHours());
108+
this.setMinute(minute ?? new Date().getMinutes());
72109
}
73110

74111
setMonthDay(value: CronSegmentValue) {
@@ -77,11 +114,9 @@ export class CronService {
77114
return;
78115
}
79116
if (value < 1 || value > 31) {
80-
this.value.set('date', new Date().getDate());
81-
} else {
82-
this.value.set('date', value);
117+
throw new Error('Invalid value');
83118
}
84-
this.setWeekDay(null);
119+
this.value.set('date', value);
85120
}
86121

87122
setWeekDay(value: CronSegmentValue) {
@@ -90,11 +125,9 @@ export class CronService {
90125
return;
91126
}
92127
if (value < 0 || value > 6) {
93-
this.value.set('day', new Date().getDay());
94-
} else {
95-
this.value.set('day', value);
128+
throw new Error('Invalid value');
96129
}
97-
this.setMonthDay(null);
130+
this.value.set('day', value);
98131
}
99132

100133
toString() {
@@ -110,15 +143,7 @@ export class CronService {
110143
const day = this.getWeekDay();
111144
const date = this.getMonthDay();
112145

113-
let schedule = 'Every ';
114-
115-
if (day === null && !date) {
116-
schedule += 'day ';
117-
} else if (day !== null && !date) {
118-
schedule += 'week ';
119-
} else {
120-
schedule += 'month ';
121-
}
146+
let schedule = `Every ${this.period} `;
122147

123148
if (day !== null) {
124149
schedule += `on ${DAYS_OF_WEEK[day]} `;

src/shared/services/cronService/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ export type CronSegmentValue = number | null;
22

33
export type CronSegmentKey = 'date' | 'day' | 'hour' | 'minute';
44

5+
export enum Period {
6+
DAY = 'day',
7+
WEEK = 'week',
8+
MONTH = 'month',
9+
}
10+
511
export enum DayOfWeek {
612
SUNDAY,
713
MONDAY,

src/shared/ui/CronSelect/components/DynamicSelect/index.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
import React from 'react';
22
import { Select } from 'antd';
33
import { getOrdinalNumber } from '@shared/utils';
4+
import { Period } from '@shared/services';
45

56
import classes from '../../styles.module.less';
67
import { DAYS_OF_MONTH_SELECT_OPTIONS, DAYS_OF_WEEK_SELECT_OPTIONS } from '../../constants';
7-
import { Period } from '../../types';
88

99
import { DynamicSelectProps } from './types';
1010

11-
export const DynamicSelect = ({
12-
periodSelectValue,
13-
weekDay,
14-
monthDay,
15-
onChangeWeekDay,
16-
onChangeMonthDay,
17-
}: DynamicSelectProps) => {
18-
switch (periodSelectValue) {
11+
export const DynamicSelect = ({ period, weekDay, monthDay, onChangeWeekDay, onChangeMonthDay }: DynamicSelectProps) => {
12+
switch (period) {
1913
case Period.WEEK:
2014
return (
2115
<Select

src/shared/ui/CronSelect/components/DynamicSelect/types.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { CronSegmentValue } from '@shared/services';
2-
3-
import { Period } from '../../types';
1+
import { CronSegmentValue, Period } from '@shared/services';
42

53
/** Interface as Props for component "DynamicSelect" */
64
export interface DynamicSelectProps {
75
/** Value of period Select */
8-
periodSelectValue: Period;
6+
period: Period;
97
/** Value of week day Select */
108
weekDay: CronSegmentValue;
119
/** Value of month day Select */

src/shared/ui/CronSelect/constants.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { DayOfWeek, DayOfWeekName } from '@shared/services';
1+
import { DayOfWeek, DayOfWeekName, Period } from '@shared/services';
22
import { prepareOptionsForSelect } from '@shared/ui';
33

4-
import { Period } from './types';
5-
64
export const PERIOD_SELECT_OPTIONS = prepareOptionsForSelect({
75
data: Object.values(Period),
86
renderLabel: (data) => data,

src/shared/ui/CronSelect/hooks/useCron/useCron.ts

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { CronSegmentValue, CronService } from '@shared/services';
1+
import { CronSegmentValue, CronService, Period } from '@shared/services';
22
import dayjs, { Dayjs } from 'dayjs';
33

4-
import { Period } from '../../types';
5-
64
import { UseCronProps } from './types';
75

86
/** Hook for handling value of CronSelect component */
@@ -12,18 +10,7 @@ export const useCron = ({ value, onChange = () => undefined }: UseCronProps) =>
1210
const handleChange = () => onChange(cronService.toString());
1311

1412
const handleChangePeriod = (period: Period) => {
15-
switch (period) {
16-
case Period.DAY:
17-
cronService.setMonthDay(null);
18-
cronService.setWeekDay(null);
19-
break;
20-
case Period.WEEK:
21-
cronService.setWeekDay(dayjs().day());
22-
break;
23-
case Period.MONTH:
24-
cronService.setMonthDay(dayjs().date());
25-
break;
26-
}
13+
cronService.setPeriod(period);
2714
handleChange();
2815
};
2916

@@ -38,23 +25,12 @@ export const useCron = ({ value, onChange = () => undefined }: UseCronProps) =>
3825
};
3926

4027
const handleChangeTime = (time: Dayjs | null) => {
41-
cronService.setMinute(time?.minute() ?? dayjs().minute());
42-
cronService.setHour(time?.hour() ?? dayjs().hour());
28+
cronService.setTime(time?.hour(), time?.minute());
4329
handleChange();
4430
};
4531

46-
const getPeriodSelectValue = () => {
47-
if (cronService.getMonthDay() === null && cronService.getWeekDay() === null) {
48-
return Period.DAY;
49-
}
50-
if (cronService.getMonthDay()) {
51-
return Period.MONTH;
52-
}
53-
return Period.WEEK;
54-
};
55-
5632
return {
57-
periodSelectValue: getPeriodSelectValue(),
33+
period: cronService.getPeriod(),
5834
weekDay: cronService.getWeekDay(),
5935
monthDay: cronService.getMonthDay(),
6036
time: dayjs(cronService.getTime(), 'HH:mm'),

src/shared/ui/CronSelect/index.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import React, { memo } from 'react';
22
import { Select } from 'antd';
33
import { TimePicker } from '@shared/ui';
4+
import { Period } from '@shared/services';
45

56
import { PERIOD_SELECT_OPTIONS } from './constants';
6-
import { CronSelectProps, Period } from './types';
7+
import { CronSelectProps } from './types';
78
import classes from './styles.module.less';
89
import { useCron } from './hooks';
910
import { DynamicSelect } from './components';
1011

1112
export const CronSelect = memo(({ value, onChange }: CronSelectProps) => {
1213
const {
13-
periodSelectValue,
14+
period,
1415
weekDay,
1516
monthDay,
1617
time,
@@ -28,16 +29,16 @@ export const CronSelect = memo(({ value, onChange }: CronSelectProps) => {
2829
className={classes.period}
2930
size="large"
3031
onChange={handleChangePeriod}
31-
value={periodSelectValue}
32+
value={period}
3233
options={PERIOD_SELECT_OPTIONS}
3334
/>
3435
</div>
3536

36-
{periodSelectValue !== Period.DAY && (
37+
{period !== Period.DAY && (
3738
<div className={classes.segment}>
3839
<span>On:</span>
3940
<DynamicSelect
40-
periodSelectValue={periodSelectValue}
41+
period={period}
4142
weekDay={weekDay}
4243
monthDay={monthDay}
4344
onChangeWeekDay={handleChangeWeekDay}

src/shared/ui/CronSelect/types.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
import { UseCronProps } from './hooks';
22

33
export interface CronSelectProps extends UseCronProps {}
4-
5-
export enum Period {
6-
DAY = 'day',
7-
WEEK = 'week',
8-
MONTH = 'month',
9-
}

0 commit comments

Comments
 (0)