Skip to content

Commit

Permalink
fix: clear references to a task key after deleting a task
Browse files Browse the repository at this point in the history
A small helper function which iterates through `workflow.tasks` and deletes references to a given task key. It's called after deleting a task, so that other tasks no longer link to the deleted task.
  • Loading branch information
eatyourgreens committed May 10, 2024
1 parent 9ca8f70 commit ae4b83b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
14 changes: 14 additions & 0 deletions app/pages/lab/helpers/removeTaskKeyFromWorkflow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default function removeTaskKeyFromWorkflow(workflow, taskKey) {
const changes = {};
Object.entries(workflow.tasks).forEach(([key, task]) => {
if (task.next === taskKey) {
changes[`tasks.${key}.next`] = '';
}
task.answers?.forEach((answer, index) => {
if (answer.next === taskKey) {
changes[`tasks.${key}.answers.${index}.next`] = '';
}
});
});
return workflow.update(changes);
}
43 changes: 43 additions & 0 deletions app/pages/lab/helpers/removeTaskKeyFromWorkflow.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { expect } from 'chai';
import sinon from 'sinon';
import removeTaskKeyFromWorkflow from './removeTaskKeyFromWorkflow.js';

describe('removeTaskKeyFromWorkflow', function () {
let workflow;

beforeEach(function () {
workflow = {
tasks: {
'T1': {
type: 'single',
answers: [
{ label: 'Yes', next: 'T3' },
{ label: 'No', next: 'T2' }
]
},
'T2': {
type: 'multiple',
next: 'T3',
answers: [
{ label: 'Blue' },
{ label: 'Green' }
]
},
'T3': {
type: 'text',
next: ''
}
},
update: sinon.stub(),
save: sinon.stub()
};
});

it('should delete the selected key from each task', function () {
removeTaskKeyFromWorkflow(workflow, 'T3');
expect(workflow.update).to.have.been.calledWith({
'tasks.T1.answers.0.next': '',
'tasks.T2.next': ''
});
});
});
4 changes: 3 additions & 1 deletion app/pages/lab/workflow.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Tutorials from './workflow-components/tutorials.jsx';
import TaskOptions from './workflow-components/task-options.jsx';
import TaskPicker from './workflow-components/task-picker.jsx';
import TaskEditor from './workflow-components/task-editor.jsx';
import removeTaskFromWorkflow from './helpers/removeTaskKeyFromWorkflow.js';

const DEMO_SUBJECT_SET_ID = process.env.NODE_ENV === 'production'
? '6' // Cats
Expand Down Expand Up @@ -807,8 +808,9 @@ class EditWorkflowPage extends Component {
changes[`tasks.${taskKey}`] = undefined;
this.props.workflow.update(changes);
}
removeTaskFromWorkflow(this.props.workflow, taskKey);

return this.updateFirstTask();
return this.updateFirstTask().save();
}
}
}
Expand Down

0 comments on commit ae4b83b

Please sign in to comment.