Skip to content

Commit

Permalink
cleanupTasksAndSteps: implement cleanup of orphaned tasks and steps
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunanoordin committed Apr 25, 2024
1 parent cfed5c4 commit 352574a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
9 changes: 3 additions & 6 deletions app/pages/lab-pages-editor/components/TasksPage/TasksPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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

/*
Expand Down Expand Up @@ -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] || {};
Expand All @@ -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);
}

Expand All @@ -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);
}
Expand Down
19 changes: 13 additions & 6 deletions app/pages/lab-pages-editor/helpers/cleanupTasksAndSteps.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 => {
Expand All @@ -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 ]
})

Expand Down

0 comments on commit 352574a

Please sign in to comment.