From 6ee2ae46ad4fbb91f41078c4cec2885211594f2e Mon Sep 17 00:00:00 2001 From: Brook Elgie Date: Fri, 28 Oct 2016 14:59:27 +0100 Subject: [PATCH] [#792] Current Entry values alongside form questions. New submissions and entry review form now displays a current value for each question, if one exists. --- census/controllers/census.js | 14 +++++++++++--- census/controllers/utils.js | 19 ++++++++++++++----- census/ui_app/EntryForm.jsx | 4 ++-- census/ui_app/HelperFields.jsx | 28 +++++++++++++++++++++------- census/ui_app/QuestionFields.jsx | 25 +++++++++++++++++++------ census/ui_app/QuestionForm.jsx | 4 +++- 6 files changed, 70 insertions(+), 24 deletions(-) diff --git a/census/controllers/census.js b/census/controllers/census.js index c1198b43..ba7f3494 100644 --- a/census/controllers/census.js +++ b/census/controllers/census.js @@ -40,6 +40,7 @@ var submitGet = function(req, res, data) { } Promise.join(qsSchemaPromise, questionsPromise, (qsSchema, questions) => { if (qsSchema === undefined) qsSchema = []; + let currentAnswers = _.get(data.currentState.match, 'answers'); questions = _.map(questions, question => { return { id: question.id, @@ -49,7 +50,8 @@ var submitGet = function(req, res, data) { description: nunjucks.renderString(question.description, {datasetContext: datasetContext}), placeholder: question.placeholder, - config: question.config + config: question.config, + currentValue: _.get(_.find(currentAnswers, {id: question.id}), 'value', '') }; }); // We might have form data to prefill the EntryForm with. @@ -207,7 +209,8 @@ var submit = function(req, res) { }); modelUtils.getData(dataOptions) .then(data => { - data.currentState = utils.getCurrentState(data, req); + let match = _.merge(req.query, req.body); + data.currentState = utils.getCurrentState(data, match, req.params.year); if (req.method === 'POST') { submitPost(req, res, data); } else { @@ -258,6 +261,9 @@ var pending = function(req, res) { Promise.join(qsSchemaPromise, questionsPromise, (qsSchema, questions) => { if (qsSchema === undefined) qsSchema = []; + let match = {place: place.id, dataset: dataset.id}; + data.currentState = utils.getCurrentState(data, match, req.params.year); + let currentAnswers = _.get(data.currentState.match, 'answers'); questions = _.map(questions, question => { return { id: question.id, @@ -267,7 +273,9 @@ var pending = function(req, res) { description: nunjucks.renderString(question.description, {datasetContext: datasetContext}), placeholder: question.placeholder, - config: question.config + config: question.config, + currentValue: _.get(_.find(currentAnswers, {id: question.id}), + 'value', '') }; }); // Prefill the EntryForm with entry data. diff --git a/census/controllers/utils.js b/census/controllers/utils.js index 448507d9..64d90806 100644 --- a/census/controllers/utils.js +++ b/census/controllers/utils.js @@ -283,10 +283,19 @@ var getFormQuestions = function(req, questions) { return _.sortByOrder(questions, 'order', 'asc'); }; -var getCurrentState = function(data, req) { - var match = _.merge(req.query, req.body); - var pending; - var matches; +var getCurrentState = function(data, match, year) { + /* + Return an object containing the state of submissions for a given + place/dataset/year. + + Returned object has `match` and `pending` properties: + `match`: the entry that isCurrent for the given place/dataset in the + `match` param obj. + `pending`: a boolean to detemine whether there's a pending entry for the + place/dataset/year. + */ + let pending; + let matches; if (!match.place || !match.dataset) { match = {}; @@ -298,7 +307,7 @@ var getCurrentState = function(data, req) { }); pending = _.any(data.pending, { isCurrent: false, - year: req.params.year, + year: year, place: match.place, dataset: match.dataset }); diff --git a/census/ui_app/EntryForm.jsx b/census/ui_app/EntryForm.jsx index 0aa1a6c8..c8e1bcaa 100644 --- a/census/ui_app/EntryForm.jsx +++ b/census/ui_app/EntryForm.jsx @@ -173,7 +173,7 @@ const EntryForm = React.createClass({

Any other comments?

-
+
@@ -35,8 +50,7 @@ const SubmitActions = props => {

-
-
+
@@ -60,8 +74,7 @@ const SubmitActions = props => {

By submitting material to the index you agreeing to terms of use and also to license your contribution (to the extent there are any rights in it!) under the Open Data Commons Public Domain Dedication and License.

-
-
+
@@ -129,5 +142,6 @@ export { QuestionInstructions, QuestionComments, QuestionHeader, - SubmitActions + SubmitActions, + CurrentEntry }; diff --git a/census/ui_app/QuestionFields.jsx b/census/ui_app/QuestionFields.jsx index a284f9a8..148772cc 100644 --- a/census/ui_app/QuestionFields.jsx +++ b/census/ui_app/QuestionFields.jsx @@ -48,7 +48,7 @@ let QuestionFieldText = React.createClass({
-
+
-
+
-
+
{scaleOptionNodes} @@ -226,6 +226,15 @@ let QuestionFieldSource = React.createClass({ return sourceValues; }, + componentWillMount() { + // Split the current value object up into a list for display by + // helpers.CurrentEntry. + this.currentValue = _.filter(this.props.currentValue, val => + (val.urlValue || val.descValue)); + this.currentValue = _.map(this.currentValue, (val, i) => +
  • {val.urlValue}
  • {val.descValue}
); + }, + render() { let sourceLines = []; let sourceValues = this._getSourceValues(); @@ -250,7 +259,7 @@ let QuestionFieldSource = React.createClass({
-
+
{sourceLines} @@ -339,10 +348,11 @@ let QuestionFieldMultipleChoice = React.createClass({ // Merge the defaultOptions with those from the value in props to // get the value store we'll use for the render. this.optionValues = _.assign(defaultOptions, this.props.value); + this.orderOptions = _.get(this.props.config, 'orderOptions', false); }, render() { - if (_.get(this.props.config, 'orderOptions', false)) { + if (this.orderOptions) { this.optionValues = _.sortBy(this.optionValues, 'description'); } let choices = _.map(this.optionValues, (option, i) => { @@ -357,6 +367,9 @@ let QuestionFieldMultipleChoice = React.createClass({ {option.description} ; }); + let currentValue = _.filter(this.props.currentValue, option => option.checked); + currentValue = _.map(currentValue, option => option.description); + if (this.orderOptions) currentValue = _.sortBy(currentValue); return (
@@ -367,7 +380,7 @@ let QuestionFieldMultipleChoice = React.createClass({
-
+
    diff --git a/census/ui_app/QuestionForm.jsx b/census/ui_app/QuestionForm.jsx index 589f6723..a0023ab6 100644 --- a/census/ui_app/QuestionForm.jsx +++ b/census/ui_app/QuestionForm.jsx @@ -17,7 +17,8 @@ const QuestionForm = React.createClass({ return { id: q.id, value: _.get(answer, 'value', ''), - commentValue: _.get(answer, 'commentValue', '') + commentValue: _.get(answer, 'commentValue', ''), + currentValue: _.get(q, 'currentValue', '') }; }); return { @@ -162,6 +163,7 @@ const QuestionForm = React.createClass({ visibleProps={this.getVisiblePropsForId(q.id)} value={q.value} commentValue={q.commentValue} + currentValue={q.currentValue} onChange={this.onFieldChange} onCommentChange={this.onCommentChange} label={this.getLabelForId(q.id)}