Corrected the place where the task name is generated, it's now part of the capability editor rather than bespoke code.
This commit is contained in:
parent
f30120d448
commit
6f49add5f7
@ -1,19 +1,18 @@
|
||||
import React from "react";
|
||||
import templateVersionsService, {
|
||||
TaskMetadata,
|
||||
} from "../services/WorkflowTemplateService";
|
||||
import { TaskMetadata } from "../services/WorkflowTemplateService";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Namespaces } from "../../../../i18n/i18n";
|
||||
|
||||
interface AddTaskButtonProps {
|
||||
taskType: string;
|
||||
allowedTasks: TaskMetadata[];
|
||||
onAdd: (selectedType: TaskMetadata) => void;
|
||||
}
|
||||
|
||||
const AddTaskButton: React.FC<AddTaskButtonProps> = ({ taskType, onAdd }) => {
|
||||
const AddTaskButton: React.FC<AddTaskButtonProps> = ({
|
||||
allowedTasks,
|
||||
onAdd,
|
||||
}) => {
|
||||
const [open, setOpen] = React.useState(false);
|
||||
const [items, setItems] = React.useState<TaskMetadata[]>([]);
|
||||
const [loading, setLoading] = React.useState(false);
|
||||
|
||||
const { t: tTaskType } = useTranslation(Namespaces.TaskTypes);
|
||||
const { t } = useTranslation(Namespaces.Common);
|
||||
@ -21,17 +20,6 @@ const AddTaskButton: React.FC<AddTaskButtonProps> = ({ taskType, onAdd }) => {
|
||||
const toggle = async () => {
|
||||
const next = !open;
|
||||
setOpen(next);
|
||||
|
||||
// Fetch only when opening AND only once
|
||||
if (next && items.length === 0) {
|
||||
setLoading(true);
|
||||
try {
|
||||
const meta = await templateVersionsService.getTaskMetadata(taskType);
|
||||
setItems(meta);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
@ -42,21 +30,18 @@ const AddTaskButton: React.FC<AddTaskButtonProps> = ({ taskType, onAdd }) => {
|
||||
|
||||
{open && (
|
||||
<div className="dropdown-menu show">
|
||||
{loading && <div className="dropdown-item">{t("Loading")}</div>}
|
||||
|
||||
{!loading &&
|
||||
items.map((item) => (
|
||||
<button
|
||||
key={item.taskType}
|
||||
className="dropdown-item"
|
||||
onClick={() => {
|
||||
onAdd(item);
|
||||
setOpen(false);
|
||||
}}
|
||||
>
|
||||
{tTaskType(item.displayName)}
|
||||
</button>
|
||||
))}
|
||||
{allowedTasks.map((item) => (
|
||||
<button
|
||||
key={item.taskType}
|
||||
className="dropdown-item"
|
||||
onClick={() => {
|
||||
onAdd(item);
|
||||
setOpen(false);
|
||||
}}
|
||||
>
|
||||
{tTaskType(item.displayName)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@ -1,12 +1,18 @@
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { InputType } from "../../../../../components/common/Input";
|
||||
import { TaskDefinition } from "../../services/WorkflowTemplateService";
|
||||
import {
|
||||
TaskDefinition,
|
||||
TaskMetadata,
|
||||
} from "../../services/WorkflowTemplateService";
|
||||
import { renderTaskField } from "../taskEditorHelpers";
|
||||
import { TaskValidationResult } from "../TasksEditor";
|
||||
import { Namespaces } from "../../../../../i18n/i18n";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
interface TaskCoreEditorProps {
|
||||
task: TaskDefinition;
|
||||
allTasks: TaskDefinition[];
|
||||
allowedTasks: TaskMetadata[];
|
||||
onChange: (updated: TaskDefinition) => void;
|
||||
onValidate: (result: TaskValidationResult) => void;
|
||||
}
|
||||
@ -14,22 +20,62 @@ interface TaskCoreEditorProps {
|
||||
export const TaskCoreEditor: React.FC<TaskCoreEditorProps> = ({
|
||||
task,
|
||||
allTasks,
|
||||
allowedTasks,
|
||||
onChange,
|
||||
onValidate,
|
||||
}) => {
|
||||
const { t: tTaskType } = useTranslation(Namespaces.TaskTypes);
|
||||
const [fieldErrors, setFieldErrors] = useState<Record<string, string>>({});
|
||||
const prevErrorsRef = useRef<Record<string, string>>({});
|
||||
|
||||
const formatNewTaskName = (
|
||||
tasks: TaskDefinition<Record<string, unknown>>[],
|
||||
) => {
|
||||
const displayName = allowedTasks.find(
|
||||
(t) => t.taskType === task.type,
|
||||
)?.displayName;
|
||||
|
||||
return `${tTaskType(displayName!)} ${tasks.length + 1}`;
|
||||
};
|
||||
|
||||
const runValidation = useCallback(() => {
|
||||
const errors: Record<string, string> = {};
|
||||
|
||||
if (!task.config.description) {
|
||||
errors["description"] = "Description cannot be empty";
|
||||
//If the task doesn't have a name (can happen when adding a new task), generate a default one.
|
||||
if (task.config.name === undefined) {
|
||||
task.config.name = formatNewTaskName(allTasks);
|
||||
}
|
||||
|
||||
if (!task.config.name || (task.config.name as string).trim() === "") {
|
||||
errors["name"] = "Name cannot be empty";
|
||||
}
|
||||
|
||||
if (task.config.name) {
|
||||
// Name must be unique across all tasks
|
||||
const duplicate = allTasks.find(
|
||||
(t) =>
|
||||
(t.config.guid as string) !== (task.config.guid as string) && // exclude self
|
||||
(t.config.name as string).trim().toLowerCase() ===
|
||||
(task.config.name as string).trim().toLowerCase(),
|
||||
);
|
||||
|
||||
if (duplicate) {
|
||||
errors["name"] = "Name must be unique.";
|
||||
}
|
||||
}
|
||||
|
||||
const descriptionMaxLength = 5000;
|
||||
if (
|
||||
task.config.description &&
|
||||
(task.config.description as string).length >= descriptionMaxLength
|
||||
) {
|
||||
errors["description"] =
|
||||
`Description can be up to ${descriptionMaxLength} characters long.`;
|
||||
}
|
||||
|
||||
const isValid = Object.keys(errors).length === 0;
|
||||
return { errors, isValid };
|
||||
}, [task.config.description]);
|
||||
}, [allTasks, task.config.description, task.config.guid, task.config.name]);
|
||||
|
||||
//Validate when task changes.
|
||||
useEffect(() => {
|
||||
|
||||
@ -13,7 +13,7 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||
interface TaskListProps {
|
||||
tasks: TaskDefinition[];
|
||||
validTasksList: Record<string, boolean>;
|
||||
taskType: string;
|
||||
allowedTasks: TaskMetadata[];
|
||||
onChange: (tasks: TaskDefinition[]) => void;
|
||||
selectedTask: TaskDefinition | null;
|
||||
onSelectTask: (task: TaskDefinition | null) => void;
|
||||
@ -22,28 +22,16 @@ interface TaskListProps {
|
||||
const TaskList: React.FC<TaskListProps> = ({
|
||||
tasks,
|
||||
validTasksList,
|
||||
taskType,
|
||||
allowedTasks,
|
||||
onChange,
|
||||
selectedTask,
|
||||
onSelectTask,
|
||||
}) => {
|
||||
const { t: tTaskType } = useTranslation(Namespaces.TaskTypes);
|
||||
|
||||
const formatNewTaskName = (
|
||||
displayName: string,
|
||||
tasks: TaskDefinition<Record<string, unknown>>[],
|
||||
) => {
|
||||
return `${tTaskType(displayName)} ${tasks.length + 1}`;
|
||||
};
|
||||
|
||||
const handleAddTask = (selectedType: TaskMetadata) => {
|
||||
const formattedName = formatNewTaskName(selectedType.displayName, tasks);
|
||||
|
||||
const newTask: TaskDefinition = {
|
||||
type: selectedType.taskType,
|
||||
|
||||
config: {
|
||||
name: formattedName,
|
||||
guid: crypto.randomUUID(),
|
||||
},
|
||||
};
|
||||
@ -56,7 +44,7 @@ const TaskList: React.FC<TaskListProps> = ({
|
||||
|
||||
return (
|
||||
<div>
|
||||
<AddTaskButton taskType={taskType} onAdd={handleAddTask} />
|
||||
<AddTaskButton allowedTasks={allowedTasks} onAdd={handleAddTask} />
|
||||
|
||||
<SelectableList
|
||||
items={tasks}
|
||||
|
||||
@ -10,6 +10,7 @@ export interface TaskValidationResult {
|
||||
interface TaskEditorProps {
|
||||
task: TaskDefinition;
|
||||
allTasks: TaskDefinition[];
|
||||
allowedTasks: TaskMetadata[];
|
||||
onChange: (updatedTask: TaskDefinition) => void;
|
||||
onValidate: (taskId: string, isValid: boolean) => void;
|
||||
}
|
||||
@ -17,6 +18,7 @@ interface TaskEditorProps {
|
||||
export const TaskEditor: React.FC<TaskEditorProps> = ({
|
||||
task,
|
||||
allTasks,
|
||||
allowedTasks,
|
||||
onChange,
|
||||
onValidate,
|
||||
}) => {
|
||||
@ -43,6 +45,7 @@ export const TaskEditor: React.FC<TaskEditorProps> = ({
|
||||
<>
|
||||
<TaskCoreEditor
|
||||
task={task}
|
||||
allowedTasks={allowedTasks}
|
||||
allTasks={allTasks}
|
||||
onChange={onChange}
|
||||
onValidate={(result) => onCapabilityValidate("core", result)}
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
import React, { useEffect, useState } from "react";
|
||||
import {
|
||||
import templateVersionsService, {
|
||||
CreateWorkflowTemplateVersion,
|
||||
TaskDefinition,
|
||||
TaskMetadata,
|
||||
} from "../services/WorkflowTemplateService";
|
||||
import TaskList from "./TaskList";
|
||||
import { TaskEditor } from "./TasksEditor";
|
||||
@ -25,6 +26,16 @@ const TasksTab: React.FC<TasksTabProps> = ({
|
||||
}) => {
|
||||
const tasks = data.tasks;
|
||||
const [selectedTask, setSelectedTask] = useState<TaskDefinition | null>(null);
|
||||
const [allowedTasks, setAllowedTasks] = React.useState<TaskMetadata[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchTaskMetadata = async () => {
|
||||
const meta = await templateVersionsService.getTaskMetadata("GeneralTask");
|
||||
setAllowedTasks(meta);
|
||||
};
|
||||
|
||||
fetchTaskMetadata();
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (tasks.length === 0) {
|
||||
@ -91,7 +102,7 @@ const TasksTab: React.FC<TasksTabProps> = ({
|
||||
<TaskList
|
||||
tasks={tasks}
|
||||
validTasksList={taskValidation}
|
||||
taskType="GeneralTask"
|
||||
allowedTasks={allowedTasks}
|
||||
onChange={handleTasksChange}
|
||||
selectedTask={selectedTask}
|
||||
onSelectTask={setSelectedTask}
|
||||
@ -99,6 +110,7 @@ const TasksTab: React.FC<TasksTabProps> = ({
|
||||
</div>
|
||||
{selectedTask && (
|
||||
<TaskEditor
|
||||
allowedTasks={allowedTasks}
|
||||
task={selectedTask}
|
||||
allTasks={tasks}
|
||||
onChange={(updatedTask) => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user