Skip to content

Commit

Permalink
cleanupTasksAndSteps() moved to its own helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunanoordin committed Feb 16, 2024
1 parent 647ddfa commit 0dbfe22
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
26 changes: 1 addition & 25 deletions app/pages/lab-pages-editor/components/TasksPage/TasksPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import getNewStepKey from '../../helpers/getNewStepKey.js';
import getNewTaskKey from '../../helpers/getNewTaskKey.js';
import linkStepsInWorkflow from '../../helpers/linkStepsInWorkflow.js';
import moveItemInArray from '../../helpers/moveItemInArray.js';
import cleanupTasksAndSteps from '../../helpers/cleanupTasksAndSteps.js';
// import strings from '../../strings.json'; // TODO: move all text into strings

import EditStepDialog from './components/EditStepDialog';
Expand Down Expand Up @@ -100,31 +101,6 @@ export default function TasksPage() {
update(cleanedTasksAndSteps);
}

/*
Clean up tasks and steps.
- TODO: Remove steps without tasks.
- TODO: Remove tasks not associated with any step.
- Remove orphaned references in branching tasks.
*/
function cleanupTasksAndSteps(tasks = {}, steps = []) {
const newTasks = structuredClone(tasks); // Copy tasks
const newSteps = steps.slice(); // Copy steps

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

Object.values(newTasks).forEach(taskBody => {
taskBody?.answers?.forEach(answer => {
// If the branching answer points to a non-existent Task Key or Step Key, remove the 'next'.
if (answer.next && !taskKeys.includes(answer.next) && !stepKeys.includes(answer.next)) {
delete answer.next;
}
})
});

return { tasks: newTasks, steps: newSteps };
}

// aka openEditStepDialog
function editStep(stepIndex) {
setActiveStepIndex(stepIndex);
Expand Down
26 changes: 26 additions & 0 deletions app/pages/lab-pages-editor/helpers/cleanupTasksAndSteps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Clean up tasks and steps.
- TODO: Remove steps without tasks.
- TODO: Remove tasks not associated with any step.
- Remove orphaned references in branching tasks.
- Returns { tasks, steps }
*/
export default function cleanupTasksAndSteps(tasks = {}, steps = []) {
const newTasks = structuredClone(tasks); // Copy tasks
const newSteps = steps.slice(); // Copy steps

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

Object.values(newTasks).forEach(taskBody => {
taskBody?.answers?.forEach(answer => {
// If the branching answer points to a non-existent Task Key or Step Key, remove the 'next'.
if (answer.next && !taskKeys.includes(answer.next) && !stepKeys.includes(answer.next)) {
delete answer.next;
}
})
});

return { tasks: newTasks, steps: newSteps };
}

0 comments on commit 0dbfe22

Please sign in to comment.