-
Notifications
You must be signed in to change notification settings - Fork 1
[DOP-22993] add transfer schedule #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { CronSegmentKey, CronSegmentValue, DayOfWeekName } from './types'; | ||
|
|
||
| export const CRON_VALUE_DEFAULT = new Map<CronSegmentKey, CronSegmentValue>([ | ||
| ['minute', new Date().getMinutes()], | ||
| ['hour', new Date().getHours()], | ||
| ['date', null], | ||
| ['day', null], | ||
| ]); | ||
|
|
||
| export const DAYS_OF_WEEK = Object.values(DayOfWeekName); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| import { getOrdinalNumber } from '@shared/utils'; | ||
|
|
||
| import { CRON_VALUE_DEFAULT, DAYS_OF_WEEK } from './constants'; | ||
| import { CronSegmentKey, CronSegmentValue, Period } from './types'; | ||
|
|
||
| /** Class for convenient handling cron settings */ | ||
| export class CronService { | ||
| private initialValueLength = 5; | ||
|
|
||
| private period: Period; | ||
|
|
||
| private value: Map<CronSegmentKey, CronSegmentValue>; | ||
|
|
||
| constructor(initialValue?: string) { | ||
| this.value = this.transformInitialValueToMap(initialValue); | ||
| this.period = this.initPeriod(); | ||
| } | ||
|
|
||
| private transformInitialValueToMap(initialValue?: string) { | ||
| const splittedValue = initialValue?.split(' '); | ||
| if (splittedValue?.length !== this.initialValueLength) { | ||
| return CRON_VALUE_DEFAULT; | ||
| } | ||
|
|
||
| const cronValue = splittedValue.map((segment) => { | ||
| const parsedValue = parseInt(segment); | ||
| if (Number.isInteger(parsedValue)) { | ||
| return parsedValue; | ||
| } | ||
| return null; | ||
| }); | ||
|
|
||
| return new Map<CronSegmentKey, CronSegmentValue>([ | ||
| ['minute', cronValue[0]], | ||
| ['hour', cronValue[1]], | ||
| ['date', cronValue[2]], | ||
| ['day', cronValue[4]], | ||
| ]); | ||
| } | ||
|
|
||
| private initPeriod() { | ||
| if (this.getMonthDay() === null && this.getWeekDay() === null) { | ||
| return Period.DAY; | ||
| } | ||
| if (this.getMonthDay()) { | ||
| return Period.MONTH; | ||
| } | ||
| return Period.WEEK; | ||
| } | ||
|
|
||
| getPeriod() { | ||
| return this.period; | ||
| } | ||
|
|
||
| getMinute(): number { | ||
| return this.value.get('minute')!; | ||
| } | ||
|
|
||
| getHour(): number { | ||
| return this.value.get('hour')!; | ||
| } | ||
|
|
||
| getTime() { | ||
| return `${this.getHour()}:${this.getMinute()}`; | ||
| } | ||
|
|
||
| getMonthDay(): CronSegmentValue { | ||
| return this.value.get('date') ?? null; | ||
| } | ||
|
|
||
| getWeekDay(): CronSegmentValue { | ||
| return this.value.get('day') ?? null; | ||
| } | ||
|
|
||
| setPeriod(period: Period) { | ||
| this.period = period; | ||
| switch (period) { | ||
| case Period.DAY: | ||
| this.setMonthDay(null); | ||
| this.setWeekDay(null); | ||
| break; | ||
| case Period.WEEK: | ||
| this.setWeekDay(new Date().getDay()); | ||
| this.setMonthDay(null); | ||
| break; | ||
| case Period.MONTH: | ||
| this.setWeekDay(null); | ||
| this.setMonthDay(new Date().getDate()); | ||
| } | ||
| } | ||
|
|
||
| setMinute(value: number) { | ||
| if (value < 0 || value > 59) { | ||
| throw new Error('Invalid value'); | ||
| } | ||
| this.value.set('minute', value); | ||
| } | ||
|
|
||
| setHour(value: number) { | ||
| if (value < 0 || value > 23) { | ||
| throw new Error('Invalid value'); | ||
| } | ||
| this.value.set('hour', value); | ||
| } | ||
|
|
||
| setTime(hour?: number, minute?: number) { | ||
| this.setHour(hour ?? new Date().getHours()); | ||
| this.setMinute(minute ?? new Date().getMinutes()); | ||
| } | ||
|
|
||
| setMonthDay(value: CronSegmentValue) { | ||
| if (value === null) { | ||
| this.value.set('date', null); | ||
| return; | ||
| } | ||
| if (value < 1 || value > 31) { | ||
jellySWATy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new Error('Invalid value'); | ||
| } | ||
| this.value.set('date', value); | ||
| } | ||
|
|
||
| setWeekDay(value: CronSegmentValue) { | ||
| if (value === null) { | ||
| this.value.set('day', null); | ||
| return; | ||
| } | ||
| if (value < 0 || value > 6) { | ||
jellySWATy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new Error('Invalid value'); | ||
| } | ||
| this.value.set('day', value); | ||
| } | ||
|
|
||
| toString() { | ||
| const minute = this.getMinute(); | ||
| const hour = this.getHour(); | ||
| const date = this.getMonthDay() ?? '*'; | ||
| const day = this.getWeekDay() ?? '*'; | ||
| return `${minute} ${hour} ${date} * ${day}`; | ||
| } | ||
|
|
||
| getSchedule() { | ||
| const time = this.getTime(); | ||
| const day = this.getWeekDay(); | ||
| const date = this.getMonthDay(); | ||
|
|
||
| let schedule = `Every ${this.period} `; | ||
|
|
||
| if (day !== null) { | ||
| schedule += `on ${DAYS_OF_WEEK[day]} `; | ||
| } else if (date) { | ||
| schedule += `${getOrdinalNumber(date)} `; | ||
| } | ||
|
|
||
| schedule += `at ${time}`; | ||
|
|
||
| return schedule; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from './cronService'; | ||
| export * from './types'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| export type CronSegmentValue = number | null; | ||
|
|
||
| export type CronSegmentKey = 'date' | 'day' | 'hour' | 'minute'; | ||
|
|
||
| export enum Period { | ||
| DAY = 'day', | ||
| WEEK = 'week', | ||
| MONTH = 'month', | ||
| } | ||
|
|
||
| export enum DayOfWeek { | ||
| SUNDAY, | ||
| MONDAY, | ||
| TUESDAY, | ||
| WEDNESDAY, | ||
| THURSDAY, | ||
| FRIDAY, | ||
| SATURDAY, | ||
| } | ||
|
|
||
| export enum DayOfWeekName { | ||
| SUNDAY = 'Sunday', | ||
| MONDAY = 'Monday', | ||
| TUESDAY = 'Tuesday', | ||
| WEDNESDAY = 'Wednesday', | ||
| THURSDAY = 'Thursday', | ||
| FRIDAY = 'Friday', | ||
| SATURDAY = 'Saturday', | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './cronService'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { Dayjs } from 'dayjs'; | ||
| import dayjsGenerateConfig from 'rc-picker/lib/generate/dayjs'; | ||
| import generateCalendar from 'antd/es/calendar/generateCalendar'; | ||
|
|
||
| export const Calendar = generateCalendar<Dayjs>(dayjsGenerateConfig); |
39 changes: 39 additions & 0 deletions
39
src/shared/ui/CronSelect/components/DynamicSelect/index.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import React from 'react'; | ||
| import { Select } from 'antd'; | ||
| import { getOrdinalNumber } from '@shared/utils'; | ||
| import { Period } from '@shared/services'; | ||
|
|
||
| import classes from '../../styles.module.less'; | ||
| import { DAYS_OF_MONTH_SELECT_OPTIONS, DAYS_OF_WEEK_SELECT_OPTIONS } from '../../constants'; | ||
|
|
||
| import { DynamicSelectProps } from './types'; | ||
|
|
||
| export const DynamicSelect = ({ period, weekDay, monthDay, onChangeWeekDay, onChangeMonthDay }: DynamicSelectProps) => { | ||
| switch (period) { | ||
| case Period.WEEK: | ||
| return ( | ||
| <Select | ||
| className={classes.day} | ||
| size="large" | ||
| onChange={onChangeWeekDay} | ||
| options={DAYS_OF_WEEK_SELECT_OPTIONS} | ||
| value={weekDay} | ||
| /> | ||
| ); | ||
| case Period.MONTH: | ||
| return ( | ||
| <div className={classes.month}> | ||
| <Select | ||
| className={classes.date} | ||
| size="large" | ||
| onChange={onChangeMonthDay} | ||
| options={DAYS_OF_MONTH_SELECT_OPTIONS} | ||
| value={monthDay} | ||
| /> | ||
| <span>{getOrdinalNumber(monthDay!, true)}</span> | ||
| </div> | ||
| ); | ||
| default: | ||
| return null; | ||
| } | ||
| }; |
15 changes: 15 additions & 0 deletions
15
src/shared/ui/CronSelect/components/DynamicSelect/types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { CronSegmentValue, Period } from '@shared/services'; | ||
|
|
||
| /** Interface as Props for component "DynamicSelect" */ | ||
| export interface DynamicSelectProps { | ||
| /** Value of period Select */ | ||
| period: Period; | ||
| /** Value of week day Select */ | ||
| weekDay: CronSegmentValue; | ||
| /** Value of month day Select */ | ||
| monthDay: CronSegmentValue; | ||
| /** Callback for changing value of week day Select */ | ||
| onChangeWeekDay: (value: CronSegmentValue) => void; | ||
| /** Callback for changing value of month day Select */ | ||
| onChangeMonthDay: (value: CronSegmentValue) => void; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './DynamicSelect'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { DayOfWeek, DayOfWeekName, Period } from '@shared/services'; | ||
| import { prepareOptionsForSelect } from '@shared/ui'; | ||
|
|
||
| export const PERIOD_SELECT_OPTIONS = prepareOptionsForSelect({ | ||
| data: Object.values(Period), | ||
| renderLabel: (data) => data, | ||
| renderValue: (data) => data, | ||
| }); | ||
|
|
||
| export const DAYS_OF_WEEK_SELECT_OPTIONS = prepareOptionsForSelect({ | ||
| data: [ | ||
| { value: DayOfWeek.MONDAY, label: DayOfWeekName.MONDAY }, | ||
| { value: DayOfWeek.TUESDAY, label: DayOfWeekName.TUESDAY }, | ||
| { value: DayOfWeek.WEDNESDAY, label: DayOfWeekName.WEDNESDAY }, | ||
| { value: DayOfWeek.THURSDAY, label: DayOfWeekName.THURSDAY }, | ||
| { value: DayOfWeek.FRIDAY, label: DayOfWeekName.FRIDAY }, | ||
| { value: DayOfWeek.SATURDAY, label: DayOfWeekName.SATURDAY }, | ||
| { value: DayOfWeek.SUNDAY, label: DayOfWeekName.SUNDAY }, | ||
| ], | ||
| renderLabel: (data) => data.label, | ||
| renderValue: (data) => data.value, | ||
| }); | ||
|
|
||
| export const DAYS_OF_MONTH_SELECT_OPTIONS = prepareOptionsForSelect({ | ||
| data: Array.from({ length: 31 }, (_, index) => index + 1), | ||
| renderLabel: (data) => data, | ||
| renderValue: (data) => data, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './useCron'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from './useCron'; | ||
| export * from './types'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| /** Interface as Props for hook "useCron" */ | ||
| export interface UseCronProps { | ||
| /** Value of cron expression like "* * * * *" */ | ||
| value?: string; | ||
| /** Callback for changing value of cron expression */ | ||
| onChange?: (value: string) => void; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.