Skip to content

Commit

Permalink
Merge pull request #62 from EyeSeeTea/feature/date-fix
Browse files Browse the repository at this point in the history
Feature/date fix
  • Loading branch information
MiquelAdell authored Jul 4, 2024
2 parents 12e4484 + 118d175 commit fbc552f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
11 changes: 7 additions & 4 deletions src/data/utils/questionHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/domain/entities/Questionnaire/QuestionnaireQuestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export interface DateQuestion extends QuestionBase {

export interface DateTimeQuestion extends QuestionBase {
type: "datetime";
value: Maybe<string>;
value: Maybe<Date>;
}

export interface QuestionOption extends NamedRef {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ export interface DatePickerWidgetProps extends BaseWidgetProps<Date> {
const DatePickerWidget: React.FC<DatePickerWidgetProps> = props => {
const { onChange: onValueChange, value } = props;

const [stateValue, setStateValue] = React.useState(value);
const [stateValue, setStateValue] = React.useState<string>(value);
React.useEffect(() => setStateValue(value), [value]);

const notifyChange = React.useCallback(
(newValue: Date) => {
(newValue: Maybe<Date>) => {
setStateValue(newValue);
onValueChange(newValue);
onValueChange(newValue || "");
},
[onValueChange]
);
return (
<LocalizationProvider dateAdapter={AdapterDateFns}>
<DatePicker
key={props.name}
value={stateValue}
value={stateValue ? new Date(stateValue) : undefined}
disabled={props.disabled}
onChange={newValue => notifyChange(newValue)}
format="dd-MM-yyyy"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@ 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<string> {
value: Maybe<string>;
export interface DateTimePickerWidgetProps extends BaseWidgetProps<Date> {
value: Maybe<Date>;
name: string;
}

const DateTimePickerWidget: React.FC<DateTimePickerWidgetProps> = props => {
const { onChange: onValueChange, value } = props;

const [stateValue, setStateValue] = React.useState(value);
const [stateValue, setStateValue] = React.useState<string>(value);
React.useEffect(() => setStateValue(value), [value]);

const notifyChange = React.useCallback(
(newValue: string) => {
(newValue: Maybe<Date>) => {
setStateValue(newValue);
onValueChange(newValue);
onValueChange(newValue || "");
},
[onValueChange]
);
return (
<LocalizationProvider dateAdapter={AdapterDateFns}>
<DateTimePicker
key={props.name}
value={new Date(stateValue)}
value={stateValue ? new Date(stateValue) : undefined}
disabled={props.disabled}
onChange={newValue => notifyChange(newValue?.toISOString() ?? "")}
onChange={newValue => notifyChange(newValue)}
ampm={false}
format="dd-MM-yyyy HH:mm"
/>
Expand Down

0 comments on commit fbc552f

Please sign in to comment.