Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Classifier: Simple dropdown task adapter #1830

Merged
merged 2 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cuid from 'cuid'
import { types } from 'mobx-state-tree'
import { panoptesAdapter } from './helpers'
import Task from '../../models/Task'
import SimpleDropdownAnnotation from './SimpleDropdownAnnotation'

Expand All @@ -14,6 +15,9 @@ const SimpleDropdown = types.model('SimpleDropdown', {
options: types.array(types.string),
type: types.literal('dropdown-simple'),
})
.preProcessSnapshot(snapshot => {
return panoptesAdapter(snapshot)
})
.views(self => ({
get defaultAnnotation () {
return SimpleDropdownAnnotation.create({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { panoptesAdapter } from './panoptesAdapter'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { panoptesAdapter } from './panoptesAdapter'
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function panoptesAdapter(snapshot) {
const newSnapshot = {}
const { selects } = snapshot
if (selects?.length === 1) {
const menu = selects[0]
newSnapshot.allowCreate = menu.allowCreate
newSnapshot.help = snapshot.help
newSnapshot.instruction = snapshot.instruction
newSnapshot.taskKey = snapshot.taskKey
newSnapshot.type = 'dropdown-simple'
const options = menu.options['*']
newSnapshot.options = options.map(option => option.label)
return newSnapshot
}
return snapshot
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { panoptesAdapter } from './'

describe('SimpleDropdownTask > panoptesAdapter', function () {

describe('with a simple dropdown task', function () {
let originalTask

before(function () {
originalTask = {
allowCreate: false,
taskKey: 'T0',
type: 'dropdown-simple',
help: 'This is some task help',
instruction: 'This is the task instruction',
options: [ 'One', 'Two' ]
}
})

it('should return the task unchanged', function () {
expect(panoptesAdapter(originalTask)).to.equal(originalTask)
})
})

describe('with a single dropdown menu', function () {
let originalTask

before(function () {
originalTask = {
taskKey: 'T0',
type: 'dropdown',
help: 'This is some task help',
instruction: 'This is the task instruction',
selects: [
{
allowCreate: false,
options: {
'*': [
{ label: 'One', value: 1 },
{ label: 'Two', value: 2 }
]
},
title: 'Test dropdown'
}
]
}
})

it('should return a simple dropdown task', function () {
expect(panoptesAdapter(originalTask)).to.deep.equal({
allowCreate: false,
taskKey: 'T0',
type: 'dropdown-simple',
help: 'This is some task help',
instruction: 'This is the task instruction',
options: [ 'One', 'Two' ]
})
})
})

describe('with a complex dropdown task', function () {
let originalTask

before(function () {
originalTask = {
taskKey: 'T0',
type: 'dropdown',
help: 'This is some task help',
instruction: 'This is the task instruction',
selects: [
{
allowCreate: false,
options: {
'*': [
{ label: 'One', value: 1 },
{ label: 'Two', value: 2 }
]
},
title: 'Numbers'
},
{
allowCreate: false,
options: {
'*': [
{ label: 'Red', value: 1 },
{ label: 'Blue', value: 2 }
]
},
title: 'Colours'
}
]
}
})

it('should return the original task', function () {
expect(panoptesAdapter(originalTask)).to.equal(originalTask)
})
})
})
9 changes: 8 additions & 1 deletion packages/lib-classifier/src/store/Step.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { types } from 'mobx-state-tree'
import taskRegistry, { taskModels } from '@plugins/tasks'

function taskDispatcher (snapshot) {
return taskRegistry.get(snapshot.type).TaskModel
switch (snapshot.type) {
case 'dropdown': {
return taskRegistry.get('dropdown-simple').TaskModel
}
default: {
return taskRegistry.get(snapshot.type).TaskModel
}
}
}

const GenericTask = types.union({ dispatcher: taskDispatcher }, ...taskModels)
Expand Down