Skip to content
This repository has been archived by the owner on Aug 3, 2022. It is now read-only.

Commit

Permalink
[#753] Only update props if provider is enabled
Browse files Browse the repository at this point in the history
Use the default properties if the provider is not enabled or not
visible, even if the provider value matches the dependency value.
  • Loading branch information
brew committed Aug 18, 2016
1 parent 965dd99 commit f837c9b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 31 deletions.
2 changes: 1 addition & 1 deletion census/controllers/census.js
Expand Up @@ -247,7 +247,7 @@ var submitReact = function(req, res) {

data.currentState = utils.getCurrentState(data, req);

let qsSchema = JSON.parse('[{"defaultProperties":{"enabled":true,"required":true,"visible":true},"id":"like_apples","position":1},{"defaultProperties":{"enabled":false,"required":false,"visible":false},"id":"bananas_instead","if":[{"dependentId":"like_apples","properties":{"enabled":true,"required":true,"visible":true},"value":"No"}],"position":1.1},{"defaultProperties":{"enabled":false,"required":false,"visible":true},"id":"apple_colour","if":[{"dependentId":"like_apples","properties":{"enabled":true,"required":true},"value":"Yes"}],"position":2},{"defaultProperties":{"enabled":false,"required":false,"visible":true},"id":"red_apple_today","if":[{"dependentId":"apple_colour","properties":{"enabled":true,"required":true},"value":"Yes"}],"position":3},{"defaultProperties":{"enabled":false,"required":false,"visible":false},"id":"doctor_away","if":[{"dependentId":"red_apple_today","properties":{"enabled":true,"visible":true},"value":"Yes"}],"position":3.1}]');
let qsSchema = JSON.parse('[{"defaultProperties":{"enabled":true,"required":true,"visible":true},"id":"like_apples","position":1},{"defaultProperties":{"enabled":false,"required":false,"visible":false},"id":"bananas_instead","if":[{"providerId":"like_apples","properties":{"enabled":true,"required":true,"visible":true},"value":"No"}],"position":1.1},{"defaultProperties":{"enabled":false,"required":false,"visible":true},"id":"apple_colour","if":[{"providerId":"like_apples","properties":{"enabled":true,"required":true},"value":"Yes"}],"position":2},{"defaultProperties":{"enabled":false,"required":false,"visible":true},"id":"red_apple_today","if":[{"providerId":"apple_colour","properties":{"enabled":true,"required":true},"value":"Yes"}],"position":3},{"defaultProperties":{"enabled":false,"required":false,"visible":false},"id":"doctor_away","if":[{"providerId":"red_apple_today","properties":{"enabled":true,"visible":true},"value":"Yes"}],"position":3.1}]');
let questions = JSON.parse('[{"id":"like_apples","text":"Do you like apples?","type":""},{"id":"bananas_instead","text":"Do you like bananas instead?","type":""},{"id":"apple_colour","text":"Do you like *RED* apples?","type":""},{"id":"red_apple_today","text":"Have you eaten a red apple today?","type":""},{"id":"doctor_away","text":"Did it keep the doctor away?","type":""}]');

res.render('create-react.html', {
Expand Down
26 changes: 14 additions & 12 deletions census/public/src/bundle.js
Expand Up @@ -61,10 +61,11 @@

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// initial static data for development
// eslint-disable-next-line
var questions = [{ 'id': 'like_apples', 'text': 'Do you like apples?', 'type': '' }, { 'id': 'bananas_instead', 'text': 'Do you like bananas instead?', 'type': '' }, { 'id': 'apple_colour', 'text': 'Do you like *RED* apples?', 'type': '' }, { 'id': 'red_apple_today', 'text': 'Have you eaten a red apple today?', 'type': '' }, { 'id': 'doctor_away', 'text': 'Did it keep the doctor away?', 'type': '' }];
// eslint-disable-next-line
var qsSchema = [{ 'defaultProperties': { 'enabled': true, 'required': true, 'visible': true }, 'id': 'like_apples', 'position': 1 }, { 'defaultProperties': { 'enabled': false, 'required': false, 'visible': false }, 'id': 'bananas_instead', 'if': [{ 'dependentId': 'like_apples', 'properties': { 'enabled': true, 'required': true, 'visible': true }, 'value': 'No' }], 'position': 1.1 }, { 'defaultProperties': { 'enabled': false, 'required': false, 'visible': true }, 'id': 'apple_colour', 'if': [{ 'dependentId': 'like_apples', 'properties': { 'enabled': true, 'required': true }, 'value': 'Yes' }], 'position': 2 }, { 'defaultProperties': { 'enabled': false, 'required': false, 'visible': true }, 'id': 'red_apple_today', 'if': [{ 'dependentId': 'apple_colour', 'properties': { 'enabled': true, 'required': true }, 'value': 'Yes' }], 'position': 3 }, { 'defaultProperties': { 'enabled': false, 'required': false, 'visible': false }, 'id': 'doctor_away', 'if': [{ 'dependentId': 'red_apple_today', 'properties': { 'enabled': true, 'visible': true }, 'value': 'Yes' }], 'position': 3.1 }];
var qsSchema = [{ 'defaultProperties': { 'enabled': true, 'required': true, 'visible': true }, 'id': 'like_apples', 'position': 1 }, { 'defaultProperties': { 'enabled': false, 'required': false, 'visible': false }, 'id': 'bananas_instead', 'if': [{ 'providerId': 'like_apples', 'properties': { 'enabled': true, 'required': true, 'visible': true }, 'value': 'No' }], 'position': 1.1 }, { 'defaultProperties': { 'enabled': false, 'required': false, 'visible': true }, 'id': 'apple_colour', 'if': [{ 'providerId': 'like_apples', 'properties': { 'enabled': true, 'required': true }, 'value': 'Yes' }], 'position': 2 }, { 'defaultProperties': { 'enabled': false, 'required': false, 'visible': true }, 'id': 'red_apple_today', 'if': [{ 'providerId': 'apple_colour', 'properties': { 'enabled': true, 'required': true }, 'value': 'Yes' }], 'position': 3 }, { 'defaultProperties': { 'enabled': false, 'required': false, 'visible': false }, 'id': 'doctor_away', 'if': [{ 'providerId': 'red_apple_today', 'properties': { 'enabled': true, 'visible': true }, 'value': 'Yes' }], 'position': 3.1 }];

(0, _reactDom.render)(_react2.default.createElement(_QuestionForm2.default, { questions: questions, qsSchema: qsSchema }), document.getElementById('questions'));

Expand Down Expand Up @@ -22102,7 +22103,9 @@
/*
Return visible properties (required, enabled, visible) for the question
with `id`. Value of the properties depends on the value of other
dependent questions, based on the question schema.
provider questions, based on the question schema.
For the sake of clarity, `dependant` objects depend on `provider`
objects.
*/

// Get the schema for this id
Expand All @@ -22113,22 +22116,21 @@
// Initally set up return value as the defaultProperties for the schema
var visProps = _lodash2.default.cloneDeep(schema.defaultProperties);

// For each `if` object in the schema, get the dependent value
// For each dependency in the `if` array in the schema
_lodash2.default.each(schema.if, function (dependency) {
// Find the current dependency state
var currentDependency = _lodash2.default.find(_this.state.questionState, function (qState) {
return qState.id === dependency.dependentId;
// Find the current state of the provider
var currentProviderState = _lodash2.default.find(_this.state.questionState, function (qState) {
return qState.id === dependency.providerId;
});
// If the actual value of the dependent field is the same as the
// expected value, and the dependent field is enabled and visible...

// ---> Only activate the dependency properties if the dependent object is itself enabled and visible. <---

if (currentDependency.value === dependency.value) {
// If the actual value of the provider field is the same as the expected
// value, and the provider field is enabled and visible...
var providerVisProps = _this.getVisiblePropsForId(dependency.providerId);
if (currentProviderState.value === dependency.value && providerVisProps.enabled && providerVisProps.visible) {
// Update the return value with the dependency properties.
_lodash2.default.assign(visProps, dependency.properties);
}
});
}, this);
return visProps;
},
getTextForId: function getTextForId(id) {
Expand Down
2 changes: 1 addition & 1 deletion census/public/src/bundle.js.map

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions census/views/create-react.html
Expand Up @@ -22,10 +22,6 @@ <h1>{{format("Make a Submission &ndash; %(sitename)s", { sitename: site.settings

<div id="questions"></div>

<!-- <script src="https://npmcdn.com/react@15.3.0/dist/react.js"></script>
<script src="https://npmcdn.com/react-dom@15.3.0/dist/react-dom.js"></script>
<script src="https://npmcdn.com/babel-core@5.8.38/browser.min.js"></script>
<script src="https://cdn.jsdelivr.net/lodash/3.10.1/lodash.min.js"></script> -->
<script src="/src/bundle.js" type="text/javascript"></script>

{% endblock %}
29 changes: 17 additions & 12 deletions scripts/app/QuestionForm.jsx
Expand Up @@ -70,7 +70,10 @@ const QuestionForm = React.createClass({
/*
Return visible properties (required, enabled, visible) for the question
with `id`. Value of the properties depends on the value of other
dependent questions, based on the question schema.
provider questions, based on the question schema.
For the sake of clarity, `dependant` objects depend on `provider`
objects.
*/

// Get the schema for this id
Expand All @@ -79,22 +82,24 @@ const QuestionForm = React.createClass({
// Initally set up return value as the defaultProperties for the schema
var visProps = _.cloneDeep(schema.defaultProperties);

// For each `if` object in the schema, get the dependent value
// For each dependency in the `if` array in the schema
_.each(schema.if, dependency => {
// Find the current dependency state
var currentDependency =
// Find the current state of the provider
var currentProviderState =
_.find(this.state.questionState,
qState => qState.id === dependency.dependentId);
// If the actual value of the dependent field is the same as the
// expected value, and the dependent field is enabled and visible...

// ---> Only activate the dependency properties if the dependent object is itself enabled and visible. <---

if (currentDependency.value === dependency.value) {
qState => qState.id === dependency.providerId);

// If the actual value of the provider field is the same as the expected
// value, and the provider field is enabled and visible...
var providerVisProps =
this.getVisiblePropsForId(dependency.providerId);
if (currentProviderState.value === dependency.value &&
providerVisProps.enabled &&
providerVisProps.visible) {
// Update the return value with the dependency properties.
_.assign(visProps, dependency.properties);
}
});
}, this);
return visProps;
},

Expand Down
3 changes: 2 additions & 1 deletion scripts/app/index.jsx
Expand Up @@ -2,10 +2,11 @@ import React from 'react';
import {render} from 'react-dom';
import QuestionForm from './QuestionForm.jsx';

// initial static data for development
// eslint-disable-next-line
let questions = [{'id':'like_apples','text':'Do you like apples?','type':''},{'id':'bananas_instead','text':'Do you like bananas instead?','type':''},{'id':'apple_colour','text':'Do you like *RED* apples?','type':''},{'id':'red_apple_today','text':'Have you eaten a red apple today?','type':''},{'id':'doctor_away','text':'Did it keep the doctor away?','type':''}];
// eslint-disable-next-line
let qsSchema = [{'defaultProperties':{'enabled':true,'required':true,'visible':true},'id':'like_apples','position':1},{'defaultProperties':{'enabled':false,'required':false,'visible':false},'id':'bananas_instead','if':[{'dependentId':'like_apples','properties':{'enabled':true,'required':true,'visible':true},'value':'No'}],'position':1.1},{'defaultProperties':{'enabled':false,'required':false,'visible':true},'id':'apple_colour','if':[{'dependentId':'like_apples','properties':{'enabled':true,'required':true},'value':'Yes'}],'position':2},{'defaultProperties':{'enabled':false,'required':false,'visible':true},'id':'red_apple_today','if':[{'dependentId':'apple_colour','properties':{'enabled':true,'required':true},'value':'Yes'}],'position':3},{'defaultProperties':{'enabled':false,'required':false,'visible':false},'id':'doctor_away','if':[{'dependentId':'red_apple_today','properties':{'enabled':true,'visible':true},'value':'Yes'}],'position':3.1}];
let qsSchema = [{'defaultProperties':{'enabled':true,'required':true,'visible':true},'id':'like_apples','position':1},{'defaultProperties':{'enabled':false,'required':false,'visible':false},'id':'bananas_instead','if':[{'providerId':'like_apples','properties':{'enabled':true,'required':true,'visible':true},'value':'No'}],'position':1.1},{'defaultProperties':{'enabled':false,'required':false,'visible':true},'id':'apple_colour','if':[{'providerId':'like_apples','properties':{'enabled':true,'required':true},'value':'Yes'}],'position':2},{'defaultProperties':{'enabled':false,'required':false,'visible':true},'id':'red_apple_today','if':[{'providerId':'apple_colour','properties':{'enabled':true,'required':true},'value':'Yes'}],'position':3},{'defaultProperties':{'enabled':false,'required':false,'visible':false},'id':'doctor_away','if':[{'providerId':'red_apple_today','properties':{'enabled':true,'visible':true},'value':'Yes'}],'position':3.1}];

render(<QuestionForm questions={questions} qsSchema={qsSchema} />,
document.getElementById('questions'));

0 comments on commit f837c9b

Please sign in to comment.