From 542f288923a8f68db619fd6274930b7bf689b3a2 Mon Sep 17 00:00:00 2001 From: Daniil Palagin Date: Wed, 16 Oct 2024 18:44:00 +0200 Subject: [PATCH] [#363] Replace datetime number format with timestamp format --- src/components/answer/DateTimeAnswer.jsx | 69 +++++++++++------------- src/util/FormUtils.js | 24 ++++----- 2 files changed, 43 insertions(+), 50 deletions(-) diff --git a/src/components/answer/DateTimeAnswer.jsx b/src/components/answer/DateTimeAnswer.jsx index c537b930..e9913d40 100644 --- a/src/components/answer/DateTimeAnswer.jsx +++ b/src/components/answer/DateTimeAnswer.jsx @@ -4,28 +4,13 @@ import { FormGroup, Form } from "react-bootstrap"; import PropTypes from "prop-types"; import { format, parse } from "date-fns"; import FormUtils from "../../util/FormUtils"; -import Constants from "../../constants/Constants"; import { ConfigurationContext } from "../../contexts/ConfigurationContext"; import classNames from "classnames"; const DateTimeAnswer = (props) => { const { componentsOptions } = useContext(ConfigurationContext); const [date, setDate] = useState(null); - - useEffect(() => { - if (props.value) { - const parsedDate = parse(props.value, datePickerFormat, new Date()); - if (!isNaN(parsedDate.getTime())) { - setDate(parsedDate); - } else { - console.error( - `Invalid date value ${props.value} of question ${props.question["@id"]}.` - ); - } - } else { - setDate(null); - } - }, [props.value, datePickerFormat]); + const [errorMessage, setErrorMessage] = useState(""); const dateFormat = FormUtils.resolveDateTimeFormat( props.question, @@ -35,28 +20,38 @@ const DateTimeAnswer = (props) => { const isDate = FormUtils.isDate(props.question); const isTime = FormUtils.isTime(props.question); - const isTimestamp = FormUtils.isTimestamp(props.question); - - // DatePicker does not know dateFormat "x", translate to datetime - const datePickerFormat = - dateFormat === "x" - ? componentsOptions.dateTimeAnswer.dateTimeFormat - : dateFormat; - - const handleDateChange = (date) => { - setDate(date); - if (!date) { - props.onChange(""); - } + const isTimestampFormat = FormUtils.isTimestamp(props.question); - let timeValue; - if (isTimestamp) { - timeValue = date.getTime(); - } else if (dateFormat === Constants.DATETIME_NUMBER_FORMAT) { - timeValue = Number(date); + useEffect(() => { + const parsedDate = parseDate(props.value); + if (parsedDate) { + setDate(parsedDate); } else { - timeValue = format(date, dateFormat); + setDate(null); + setErrorMessage( + `Invalid date value ${props.value} of question ${props.question["@id"]}.` + ); + console.error(errorMessage); } + }, [props.value, dateFormat]); + + const parseDate = (value) => { + if (!value) return null; + + const parsed = isTimestampFormat + ? new Date(value) + : parse(value, dateFormat, new Date()); + return isNaN(parsed.getTime()) ? null : parsed; + }; + + const handleDateChange = (selectedDate) => { + setDate(selectedDate); + const timeValue = selectedDate + ? isTimestampFormat + ? selectedDate.getTime() + : format(selectedDate, dateFormat) + : ""; + props.onChange(timeValue); }; @@ -65,14 +60,14 @@ const DateTimeAnswer = (props) => { {props.label}