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

Add date time formats context #375

Merged
merged 1 commit into from
Nov 19, 2024
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
12 changes: 6 additions & 6 deletions src/components/BaseQuestionnaireResponseForm/widgets/date.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Form } from 'antd';
import moment, { type Moment } from 'moment';
import { useCallback, useMemo } from 'react';
import { useCallback, useContext, useMemo } from 'react';
import { QuestionItemProps } from 'sdc-qrf';

import {
FHIRDateFormat,
FHIRTimeFormat,
FHIRDateTimeFormat,
formatFHIRDate,
formatFHIRDateTime,
formatFHIRTime,
Expand All @@ -16,6 +14,7 @@ import { DatePicker } from 'src/components/DatePicker';
import { TimePicker } from 'src/components/TimePicker';

import { useFieldController } from '../hooks';
import { DateTimeFormatContext } from 'src/contexts/date-time-format';

export function QuestionDateTime({ parentPath, questionItem }: QuestionItemProps) {
const { linkId, type, regex } = questionItem;
Expand Down Expand Up @@ -46,6 +45,7 @@ interface DateTimePickerWrapperProps {
}

function DateTimePickerWrapper(props: DateTimePickerWrapperProps) {
const { humanDate, humanTime, humanDateTime } = useContext(DateTimeFormatContext);
const { value, onChange, type, disabled, placeholder, format } = props;

const newValue = useMemo(() => {
Expand All @@ -65,15 +65,15 @@ function DateTimePickerWrapper(props: DateTimePickerWrapperProps) {
let formatFunction: (value: Moment) => string;

if (type === 'date') {
resultFormat = format || FHIRDateFormat;
resultFormat = format || humanDate;
showTime = false;
formatFunction = formatFHIRDate;
} else if (type === 'time') {
resultFormat = format || FHIRTimeFormat;
resultFormat = format || humanTime;
showTime = { format: resultFormat };
formatFunction = formatFHIRTime;
} else {
resultFormat = format || FHIRDateTimeFormat;
resultFormat = format || humanDateTime;
showTime = { format: resultFormat };
formatFunction = formatFHIRDateTime;
}
Expand Down
7 changes: 7 additions & 0 deletions src/contexts/date-time-format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createContext } from 'react';

import { humanDate, humanDateTime, humanDateYearMonth, humanTime } from 'src/utils/date';

export const defaultDateTimeFormats = { humanDate, humanDateYearMonth, humanTime, humanDateTime };

export const DateTimeFormatContext = createContext(defaultDateTimeFormats);
17 changes: 10 additions & 7 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { ValueSetExpandProvider } from './contexts';
import { expandEMRValueSet } from './services';
import * as serviceWorker from './serviceWorker';
import { ThemeProvider } from './theme/ThemeProvider';
import { DateTimeFormatContext, defaultDateTimeFormats } from './contexts/date-time-format';

const AppWithContext = () => {
useEffect(() => {
Expand All @@ -25,13 +26,15 @@ const AppWithContext = () => {

return (
<I18nProvider i18n={i18n}>
<PatientDashboardProvider dashboard={dashboard}>
<ValueSetExpandProvider.Provider value={expandEMRValueSet}>
<ThemeProvider>
<App />
</ThemeProvider>
</ValueSetExpandProvider.Provider>
</PatientDashboardProvider>
<DateTimeFormatContext.Provider value={defaultDateTimeFormats}>
<PatientDashboardProvider dashboard={dashboard}>
<ValueSetExpandProvider.Provider value={expandEMRValueSet}>
<ThemeProvider>
<App />
</ThemeProvider>
</ValueSetExpandProvider.Provider>
</PatientDashboardProvider>
</DateTimeFormatContext.Provider>
</I18nProvider>
);
};
Expand Down
Loading