From 352574abe403f160a1b418a9254840b25aabdfd1 Mon Sep 17 00:00:00 2001 From: "Shaun A. Noordin" Date: Tue, 9 Apr 2024 16:55:39 +0100 Subject: [PATCH] cleanupTasksAndSteps: implement cleanup of orphaned tasks and steps --- .../components/TasksPage/TasksPage.jsx | 9 +++------ .../helpers/cleanupTasksAndSteps.js | 19 +++++++++++++------ 2 files changed, 16 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 a2d2c0f8f4..a948863863 100644 --- a/app/pages/lab-pages-editor/components/TasksPage/TasksPage.jsx +++ b/app/pages/lab-pages-editor/components/TasksPage/TasksPage.jsx @@ -21,7 +21,6 @@ export default function TasksPage() { const newTaskDialog = useRef(null); const [ activeStepIndex, setActiveStepIndex ] = useState(-1); // Tracks which Step is being edited. const [ activeDragItem, setActiveDragItem ] = useState(-1); // Keeps track of active item being dragged (StepItem). This is because "dragOver" CAN'T read the data from dragEnter.dataTransfer.getData(). - const activeStepKey = workflow?.steps?.[activeStepIndex]?.[0]; const isActive = true; // TODO /* @@ -85,7 +84,7 @@ export default function TasksPage() { const newTasks = structuredClone(workflow?.tasks || {}); delete newTasks[taskKey]; - // Delete task + // Delete the task reference in steps const newSteps = structuredClone(workflow?.steps || {}); newSteps.forEach(step => { const stepBody = step[1] || {}; @@ -94,7 +93,6 @@ export default function TasksPage() { // Cleanup, then commit const cleanedTasksAndSteps = cleanupTasksAndSteps(newTasks, newSteps); - // console.log('+++ Delete Task: ', taskKey, ' ==> \n', cleanedTasksAndSteps); update(cleanedTasksAndSteps); } @@ -109,16 +107,15 @@ export default function TasksPage() { function deleteStep(stepIndex) { if (!workflow) return; const { steps, tasks } = workflow; - const [ stepKey, stepBody ] = steps[stepIndex] || []; - const tasksToBeDeleted = stepBody?.taskKeys || []; + const [ stepKey ] = steps[stepIndex] || []; const confirmed = confirm(`Delete Page ${stepKey}?`); if (!confirmed) return; const newSteps = steps.toSpliced(stepIndex, 1); // Copy then delete Step at stepIndex const newTasks = tasks ? { ...tasks } : {}; // Copy tasks - tasksToBeDeleted.forEach(taskKey => delete newTasks[taskKey]); + // cleanedupTasksAndSteps() will also remove tasks not associated with any step. const cleanedTasksAndSteps = cleanupTasksAndSteps(newTasks, newSteps); update(cleanedTasksAndSteps); } diff --git a/app/pages/lab-pages-editor/helpers/cleanupTasksAndSteps.js b/app/pages/lab-pages-editor/helpers/cleanupTasksAndSteps.js index cfce9be4f2..b5153191a1 100644 --- a/app/pages/lab-pages-editor/helpers/cleanupTasksAndSteps.js +++ b/app/pages/lab-pages-editor/helpers/cleanupTasksAndSteps.js @@ -1,7 +1,7 @@ /* Clean up tasks and steps. -- TODO: Remove steps without tasks. -- TODO: Remove tasks not associated with any step. +- Remove steps without tasks. +- Remove tasks not associated with any step. - Remove orphaned references in branching tasks. - Remove orphaned references in steps. @@ -15,9 +15,18 @@ export default function cleanupTasksAndSteps(tasks = {}, steps = []) { const taskKeys = Object.keys(newTasks); const stepKeys = newSteps.map(step => step[0]); - // Remove steps without tasks + // Remove steps without tasks. newSteps = newSteps.filter(step => step?.[1]?.taskKeys?.length > 0); + // Remove tasks not associated with any step. + Object.keys(newTasks).forEach(taskKey => { + let existsInAnyStep = false; + newSteps.forEach(step => { + existsInAnyStep = existsInAnyStep || !!step?.[1]?.taskKeys?.includes(taskKey); + }); + if (!existsInAnyStep) delete newTasks[taskKey]; + }); + // Remove orphaned references in branching tasks. Object.values(newTasks).forEach(task => { task?.answers?.forEach(answer => { @@ -30,13 +39,11 @@ export default function cleanupTasksAndSteps(tasks = {}, steps = []) { // Remove orphaned references in steps. newSteps = newSteps.map(step => { - const [stepKey, stepBody] = step; - // If the stepBody points to a non-existent Task Key or Step Key, remove the 'next'. + const [stepKey, stepBody] = step; if (stepBody.next && !taskKeys.includes(stepBody.next) && !stepKeys.includes(stepBody.next)) { delete stepBody.next; } - return [ stepKey, stepBody ] })