-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanupTasksAndSteps() moved to its own helper function
- Loading branch information
1 parent
647ddfa
commit 0dbfe22
Showing
2 changed files
with
27 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
app/pages/lab-pages-editor/helpers/cleanupTasksAndSteps.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |