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 ff0d887
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
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': ''
});
});
});
7 changes: 3 additions & 4 deletions app/pages/lab/workflow.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { Component, useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import handleInputChange from '../../lib/handle-input-change.js';
import TriggeredModalForm from 'modal-form/triggered';
import ModalFormDialog from 'modal-form/dialog';
import apiClient from 'panoptes-client/lib/api-client';
import ChangeListener from '../../components/change-listener.cjsx';
import RetirementRulesEditor from '../../components/retirement-rules-editor.cjsx';
import {Link} from 'react-router';
import MultiImageSubjectOptionsEditor from '../../components/multi-image-subject-options-editor.jsx';
import taskComponents from '../../classifier/tasks/index.js';
import AutoSave from '../../components/auto-save.coffee';
import FileButton from '../../components/file-button.cjsx';
import WorkflowCreateForm from './workflow-create-form.cjsx';
import workflowActions from './actions/workflow.js';
import classnames from 'classnames';
Expand All @@ -21,6 +18,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 removeTaskKeyFromWorkflow from './helpers/removeTaskKeyFromWorkflow.js';

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

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

0 comments on commit ff0d887

Please sign in to comment.