From 8d36443a8243566b053f69c2313ecf30e375752f Mon Sep 17 00:00:00 2001 From: Nikita Date: Fri, 22 Nov 2024 19:14:23 +0200 Subject: [PATCH 1/5] fix(question): fix min/max fatal error --- .../(main)/questions/components/numeric_question_input.tsx | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/front_end/src/app/(main)/questions/components/numeric_question_input.tsx b/front_end/src/app/(main)/questions/components/numeric_question_input.tsx index 27874fdc6a..2cd84a6fa2 100644 --- a/front_end/src/app/(main)/questions/components/numeric_question_input.tsx +++ b/front_end/src/app/(main)/questions/components/numeric_question_input.tsx @@ -134,6 +134,9 @@ const NumericQuestionInput: React.FC<{ } } if (min !== undefined && max !== undefined) { + if (isNaN(min) || isNaN(max)) { + current_errors.push("Provide correct min and max values"); + } if (min >= max) { current_errors.push("Minimum value should be less than maximum value"); } @@ -235,7 +238,7 @@ const NumericQuestionInput: React.FC<{ type="datetime-local" className="w-full rounded border border-gray-500 px-3 py-2 text-base dark:border-gray-500-dark dark:bg-blue-50-dark" defaultValue={ - min !== undefined && !Number.isNaN(min) + !isNil(min) && !Number.isNaN(min) ? format(new Date(min * 1000), "yyyy-MM-dd'T'HH:mm") : undefined } @@ -252,7 +255,7 @@ const NumericQuestionInput: React.FC<{ type="datetime-local" className="w-full rounded border border-gray-500 px-3 py-2 text-base dark:border-gray-500-dark dark:bg-blue-50-dark" defaultValue={ - max !== undefined && !Number.isNaN(max) + !isNil(max) && !Number.isNaN(max) ? format(new Date(max * 1000), "yyyy-MM-dd'T'HH:mm") : undefined } From c7d8a5bc44cb8a4fad45b0acd2d1fc1e65cd288f Mon Sep 17 00:00:00 2001 From: Nikita Date: Mon, 25 Nov 2024 16:32:27 +0200 Subject: [PATCH 2/5] fix(question): use DatatimeUtc component for question creation --- .../resolution/resolution_modal.tsx | 5 +++- .../questions/components/group_form.tsx | 7 ++++-- .../components/numeric_question_input.tsx | 24 ++++++++++--------- front_end/src/components/ui/datetime_utc.tsx | 3 ++- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/front_end/src/app/(main)/questions/[id]/components/forecast_maker/resolution/resolution_modal.tsx b/front_end/src/app/(main)/questions/[id]/components/forecast_maker/resolution/resolution_modal.tsx index 4fa52d9843..7b284f5df8 100644 --- a/front_end/src/app/(main)/questions/[id]/components/forecast_maker/resolution/resolution_modal.tsx +++ b/front_end/src/app/(main)/questions/[id]/components/forecast_maker/resolution/resolution_modal.tsx @@ -152,7 +152,10 @@ const QuestionResolutionModal: FC = ({ isOpen, onClose, question }) => { buttons={unambiguousOptions} onChange={(value) => { setValue("unambiguousType", value); - value !== "knownValue" && setValue("resolutionValue", value); + value !== "knownValue" + ? setValue("resolutionValue", value) + : setValue("resolutionValue", undefined); + console.log(value); }} variant="tertiary" /> diff --git a/front_end/src/app/(main)/questions/components/group_form.tsx b/front_end/src/app/(main)/questions/components/group_form.tsx index f9e0e26370..2fc18ffe08 100644 --- a/front_end/src/app/(main)/questions/components/group_form.tsx +++ b/front_end/src/app/(main)/questions/components/group_form.tsx @@ -156,7 +156,9 @@ const GroupForm: React.FC = ({ }; } else if (subtype === QuestionType.Date) { if (x.scaling.range_max === null || x.scaling.range_min === null) { - setError("Please enter a max or min value for numeric questions"); + setError( + "Please enter a range_max and range_min value for date questions" + ); break_out = true; return; } @@ -460,7 +462,7 @@ const GroupForm: React.FC = ({ {collapsedSubQuestions[index] && (
-
+
= ({ hasForecasts={ subquestionHasForecasts && mode !== "create" } + chartWidth={720} onChange={({ min: range_min, max: range_max, diff --git a/front_end/src/app/(main)/questions/components/numeric_question_input.tsx b/front_end/src/app/(main)/questions/components/numeric_question_input.tsx index 2cd84a6fa2..086a87fcff 100644 --- a/front_end/src/app/(main)/questions/components/numeric_question_input.tsx +++ b/front_end/src/app/(main)/questions/components/numeric_question_input.tsx @@ -4,6 +4,7 @@ import dynamic from "next/dynamic"; import { useEffect, useRef, useState } from "react"; import Checkbox from "@/components/ui/checkbox"; +import DatetimeUtc from "@/components/ui/datetime_utc"; import { Input } from "@/components/ui/form_field"; import { QuestionWithNumericForecasts } from "@/types/question"; import { QuestionType } from "@/types/question"; @@ -36,6 +37,7 @@ const NumericQuestionInput: React.FC<{ defaultOpenLowerBound: boolean | undefined | null; defaultZeroPoint: number | undefined | null; hasForecasts: boolean; + chartWidth?: number; }> = ({ onChange, questionType, @@ -45,6 +47,7 @@ const NumericQuestionInput: React.FC<{ defaultOpenLowerBound, defaultZeroPoint, hasForecasts, + chartWidth = 800, }) => { const [errors, setError] = useState([]); const [max, setMax] = useState(defaultMax); @@ -232,35 +235,34 @@ const NumericQuestionInput: React.FC<{
Min - { - setMin(new Date(e.target.value).getTime() / 1000); + onChange={(dateString) => { + setMin(new Date(dateString).getTime() / 1000); }} />
Max - { - setMax(new Date(e.target.value).getTime() / 1000); + onChange={(dateString) => { + setMax(new Date(dateString).getTime() / 1000); }} />
@@ -415,7 +417,7 @@ const NumericQuestionInput: React.FC<{ question={question} readOnly={false} height={100} - width={800} + width={chartWidth} showCP={false} /> diff --git a/front_end/src/components/ui/datetime_utc.tsx b/front_end/src/components/ui/datetime_utc.tsx index 5b6eef001e..fe22e56d02 100644 --- a/front_end/src/components/ui/datetime_utc.tsx +++ b/front_end/src/components/ui/datetime_utc.tsx @@ -1,4 +1,5 @@ import { format, formatISO, parseISO } from "date-fns"; +import { isNil } from "lodash"; import React, { ChangeEvent, InputHTMLAttributes, @@ -29,7 +30,7 @@ const DatetimeUtc: React.FC = ({ const [localValue, setLocalValue] = useState(""); useEffect(() => { - if (defaultValue) { + if (!isNil(defaultValue)) { // Convert stored UTC value to local time for rendering const localDate = parseISO(defaultValue); const localDateString = format(localDate, "yyyy-MM-dd'T'HH:mm"); From 650d3f8069966ab015a1f318ca98e2744539c091 Mon Sep 17 00:00:00 2001 From: Nikita Date: Mon, 25 Nov 2024 16:32:33 +0200 Subject: [PATCH 3/5] fix(datetime): add check for invalid date --- front_end/src/components/ui/datetime_utc.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/front_end/src/components/ui/datetime_utc.tsx b/front_end/src/components/ui/datetime_utc.tsx index fe22e56d02..f4314954ae 100644 --- a/front_end/src/components/ui/datetime_utc.tsx +++ b/front_end/src/components/ui/datetime_utc.tsx @@ -33,6 +33,8 @@ const DatetimeUtc: React.FC = ({ if (!isNil(defaultValue)) { // Convert stored UTC value to local time for rendering const localDate = parseISO(defaultValue); + if (isNaN(localDate.getTime())) return; + const localDateString = format(localDate, "yyyy-MM-dd'T'HH:mm"); setLocalValue(localDateString); } From 7ac35b41375442e9d87bb0b2748c81599f177f44 Mon Sep 17 00:00:00 2001 From: Nikita Date: Mon, 25 Nov 2024 16:32:39 +0200 Subject: [PATCH 4/5] fix(codestyle): remove comments and logs --- .../components/forecast_maker/resolution/resolution_modal.tsx | 1 - .../app/(main)/questions/components/numeric_question_input.tsx | 1 - 2 files changed, 2 deletions(-) diff --git a/front_end/src/app/(main)/questions/[id]/components/forecast_maker/resolution/resolution_modal.tsx b/front_end/src/app/(main)/questions/[id]/components/forecast_maker/resolution/resolution_modal.tsx index 7b284f5df8..5f00db1620 100644 --- a/front_end/src/app/(main)/questions/[id]/components/forecast_maker/resolution/resolution_modal.tsx +++ b/front_end/src/app/(main)/questions/[id]/components/forecast_maker/resolution/resolution_modal.tsx @@ -155,7 +155,6 @@ const QuestionResolutionModal: FC = ({ isOpen, onClose, question }) => { value !== "knownValue" ? setValue("resolutionValue", value) : setValue("resolutionValue", undefined); - console.log(value); }} variant="tertiary" /> diff --git a/front_end/src/app/(main)/questions/components/numeric_question_input.tsx b/front_end/src/app/(main)/questions/components/numeric_question_input.tsx index 086a87fcff..5e14e617b7 100644 --- a/front_end/src/app/(main)/questions/components/numeric_question_input.tsx +++ b/front_end/src/app/(main)/questions/components/numeric_question_input.tsx @@ -243,7 +243,6 @@ const NumericQuestionInput: React.FC<{ !isNil(min) && !Number.isNaN(min) ? new Date(min * 1000).toISOString() : undefined - // ? format(new Date(min * 1000), "yyyy-MM-dd'T'HH:mm") } onChange={(dateString) => { setMin(new Date(dateString).getTime() / 1000); From 49c0465f6fd913b490d04a67e93dae9443501700 Mon Sep 17 00:00:00 2001 From: Nikita Date: Mon, 25 Nov 2024 16:39:02 +0200 Subject: [PATCH 5/5] fix(question): DatetimeUtc types and numeric question input adjustments --- .../components/numeric_question_input.tsx | 18 +++++++-------- front_end/src/components/ui/datetime_utc.tsx | 22 +++++++------------ 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/front_end/src/app/(main)/questions/components/numeric_question_input.tsx b/front_end/src/app/(main)/questions/components/numeric_question_input.tsx index 5e14e617b7..abf3da6b25 100644 --- a/front_end/src/app/(main)/questions/components/numeric_question_input.tsx +++ b/front_end/src/app/(main)/questions/components/numeric_question_input.tsx @@ -314,8 +314,7 @@ const NumericQuestionInput: React.FC<{ }} checked={zeroPoint !== null && zeroPoint !== undefined} /> - {zeroPoint !== null && - zeroPoint !== undefined && + {!isNil(zeroPoint) && (questionType == QuestionType.Numeric ? (
Zero Point @@ -332,17 +331,16 @@ const NumericQuestionInput: React.FC<{ ) : (
Zero Point - { - setZeroPoint(new Date(e.target.value).getTime() / 1000); + className="w-full rounded border border-gray-500 px-3 py-2 text-base dark:border-gray-500-dark dark:bg-blue-50-dark" + defaultValue={new Date( + !Number.isNaN(zeroPoint) ? zeroPoint * 1000 : 0 + ).toISOString()} + onChange={(dateString) => { + setZeroPoint(new Date(dateString).getTime() / 1000); }} - defaultValue={format( - new Date(!Number.isNaN(zeroPoint) ? zeroPoint * 1000 : 0), - "yyyy-MM-dd'T'HH:mm" - )} />
))} diff --git a/front_end/src/components/ui/datetime_utc.tsx b/front_end/src/components/ui/datetime_utc.tsx index f4314954ae..1eea04d565 100644 --- a/front_end/src/components/ui/datetime_utc.tsx +++ b/front_end/src/components/ui/datetime_utc.tsx @@ -1,17 +1,11 @@ import { format, formatISO, parseISO } from "date-fns"; import { isNil } from "lodash"; -import React, { - ChangeEvent, - InputHTMLAttributes, - useEffect, - useState, -} from "react"; +import React, { ChangeEvent, useEffect, useState } from "react"; -import { Input } from "@/components/ui/form_field"; +import { Input, InputProps } from "@/components/ui/form_field"; import { logError } from "@/utils/errors"; -interface DatetimeUtcProps - extends Omit, "onChange"> { +interface DatetimeUtcProps extends Omit { defaultValue?: string; onChange?: (value: string) => void; onError?: (error: any) => void; @@ -46,12 +40,12 @@ const DatetimeUtc: React.FC = ({ try { // Convert local time to UTC for storage - const localDate = new Date(localDateString); - const utcDateString = formatISO(localDate, { - representation: "complete", - }); - if (onChange) { + const localDate = new Date(localDateString); + const utcDateString = formatISO(localDate, { + representation: "complete", + }); + onChange(utcDateString); } } catch (e) {