Skip to content
Draft
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
Expand Up @@ -2,9 +2,12 @@ import './trigger-list-modal.scss'

import React from 'react'

import SimpleDialog from '../../../common/components/modal/simple-dialog'
import Button from '../../../common/components/button'
import Switch from '../../../common/components/switch'
import Common from 'obojobo-document-engine/src/scripts/common'
const { SimpleDialog } = Common.components.modal
const { Button, Switch } = Common.components
const { OboModel } = Common.models

const ASSESSMENT_NODE = 'ObojoboDraft.Sections.Assessment'

class TriggerListModal extends React.Component {
constructor(props) {
Expand All @@ -14,6 +17,7 @@ class TriggerListModal extends React.Component {
if (!this.state.triggers) this.state.triggers = []

this.createTrigger = this.createTrigger.bind(this)
this.renderErrorMessage = this.renderErrorMessage.bind(this)
}

componentWillUnmount() {
Expand Down Expand Up @@ -118,32 +122,58 @@ class TriggerListModal extends React.Component {
}))
}

updateActionValue(triggerIndex, actionIndex, key, event) {
updateActionValue(triggerIndex, actionIndex, key, actionType, event) {
const targetValue = event.target.value
const value = {}
// pull changes off the event
// checkbox handles events from <Switch> being a checkbox
value[key] = event.target.type === 'checkbox' ? event.target.checked : event.target.value
value[key] = event.target.type === 'checkbox' ? event.target.checked : targetValue

// Update triggers[triggerIndex].actions[actionIndex].value.key
// The nested loops insure that React's immutable state is updated properly
return this.setState(prevState => ({
/* eslint-disable no-mixed-spaces-and-tabs */
triggers: prevState.triggers.map((trigger, tIndex) =>
triggerIndex === tIndex
? Object.assign(trigger, {
actions: trigger.actions.map((action, aIndex) =>
actionIndex === aIndex
? Object.assign(action, {
value: Object.assign({}, action.value, value)
})
: action
)
})
: trigger
)
triggers: prevState.triggers.map((trigger, tIndex) => {
if (triggerIndex === tIndex) {
trigger = Object.assign(trigger, {
actions: trigger.actions.map((action, aIndex) => {

if (actionIndex === aIndex) {
action = Object.assign(action, { value: Object.assign({}, action.value, value) })

// Updating error message if needed
if (actionType && (actionType === 'assessment:startAttempt' || actionType === 'assessment:endAttempt')) {

const typedId = targetValue
if (!this.isValidAssessmentId(typedId)) {
action = Object.assign(action, { errorMessage: "Invalid Assessment Id" })
}else {
action = Object.assign(action, { errorMessage: "" })
}
}
}

return action
})
})
}

return trigger
})
}))
}

isValidAssessmentId(id) {
if (id === "") return true

// Checks if id exists
if (!OboModel.models[id]) {
return false
}

// Checks if found id belongs to an assessment
return OboModel.models[id].attributes.type === ASSESSMENT_NODE
}

deleteAction(triggerIndex, actionIndex) {
// Delete triggers[triggerIndex].actions[actionIndex]
return this.setState(prevState => ({
Expand Down Expand Up @@ -218,6 +248,15 @@ class TriggerListModal extends React.Component {
}))
}

renderErrorMessage(triggerIndex, actionIndex) {
const trigger = this.state.triggers[triggerIndex]
const action = trigger.actions[actionIndex]

if (action.errorMessage) {
return <span className="invalid-assessment-id-message">{action.errorMessage}</span>
}
}

renderActionOptions(triggerIndex, actionIndex, action) {
switch (action.type) {
case 'nav:goto':
Expand Down Expand Up @@ -255,9 +294,10 @@ class TriggerListModal extends React.Component {
<input
className="input-item"
value={action.value.id || ''}
onChange={this.updateActionValue.bind(this, triggerIndex, actionIndex, 'id')}
onChange={this.updateActionValue.bind(this, triggerIndex, actionIndex, 'id', action.type)}
/>
</div>
{this.renderErrorMessage(triggerIndex, actionIndex)}
</div>
)
case 'viewer:alert':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@
margin-bottom: 1em;
margin-right: 4.7em;
}

.invalid-assessment-id-message {
display: block;
border-radius: $dimension-rounded-radius;
color: $color-bg;
background-color: $color-error;
width: 10.2em;
padding: 0.2em;
text-align: left;
margin-left: 15.8em;
margin-bottom: 1em;
}
}
}

Expand Down