Skip to content

Commit

Permalink
TasksPage: deleteTask() closes dialog if last task in step. cleanupTa…
Browse files Browse the repository at this point in the history
…sksAndSteps: fix order of actions.
  • Loading branch information
shaunanoordin committed Apr 9, 2024
1 parent a6097ac commit ae9d5d1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
33 changes: 24 additions & 9 deletions app/pages/lab-pages-editor/components/TasksPage/TasksPage.jsx
Expand Up @@ -76,22 +76,37 @@ export default function TasksPage() {
}

function deleteTask(taskKey) {
// First check: does it exist?
if (!taskKey) return;
if (!workflow?.tasks?.[taskKey]) return;
// First check: does the task exist?
if (!workflow || !taskKey || !workflow?.tasks?.[taskKey]) return;

// Second check: is this the only task in the step?
const activeStepTaskKeys = workflow.steps?.[activeStepIndex]?.[1]?.taskKeys || [];
const onlyTaskInStep = !!(activeStepTaskKeys.length === 1 && activeStepTaskKeys[0] === taskKey);

// Third check: are you sure?
const confirmed = onlyTaskInStep
? confirm(`Delete Task ${taskKey}? This will also delete the Page.`)
: confirm(`Delete Task ${taskKey}?`);
if (!confirmed) return;

// Delete the task
const newTasks = structuredClone(workflow?.tasks || {});
// Delete the task.
const newTasks = structuredClone(workflow.tasks || {});
delete newTasks[taskKey];

// Delete the task reference in steps
const newSteps = structuredClone(workflow?.steps || {});
// Delete the task reference in steps.
const newSteps = structuredClone(workflow.steps || {});
newSteps.forEach(step => {
const stepBody = step[1] || {};
stepBody.taskKeys = (stepBody?.taskKeys || []).filter(key => key !== taskKey);
});
});

// Close the Edit Step Dialog, if necessary.
// Note that this will also trigger handleCloseEditStepDialog()
if (onlyTaskInStep) {
editStepDialog.current?.closeDialog();
}

// Cleanup, then commit
// Cleanup, then commit.
const cleanedTasksAndSteps = cleanupTasksAndSteps(newTasks, newSteps);
update(cleanedTasksAndSteps);
}
Expand Down
Expand Up @@ -27,6 +27,7 @@ function EditStepDialog({

useImperativeHandle(forwardedRef, () => {
return {
closeDialog,
openDialog
};
});
Expand Down
6 changes: 3 additions & 3 deletions app/pages/lab-pages-editor/helpers/cleanupTasksAndSteps.js
Expand Up @@ -12,9 +12,6 @@ export default function cleanupTasksAndSteps(tasks = {}, steps = []) {
const newTasks = structuredClone(tasks); // Copy tasks
let newSteps = structuredClone(steps); // Copy steps. This is a deep copy, compared to steps.slice()

const taskKeys = Object.keys(newTasks);
const stepKeys = newSteps.map(step => step[0]);

// Remove steps without tasks.
newSteps = newSteps.filter(step => step?.[1]?.taskKeys?.length > 0);

Expand All @@ -27,6 +24,9 @@ export default function cleanupTasksAndSteps(tasks = {}, steps = []) {
if (!existsInAnyStep) delete newTasks[taskKey];
});

const taskKeys = Object.keys(newTasks);
const stepKeys = newSteps.map(step => step[0]);

// Remove orphaned references in branching tasks.
Object.values(newTasks).forEach(task => {
task?.answers?.forEach(answer => {
Expand Down

0 comments on commit ae9d5d1

Please sign in to comment.