Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[115] Update formik datepicker date parsing method to avoid timezone issues #133

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions verification/curator-service/ui/src/api/models/Day0Case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,26 @@ export interface Location {
}

export interface Events {
dateEntry: string | null;
dateReported: string | null;
dateLastModified: string | null;
dateOnset?: string | null;
dateConfirmation?: string | null;
dateEntry: Date | string | null;
dateReported: Date | string | null;
dateLastModified: Date | string | null;
dateOnset?: Date | string | null;
dateConfirmation?: Date | string | null;
confirmationMethod?: string;
dateOfFirstConsult?: string | null;
dateOfFirstConsult?: Date | string | null;
hospitalized?: YesNo | '';
reasonForHospitalization?: HospitalizationReason | '';
dateHospitalization?: string | null;
dateDischargeHospital?: string | null;
dateHospitalization?: Date | string | null;
dateDischargeHospital?: Date | string | null;
intensiveCare?: YesNo | '';
dateAdmissionICU?: string | null;
dateDischargeICU?: string | null;
dateAdmissionICU?: Date | string | null;
dateDischargeICU?: Date | string | null;
homeMonitoring?: YesNo | '';
isolated?: YesNo | '';
dateIsolation?: string | null;
dateIsolation?: Date | string | null;
outcome?: Outcome | '';
dateDeath?: string | null;
dateRecovered?: string | null;
dateDeath?: Date | string | null;
dateRecovered?: Date | string | null;
}

