Skip to content

Commit

Permalink
NEOS-355:updated neocron with disbale schedule button (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
evisdrenova authored Dec 6, 2023
1 parent 4d2465b commit 83d9937
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
45 changes: 36 additions & 9 deletions frontend/app/new/job/define/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
FormMessage,
} from '@/components/ui/form';
import { Input } from '@/components/ui/input';
import { Switch } from '@/components/ui/switch';
import { yupResolver } from '@hookform/resolvers/yup';
import NeoCron from 'neocron';
import 'neocron/dist/src/globals.css';
Expand All @@ -37,17 +38,19 @@ export default function Page({ searchParams }: PageProps): ReactElement {
}
}, [searchParams?.sessionId]);

const defaultCronString = '0 0 * * *'; //by default runs every day

const sessionPrefix = searchParams?.sessionId ?? '';
const [defaultValues] = useSessionStorage<DefineFormValues>(
`${sessionPrefix}-new-job-define`,
{
jobName: '',
cronSchedule: '* * * * *',
cronSchedule: '',
initiateJobRun: false,
}
);

const form = useForm({
const form = useForm<DefineFormValues>({
resolver: yupResolver<DefineFormValues>(DEFINE_FORM_SCHEMA),
defaultValues,
});
Expand All @@ -63,6 +66,9 @@ export default function Page({ searchParams }: PageProps): ReactElement {
const newJobType = getNewJobType(getSingleOrUndefined(searchParams?.jobType));

async function onSubmit(_values: DefineFormValues) {
if (disableSchedule) {
form.setValue('cronSchedule', '');
}
if (newJobType === 'generate-table') {
router.push(
`/new/job/generate/single/connect?sessionId=${sessionPrefix}`
Expand All @@ -78,6 +84,8 @@ export default function Page({ searchParams }: PageProps): ReactElement {
setIsClient(true);
}, []);

const [disableSchedule, setDisableSchedule] = useState<boolean>(true);

return (
<div
id="newjobdefine"
Expand Down Expand Up @@ -117,22 +125,41 @@ export default function Page({ searchParams }: PageProps): ReactElement {
</FormItem>
)}
/>

<div>
<div></div>
</div>
{isClient && (
<Controller
control={form.control}
name="cronSchedule"
render={({ field }) => (
<FormItem>
<FormLabel>Schedule</FormLabel>
<div className="flex flex-row items-center gap-2">
<FormLabel>Schedule</FormLabel>
<Switch
checked={!disableSchedule}
onCheckedChange={() => {
disableSchedule
? setDisableSchedule(false)
: setDisableSchedule(true);
}}
/>
</div>
<FormDescription>
Define a schedule to run this job.
Define a schedule to run this job. If disabled, job will
need to be manually triggered.
</FormDescription>
<FormControl>
<NeoCron
cronString={field.value ?? ''}
defaultCronString="* * * * *"
setCronString={field.onChange}
/>
{!disableSchedule && (
<NeoCron
cronString={
field.value ? field.value : defaultCronString
}
defaultCronString={defaultCronString}
setCronString={field.onChange}
/>
)}
</FormControl>
<FormMessage />
</FormItem>
Expand Down
10 changes: 1 addition & 9 deletions frontend/app/new/job/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
SCHEMA_FORM_SCHEMA,
SOURCE_FORM_SCHEMA,
} from '@/yup-validations/jobs';
import cron from 'cron-validate';
import * as Yup from 'yup';

export const DEFINE_FORM_SCHEMA = Yup.object({
Expand All @@ -26,14 +25,7 @@ export const DEFINE_FORM_SCHEMA = Yup.object({
const res = await isJobNameAvailable(value, account.id);
return res.isAvailable;
}),
cronSchedule: Yup.string()
.optional()
.test('isValidCron', 'Not a valid cron schedule', (value) => {
if (!value) {
return true;
}
return !!value && cron(value).isValid();
}),
cronSchedule: Yup.string().optional(),
initiateJobRun: Yup.boolean(),
});

Expand Down

0 comments on commit 83d9937

Please sign in to comment.