From ae9d5d13682ea9c62ff645882a0f554daee2d6ec Mon Sep 17 00:00:00 2001 From: "Shaun A. Noordin" Date: Tue, 9 Apr 2024 18:01:48 +0100 Subject: [PATCH] TasksPage: deleteTask() closes dialog if last task in step. cleanupTasksAndSteps: fix order of actions. --- .../components/TasksPage/TasksPage.jsx | 33 ++++++++++++++----- .../EditStepDialog/EditStepDialog.jsx | 1 + .../helpers/cleanupTasksAndSteps.js | 6 ++-- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/app/pages/lab-pages-editor/components/TasksPage/TasksPage.jsx b/app/pages/lab-pages-editor/components/TasksPage/TasksPage.jsx index a948863863..2f421243b7 100644 --- a/app/pages/lab-pages-editor/components/TasksPage/TasksPage.jsx +++ b/app/pages/lab-pages-editor/components/TasksPage/TasksPage.jsx @@ -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); } diff --git a/app/pages/lab-pages-editor/components/TasksPage/components/EditStepDialog/EditStepDialog.jsx b/app/pages/lab-pages-editor/components/TasksPage/components/EditStepDialog/EditStepDialog.jsx index 355d9f61ec..7a3de6a703 100644 --- a/app/pages/lab-pages-editor/components/TasksPage/components/EditStepDialog/EditStepDialog.jsx +++ b/app/pages/lab-pages-editor/components/TasksPage/components/EditStepDialog/EditStepDialog.jsx @@ -27,6 +27,7 @@ function EditStepDialog({ useImperativeHandle(forwardedRef, () => { return { + closeDialog, openDialog }; }); diff --git a/app/pages/lab-pages-editor/helpers/cleanupTasksAndSteps.js b/app/pages/lab-pages-editor/helpers/cleanupTasksAndSteps.js index b5153191a1..762558b506 100644 --- a/app/pages/lab-pages-editor/helpers/cleanupTasksAndSteps.js +++ b/app/pages/lab-pages-editor/helpers/cleanupTasksAndSteps.js @@ -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); @@ -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 => {