export interface PreexistingConditions {
Expand All @@ -134,7 +134,7 @@ export interface Transmission {

export interface TravelHistory {
travelHistory?: YesNo | '';
travelHistoryEntry?: string | null;
travelHistoryEntry?: Date | string | null;
travelHistoryStart?: string;
travelHistoryLocation?: string;
travelHistoryCountry?: string;
Expand Down Expand Up @@ -237,7 +237,7 @@ interface PreexistingConditionsFormValues {
interface VaccinationFormValues {
vaccination?: YesNo | '';
vaccineName?: string;
vaccineDate?: string | null;
vaccineDate?: Date | string | null;
vaccineSideEffects?: string[];
}

Expand Down
46 changes: 16 additions & 30 deletions verification/curator-service/ui/src/components/CaseForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ const initialValuesFromCase = (
},
events: {
...c.events,
dateEntry: toLocalDate(c.events.dateEntry),
dateReported: toLocalDate(c.events.dateReported),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason not to report in UTC everywhere? I can see the value of timezones in data collection, less so for reporting.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do store all of the dates in the UTC values, however the component for selecting date on the front-end automatically adjusts date in UTC to local time, thus making the local-date itself. This was an issue as the users in "minus" timezones experienced an issue where they see a date set to a day before.
It can be solved by parsing the UTC date to a local date before by removing the time component (dayjs(dateString.split('T')[0]).toDate()), which will solve the issue with "skipping days" in certain timezones.

dateOnset: toLocalDate(c.events.dateOnset),
dateConfirmation: toLocalDate(c.events.dateConfirmation),
dateOfFirstConsult: toLocalDate(c.events.dateOfFirstConsult),
Expand Down Expand Up @@ -420,45 +422,31 @@ export default function CaseForm(props: Props): JSX.Element {
},
events: {
...values.events,
dateEntry:
toUTCDate(values.events.dateEntry || undefined) || null,
dateReported:
toUTCDate(values.events.dateReported || undefined) || null,
dateOnset: toUTCDate(values.events.dateOnset || undefined),
dateConfirmation: toUTCDate(
values.events.dateConfirmation || undefined,
),
dateEntry: toUTCDate(values.events.dateEntry) || null,
dateReported: toUTCDate(values.events.dateReported) || null,
dateOnset: toUTCDate(values.events.dateOnset),
dateConfirmation: toUTCDate(values.events.dateConfirmation),
confirmationMethod:
values.events.confirmationMethod || undefined,
dateOfFirstConsult: toUTCDate(
values.events.dateOfFirstConsult || undefined,
),
dateOfFirstConsult: toUTCDate(values.events.dateOfFirstConsult),
hospitalized: values.events.hospitalized || undefined,
reasonForHospitalization:
values.events.reasonForHospitalization || undefined,
dateHospitalization: toUTCDate(
values.events.dateHospitalization || undefined,
values.events.dateHospitalization,
),
dateDischargeHospital: toUTCDate(
values.events.dateDischargeHospital || undefined,
values.events.dateDischargeHospital,
),
intensiveCare: values.events.intensiveCare || undefined,
dateAdmissionICU: toUTCDate(
values.events.dateAdmissionICU || undefined,
),
dateDischargeICU: toUTCDate(
values.events.dateDischargeICU || undefined,
),
dateAdmissionICU: toUTCDate(values.events.dateAdmissionICU),
dateDischargeICU: toUTCDate(values.events.dateDischargeICU),
homeMonitoring: values.events.homeMonitoring || undefined,
isolated: values.events.isolated || undefined,
dateIsolation: toUTCDate(
values.events.dateIsolation || undefined,
),
dateIsolation: toUTCDate(values.events.dateIsolation),
outcome: values.events.outcome || undefined,
dateDeath: toUTCDate(values.events.dateDeath || undefined),
dateRecovered: toUTCDate(
values.events.dateRecovered || undefined,
),
dateDeath: toUTCDate(values.events.dateDeath),
dateRecovered: toUTCDate(values.events.dateRecovered),
},
preexistingConditions: {
...values.preexistingConditions,
Expand All @@ -472,9 +460,7 @@ export default function CaseForm(props: Props): JSX.Element {
...values.vaccination,
vaccineSideEffects: vaccineSideEffects.join(', '),
vaccination: values.vaccination.vaccination || undefined,
vaccineDate: toUTCDate(
values.vaccination.vaccineDate || undefined,
),
vaccineDate: toUTCDate(values.vaccination.vaccineDate),
},
transmission: {
...values.transmission,
Expand All @@ -486,7 +472,7 @@ export default function CaseForm(props: Props): JSX.Element {
...values.travelHistory,
travelHistory: values.travelHistory.travelHistory || undefined,
travelHistoryEntry: toUTCDate(
values.travelHistory.travelHistoryEntry || undefined,
values.travelHistory.travelHistoryEntry,
),
},
location: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios from 'axios';
import { FastField, Field, useFormikContext } from 'formik';
import { Select, TextField } from 'formik-mui';
import { get } from 'lodash';
import dayjs from 'dayjs';
import {
Autocomplete,
FormControl,
Expand Down Expand Up @@ -196,7 +197,8 @@ export function DateField(props: DateFieldProps): JSX.Element {
const { classes } = useStyles();

const dateValue =
typeof props.value === 'string' ? new Date(props.value) : props.value;
// We cut the time part of the date string to avoid timezone issues
typeof props.value === 'string' ? dayjs(props.value?.split('T')[0]).toDate() : props.value;

return (
<div className={classes.fieldRow}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default function TravelHistory(): JSX.Element {
travelHistory: {
...values.travelHistory,
travelHistoryEntry:
newValue?.toDateString() || undefined,
newValue || null,
},
});
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default function Vaccines(): JSX.Element {
vaccination: {
...values.vaccination,
vaccineDate:
newValue?.toDateString() || undefined,
newValue || null,
},
});
}}
Expand Down
19 changes: 12 additions & 7 deletions verification/curator-service/ui/src/components/util/date.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parse } from 'date-fns';
import dayjs from "dayjs";

export default function renderDate(
date: string | Date | undefined | null,
Expand All @@ -14,8 +15,13 @@ export default function renderDate(
}

// Changes the date to be in UTC while maintaining the current date values
export function toUTCDate(dateString: string | undefined): string | undefined {
export function toUTCDate(dateString: Date | string | undefined | null): string | undefined {
if (!dateString) return undefined;
if (dateString instanceof Date) {
return new Date(
Date.UTC(dateString.getFullYear(), dateString.getMonth(), dateString.getDate()),
).toString();
}

const date = new Date(dateString);
// The datepicker sometimes returns the date at the
Expand All @@ -29,14 +35,13 @@ export function toUTCDate(dateString: string | undefined): string | undefined {
}

export function toLocalDate(
dateString: string | null | undefined,
): string | undefined {
if (!dateString) return undefined;
dateString: Date | string | null | undefined,
): Date | null {
if (!dateString) return null;
if (dateString instanceof Date) return dateString;

// Parse date as local timezone to properly display it
const date = parse(dateString.slice(0, 10), 'yyyy-MM-dd', new Date());

return date.toString();
return dayjs(dateString.split('T')[0]).toDate()
}

export function renderDateRange(range?: {
Expand Down
Loading