Skip to content

Commit 297977a

Browse files
authored
Merge pull request #248 from HarvardOpenData/kh-predictions-continuous
Integrate "continuous" range for intervals
2 parents 74f31f2 + 2d400e5 commit 297977a

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

web/src/components/predictions/predictions-game.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const PredictionsGame = ({ user }) => {
5757
}
5858

5959
// render appropriate component for each question
60-
function renderQuestion(question, date_expired, answer, disabled) {
60+
function renderQuestion(question, date_expired, answer, disabled, step=1) {
6161
const qid = question.key;
6262
const prediction = snapshot.child(qid).val();
6363
const choices = question.child("choices").val();
@@ -110,6 +110,7 @@ const PredictionsGame = ({ user }) => {
110110
answer={answer}
111111
lower={choices[0]}
112112
upper={choices[1]}
113+
step={step}
113114
date_expired={date_expired}
114115
prediction={prediction}
115116
explanation={question.child("explanation").val()}
@@ -134,12 +135,14 @@ const PredictionsGame = ({ user }) => {
134135
questions.forEach((question) => {
135136
const date_expired = question.child("date_expired").val();
136137
let answer = question.child("answer").val();
138+
let step = question.child("step").val() === 'continuous' ? 0.1 : 1;
139+
137140
if (answer !== null) {
138141
answer = parseInt(answer);
139142
}
140143

141144
if (new Date(date_expired).getTime() > new Date().getTime()) {
142-
liveQuestions.push(renderQuestion(question, date_expired, answer, false));
145+
liveQuestions.push(renderQuestion(question, date_expired, answer, false, step));
143146
} else if (
144147
new Date(date_expired).getTime() < new Date().getTime() &&
145148
answer == null

web/src/components/predictions/questions/interval-choice.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function IntervalChoice(props) {
4242

4343
const validateLower = (e) => {
4444
const bound = Math.min(values[1], upper);
45-
let value = parseInt(e.target.value);
45+
let value = parseFloat(e.target.value);
4646
if (value < lower || e.target.value === "") {
4747
value = lower;
4848
} else if (value > bound) {
@@ -54,7 +54,7 @@ function IntervalChoice(props) {
5454

5555
const validateUpper = (e) => {
5656
const bound = Math.max(values[0], lower);
57-
let value = parseInt(e.target.value);
57+
let value = parseFloat(e.target.value);
5858
if (value > upper || e.target.value === "") {
5959
value = upper;
6060
} else if (value < bound) {
@@ -95,6 +95,13 @@ function IntervalChoice(props) {
9595
}
9696
}
9797

98+
function update(value, values, step) {
99+
if (step === 1) {
100+
setValues([value && parseInt(value), values[1]])
101+
}
102+
setValues([value && parseFloat(value), values[1]]);
103+
}
104+
98105
return (
99106
<form onSubmit={(event) => afterSubmission(event)}>
100107
<Grid mt={1} mx={3} gap={0} columns={[2, "3fr 5fr"]}>
@@ -152,9 +159,7 @@ function IntervalChoice(props) {
152159
max={upper}
153160
value={values[0]}
154161
disabled={props.disabled}
155-
onChange={(e) =>
156-
setValues([e.target.value && parseInt(e.target.value), values[1]])
157-
}
162+
onChange={(e) => update(e.target.value, values, props.step)}
158163
onBlur={validateLower}
159164
onKeyDown={(e) => {
160165
if (e.key === "Enter") e.target.blur();
@@ -172,9 +177,7 @@ function IntervalChoice(props) {
172177
max={upper}
173178
value={values[1]}
174179
disabled={props.disabled}
175-
onChange={(e) =>
176-
setValues([values[0], e.target.value && parseInt(e.target.value)])
177-
}
180+
onChange={(e) => update(e.target.value, values, props.step)}
178181
onBlur={validateUpper}
179182
onKeyDown={(e) => {
180183
if (e.key === "Enter") e.target.blur();

0 commit comments

Comments
 (0)