Skip to content

Commit

Permalink
Merge pull request #123 from shoonia/refactoring-cron-errors
Browse files Browse the repository at this point in the history
Refactoring cron errors
  • Loading branch information
shoonia authored Apr 1, 2024
2 parents 2937e0f + ab417e1 commit b7eef8c
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 70 deletions.
14 changes: 0 additions & 14 deletions jest.config.js

This file was deleted.

18 changes: 0 additions & 18 deletions src/components/CronTrue/index.tsx

This file was deleted.

15 changes: 6 additions & 9 deletions src/components/Jobs/Cron.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,23 @@ import { useEffect, useRef } from 'preact/hooks';
import s from './styles.css';
import { Label } from './Label';
import { KEYS } from '../../constants';
import { useFormScope } from '../../hooks/formScope';

interface Props {
value: string;
error: boolean;
}

export const Cron: FC<Props> = ({ value, error }) => {
export const Cron: FC = () => {
const { cronExpression, cronError = '' } = useFormScope();
const ref = useRef<HTMLInputElement>(null);

useEffect(() => {
ref.current?.setCustomValidity(error ? 'error': '');
}, [error]);
ref.current?.setCustomValidity(cronError);
}, [cronError]);

return (
<Label top="Cron Expression">
<input
ref={ref}
type="text"
className={s.mono}
value={value}
value={cronExpression}
data-name={KEYS.cronExpression}
spellcheck={false}
list="cron-examples"
Expand Down
16 changes: 16 additions & 0 deletions src/components/Jobs/CronTrue.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@import url("../../styles/vars.css");

.message {
white-space: pre-wrap;
line-height: 1.5em;
margin-bottom: 1em;
padding: 1em;
border: 1px solid $color-focus;
border-radius: 6px;
}

.error {
color: $color-danger-dark;
border-color: $color-danger-dark;
background-color: $color-danger-light;
}
27 changes: 27 additions & 0 deletions src/components/Jobs/CronTrue.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useEffect } from 'preact/hooks';

import s from './CronTrue.css';
import { parseCron } from '../../util/parseCron';
import { classNames } from '../../util/component';
import { useFormScope } from '../../hooks/formScope';
import { useDispatch } from '../../store';

export const CronTrue: FC = () => {
const dispatch = useDispatch();
const { id, cronExpression } = useFormScope();
const [isError, message] = parseCron(cronExpression);

useEffect(() => {
dispatch('items/update', {
id,
name: 'cronError',
value: isError ? message : '',
});
}, [isError]);

return (
<div className={classNames([s.message, isError && s.error ])}>
{message}
</div>
);
};
14 changes: 4 additions & 10 deletions src/components/Jobs/ExecutionConfig.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
import { useState } from 'preact/hooks';

import s from './styles.css';
import { CronTrue } from '../CronTrue';
import { CronTrue } from './CronTrue';
import { Cron } from './Cron';
import { Time } from './Time';
import { CronExamples } from './CronExamples';
import { DayOfWeek } from './DayOfWeek';
import { DateInMonth } from './DateInMonth';
import { PERIOD } from '../../constants';
import { classNames } from '../../util/component';
import { useFormScope } from '../../hooks/formScope';

export const ExecutionConfig: FC = () => {
const [isError, setValidity] = useState<boolean>(false);
const { period, cronExpression } = useFormScope();
const { period } = useFormScope();

const isCron = (period === PERIOD.CRON);

const cronOrTime = isCron
? <Cron value={cronExpression} error={isError} />
? <Cron />
: <Time />;

const cronExamples = isCron && (
<CronExamples />
);

const cronMessage = isCron && (
<div className={classNames([s.message, isError && s.error ])}>
<CronTrue value={cronExpression} setValidity={setValidity} />
</div>
<CronTrue />
);

const dayInput = period === PERIOD.WEEKLY && (
Expand Down
15 changes: 0 additions & 15 deletions src/components/Jobs/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,6 @@
text-align: center;
}

.message {
white-space: pre-wrap;
line-height: 1.5em;
margin-bottom: 1em;
padding: 1em;
border: 1px solid $color-focus;
border-radius: 6px;
}

.error {
color: $color-danger-dark;
border-color: $color-danger-dark;
background-color: $color-danger-light;
}

@media (max-width: $mobile-width) {
.item {
min-width: auto;
Expand Down
2 changes: 1 addition & 1 deletion src/components/Parser/isValidConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { reservedWords } from '../../util/reservedWords';
import { isValidFunctionLocation, isUTCTime, isValidFunctionName } from '../../util/validator';
import { isString } from '../../util/component';
import { KEYS, MAX_ITEMS } from '../../constants';
import { parseCron } from '../CronTrue/parseCron';
import { parseCron } from '../../util/parseCron';

type TValidResult = [
hasError: boolean,
Expand Down
2 changes: 2 additions & 0 deletions src/util/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface IItem {
readonly dayOfWeek: TWeekList;
readonly dateInMonth: number;
readonly cronExpression: string;
readonly cronError?: string;
readonly period: PERIOD;
readonly isNew?: boolean;
}
Expand Down Expand Up @@ -100,6 +101,7 @@ export const newItem = (isNew?: boolean): IItem => ({
dayOfWeek: dDay,
dateInMonth: 1,
cronExpression: dCron,
cronError: '',
period: PERIOD.DAILY,
isNew,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, it } from 'node:test';
import { strictEqual } from 'node:assert/strict';

import { parseCron } from './parseCron';
import list from '../Tooltip/CronExamplesTooltip/cronExamples.json' with { type: 'json' };
import list from '../components/Tooltip/CronExamplesTooltip/cronExamples.json' with { type: 'json' };

const invalidList = [
'* * * * * * *',
Expand Down
File renamed without changes.
2 changes: 0 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ const buildConfig = ({ NODE_ENV }) => {
options: {
cacheDirectory: isDev,
cacheCompression: false,
comments: isDev,
compact: isProd,
minified: isProd,
plugins: [
Expand All @@ -175,7 +174,6 @@ const buildConfig = ({ NODE_ENV }) => {
options: {
cacheDirectory: isDev,
cacheCompression: false,
comments: isDev,
compact: isProd,
minified: isProd,
presets: [
Expand Down

0 comments on commit b7eef8c

Please sign in to comment.