diff --git a/src/modules/manager/workflowTemplates/components/CapabilityEditors/TaskCoreEditor.tsx b/src/modules/manager/workflowTemplates/components/CapabilityEditors/TaskCoreEditor.tsx index 69cbc00..632f611 100644 --- a/src/modules/manager/workflowTemplates/components/CapabilityEditors/TaskCoreEditor.tsx +++ b/src/modules/manager/workflowTemplates/components/CapabilityEditors/TaskCoreEditor.tsx @@ -1,4 +1,4 @@ -import { useEffect, useRef, useState } from "react"; +import { useCallback, useEffect, useRef, useState } from "react"; import { InputType } from "../../../../../components/common/Input"; import { TaskDefinition } from "../../services/WorkflowTemplateService"; import { renderTaskField } from "../taskEditorHelpers"; @@ -20,17 +20,31 @@ export const TaskCoreEditor: React.FC = ({ const [fieldErrors, setFieldErrors] = useState>({}); const prevErrorsRef = useRef>({}); - useEffect(() => { + const runValidation = useCallback(() => { const errors: Record = {}; - // Validation rules - if (task.config.description === "") { + if (!task.config.description) { errors["description"] = "Description cannot be empty"; } const isValid = Object.keys(errors).length === 0; + return { errors, isValid }; + }, [task.config.description]); + + //Validate when task changes. + useEffect(() => { + const { errors, isValid } = runValidation(); + + setFieldErrors(errors); + prevErrorsRef.current = errors; + + onValidate({ isValid, errors }); + }, [onValidate, runValidation, task.config.guid]); + + //Validate when fields change + useEffect(() => { + const { errors, isValid } = runValidation(); - // Compare with previous errors const prevErrors = prevErrorsRef.current; const errorsChanged = Object.keys(prevErrors).length !== Object.keys(errors).length || @@ -41,7 +55,7 @@ export const TaskCoreEditor: React.FC = ({ onValidate({ isValid, errors }); prevErrorsRef.current = errors; } - }, [task.config.description, task.config.name, onValidate]); + }, [task.config.description, task.config.name, onValidate, runValidation]); return (
diff --git a/src/modules/manager/workflowTemplates/components/TaskList.tsx b/src/modules/manager/workflowTemplates/components/TaskList.tsx index 1f567ea..44ead31 100644 --- a/src/modules/manager/workflowTemplates/components/TaskList.tsx +++ b/src/modules/manager/workflowTemplates/components/TaskList.tsx @@ -48,9 +48,9 @@ const TaskList: React.FC = ({ }, }; - if (tasks.length === 0) { - onSelectTask(newTask); - } + //if (tasks.length === 0) { + onSelectTask(newTask); + //} onChange([...tasks, newTask]); }; diff --git a/src/modules/manager/workflowTemplates/components/TasksEditor.tsx b/src/modules/manager/workflowTemplates/components/TasksEditor.tsx index e49d5b5..9c44b90 100644 --- a/src/modules/manager/workflowTemplates/components/TasksEditor.tsx +++ b/src/modules/manager/workflowTemplates/components/TasksEditor.tsx @@ -1,4 +1,4 @@ -import { useState } from "react"; +import { useEffect, useState } from "react"; import { TaskDefinition } from "../services/WorkflowTemplateService"; import { TaskCoreEditor } from "./CapabilityEditors/TaskCoreEditor"; diff --git a/src/modules/manager/workflowTemplates/components/TasksTab.tsx b/src/modules/manager/workflowTemplates/components/TasksTab.tsx index 9876d39..c54d6c0 100644 --- a/src/modules/manager/workflowTemplates/components/TasksTab.tsx +++ b/src/modules/manager/workflowTemplates/components/TasksTab.tsx @@ -50,7 +50,6 @@ const TasksTab: React.FC = ({ } if (!selectedTask) { - setSelectedTask(tasks[0]); return; }