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

Commit

Permalink
[#792] Current Entry values alongside form questions.
Browse files Browse the repository at this point in the history
New submissions and entry review form now displays a current value for
each question, if one exists.
  • Loading branch information
brew committed Oct 28, 2016
1 parent 710bb2d commit 6ee2ae4
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 24 deletions.
14 changes: 11 additions & 3 deletions census/controllers/census.js
Expand Up @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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.
Expand Down
19 changes: 14 additions & 5 deletions census/controllers/utils.js
Expand Up @@ -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 = {};
Expand All @@ -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
});
Expand Down
4 changes: 2 additions & 2 deletions census/ui_app/EntryForm.jsx
Expand Up @@ -173,7 +173,7 @@ const EntryForm = React.createClass({
<h2>Any other comments?</h2>
</div>
<div>
<div className="current"></div>
<helpers.CurrentEntry />
<div className="answer-wrapper">
<div className="answer">
<textarea name="details" rows="5"
Expand All @@ -198,7 +198,7 @@ const EntryForm = React.createClass({
<h2>Would you prefer your submission to remain anonymous?</h2>
</div>
<div>
<div className="current"></div>
<helpers.CurrentEntry />
<div className="answer-wrapper">
<div className="answer">
<input type="radio" name="anonymous"
Expand Down
28 changes: 21 additions & 7 deletions census/ui_app/HelperFields.jsx
@@ -1,5 +1,21 @@
import _ from 'lodash';
import React from 'react';

const CurrentEntry = props => {
if (props.currentValue && props.currentValue.length) {
let currentValue = (_.isArray(props.currentValue)) ?
props.currentValue : [props.currentValue];
let dds = _.map(currentValue, (cv, i) => <dd key={i}>{cv}</dd>);
return (<div className="current">
<dl>
<dt>Current entry</dt>
{dds}
</dl>
</div>);
}
return <div className="current" />;
};

const SubmitActions = props => {
if (props.isReview) {
return (<div>
Expand All @@ -10,8 +26,7 @@ const SubmitActions = props => {
<h2>Please add a short comment as to why you are accepting or rejecting this submission. <strong>Note that your message will be publically visible</strong></h2>
</div>
<div>
<div className="current">
</div>
<CurrentEntry />
<div className="answer-wrapper">
<div className="answer">
<textarea name="reviewComments" rows="5" defaultValue={props.reviewComments}></textarea>
Expand All @@ -35,8 +50,7 @@ const SubmitActions = props => {
</p>
</div>
<div>
<div className="current">
</div>
<CurrentEntry />
<div className="answer-wrapper">
<div className="answer">
<form method="post" acceptCharset="utf-8" onSubmit={props.onSubmitHandler}>
Expand All @@ -60,8 +74,7 @@ const SubmitActions = props => {
<p><small>By submitting material to the index you agreeing to <a href="http://okfn.org/terms-of-use/">terms of use</a> and also to license your contribution (to the extent there are any rights in it!) under the <a href="http://opendatacommons.org/licenses/pddl/1.0/">Open Data Commons Public Domain Dedication and License</a>.</small></p>
</div>
<div>
<div className="current">
</div>
<CurrentEntry />
<div className="answer-wrapper">
<div className="answer">
<form method="post" acceptCharset="utf-8" onSubmit={props.onSubmitHandler}>
Expand Down Expand Up @@ -129,5 +142,6 @@ export {
QuestionInstructions,
QuestionComments,
QuestionHeader,
SubmitActions
SubmitActions,
CurrentEntry
};
25 changes: 19 additions & 6 deletions census/ui_app/QuestionFields.jsx
Expand Up @@ -48,7 +48,7 @@ let QuestionFieldText = React.createClass({
</helpers.QuestionHeader>
</div>
<div>
<div className="current"></div>
<helpers.CurrentEntry currentValue={this.props.currentValue} />
<div className="answer-wrapper">
<div className="answer">
<input type="text"
Expand Down Expand Up @@ -86,7 +86,7 @@ let QuestionFieldYesNo = React.createClass({
</helpers.QuestionHeader>
</div>
<div>
<div className="current"></div>
<helpers.CurrentEntry currentValue={this.props.currentValue} />
<div className="answer-wrapper">
<div className="answer">
<input type="radio"
Expand Down Expand Up @@ -166,7 +166,7 @@ let QuestionFieldLikert = React.createClass({
</helpers.QuestionHeader>
</div>
<div>
<div className="current"></div>
<helpers.CurrentEntry currentValue={this.props.currentValue} />
<div className="answer-wrapper">
<div className="answer">
{scaleOptionNodes}
Expand Down Expand Up @@ -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) =>
<ul key={i}><li>{val.urlValue}</li><li>{val.descValue}</li></ul>);
},

render() {
let sourceLines = [];
let sourceValues = this._getSourceValues();
Expand All @@ -250,7 +259,7 @@ let QuestionFieldSource = React.createClass({
</helpers.QuestionHeader>
</div>
<div>
<div className="current"></div>
<helpers.CurrentEntry currentValue={this.currentValue} />
<div className="answer-wrapper">
<div className="answer">
{sourceLines}
Expand Down Expand Up @@ -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) => {
Expand All @@ -357,6 +367,9 @@ let QuestionFieldMultipleChoice = React.createClass({
{option.description}
</QuestionFieldMultipleChoiceOption>;
});
let currentValue = _.filter(this.props.currentValue, option => option.checked);
currentValue = _.map(currentValue, option => option.description);
if (this.orderOptions) currentValue = _.sortBy(currentValue);
return (<div className={'multiple question ' + this.props.getClassValues()}>
<div className="main">
<div>
Expand All @@ -367,7 +380,7 @@ let QuestionFieldMultipleChoice = React.createClass({
</helpers.QuestionHeader>
</div>
<div>
<div className="current"></div>
<helpers.CurrentEntry currentValue={currentValue} />
<div className="answer-wrapper">
<div className={this._getAnswerClassNames()}>
<ul>
Expand Down
4 changes: 3 additions & 1 deletion census/ui_app/QuestionForm.jsx
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)}
Expand Down

0 comments on commit 6ee2ae4

Please sign in to comment.