From 118d175741634359bb490d9b3f4512e78c3d5aff Mon Sep 17 00:00:00 2001 From: 9sneha-n <9sneha.n@gmail.com> Date: Mon, 1 Jul 2024 13:28:12 +0530 Subject: [PATCH] feat: date fix --- src/data/utils/questionHelper.ts | 11 +++++++---- .../Questionnaire/QuestionnaireQuestion.ts | 2 +- .../survey-questions/widgets/DatePickerWidget.tsx | 8 ++++---- .../widgets/DateTimePickerWidget.tsx | 14 +++++++------- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/data/utils/questionHelper.ts b/src/data/utils/questionHelper.ts index 99d4418..a9e7cd1 100644 --- a/src/data/utils/questionHelper.ts +++ b/src/data/utils/questionHelper.ts @@ -173,7 +173,7 @@ export const getQuestion = ( const dateQ: DateQuestion = { ...base, type: "date", - value: dataValue ? new Date(dataValue as string) : undefined, + value: dataValue ? new Date(dataValue) : undefined, }; return dateQ; } @@ -182,7 +182,7 @@ export const getQuestion = ( const dateQ: DateTimeQuestion = { ...base, type: "datetime", - value: dataValue ? new Date(dataValue as string).toISOString() : undefined, + value: dataValue ? new Date(dataValue) : undefined, }; return dateQ; } @@ -198,10 +198,13 @@ export const mapQuestionsToDataValues = (questions: Question[]): DataValue[] => dataElement: question.id, value: question.value.code, }; - } else if (question.type === "date" && question.value) { + } else if ( + (question.type === "date" || question.type === "datetime") && + question.value + ) { return { dataElement: question.id, - value: question.value.toISOString().split("T")?.at(0) || "", + value: question.value.toISOString(), }; } else if (question.type === "boolean" && question.storeFalse === false) { return { diff --git a/src/domain/entities/Questionnaire/QuestionnaireQuestion.ts b/src/domain/entities/Questionnaire/QuestionnaireQuestion.ts index d2dac2e..32a65f3 100644 --- a/src/domain/entities/Questionnaire/QuestionnaireQuestion.ts +++ b/src/domain/entities/Questionnaire/QuestionnaireQuestion.ts @@ -118,7 +118,7 @@ export interface DateQuestion extends QuestionBase { export interface DateTimeQuestion extends QuestionBase { type: "datetime"; - value: Maybe; + value: Maybe; } export interface QuestionOption extends NamedRef { diff --git a/src/webapp/components/survey-questions/widgets/DatePickerWidget.tsx b/src/webapp/components/survey-questions/widgets/DatePickerWidget.tsx index 8922de3..f3f0555 100644 --- a/src/webapp/components/survey-questions/widgets/DatePickerWidget.tsx +++ b/src/webapp/components/survey-questions/widgets/DatePickerWidget.tsx @@ -13,13 +13,13 @@ export interface DatePickerWidgetProps extends BaseWidgetProps { const DatePickerWidget: React.FC = props => { const { onChange: onValueChange, value } = props; - const [stateValue, setStateValue] = React.useState(value); + const [stateValue, setStateValue] = React.useState(value); React.useEffect(() => setStateValue(value), [value]); const notifyChange = React.useCallback( - (newValue: Date) => { + (newValue: Maybe) => { setStateValue(newValue); - onValueChange(newValue); + onValueChange(newValue || ""); }, [onValueChange] ); @@ -27,7 +27,7 @@ const DatePickerWidget: React.FC = props => { notifyChange(newValue)} format="dd-MM-yyyy" diff --git a/src/webapp/components/survey-questions/widgets/DateTimePickerWidget.tsx b/src/webapp/components/survey-questions/widgets/DateTimePickerWidget.tsx index 8f53cb5..43fc386 100644 --- a/src/webapp/components/survey-questions/widgets/DateTimePickerWidget.tsx +++ b/src/webapp/components/survey-questions/widgets/DateTimePickerWidget.tsx @@ -6,21 +6,21 @@ import { DateTimePicker } from "@mui/x-date-pickers"; import { AdapterDateFns } from "@mui/x-date-pickers/AdapterDateFns"; import { LocalizationProvider } from "@mui/x-date-pickers"; -export interface DateTimePickerWidgetProps extends BaseWidgetProps { - value: Maybe; +export interface DateTimePickerWidgetProps extends BaseWidgetProps { + value: Maybe; name: string; } const DateTimePickerWidget: React.FC = props => { const { onChange: onValueChange, value } = props; - const [stateValue, setStateValue] = React.useState(value); + const [stateValue, setStateValue] = React.useState(value); React.useEffect(() => setStateValue(value), [value]); const notifyChange = React.useCallback( - (newValue: string) => { + (newValue: Maybe) => { setStateValue(newValue); - onValueChange(newValue); + onValueChange(newValue || ""); }, [onValueChange] ); @@ -28,9 +28,9 @@ const DateTimePickerWidget: React.FC = props => { notifyChange(newValue?.toISOString() ?? "")} + onChange={newValue => notifyChange(newValue)} ampm={false} format="dd-MM-yyyy HH:mm" />