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

(feat) O3-4342: Split allergy form fields #2193

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const AllergiesDetailedSummary: React.FC<AllergiesDetailedSummaryProps> = ({ pat
{ key: 'reaction', header: t('reaction', 'Reaction') },
{
key: 'note',
header: t('onsetDateAndComments', 'Onset date and comments'),
header: t('onsetDate', 'Onset date '),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
header: t('onsetDate', 'Onset date '),
header: t('onsetDate', 'Onset date'),

},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
TextArea,
TextInput,
} from '@carbon/react';
import { z } from 'zod';
import { date, z } from 'zod';
import { zodResolver } from '@hookform/resolvers/zod';
import { type Control, Controller, useForm, type UseFormSetValue, type UseFormGetValues } from 'react-hook-form';
import {
Expand All @@ -27,6 +27,7 @@ import {
useConfig,
useLayoutType,
ResponsiveWrapper,
OpenmrsDatePicker,
} from '@openmrs/esm-framework';
import { type DefaultPatientWorkspaceProps } from '@openmrs/esm-patient-common-lib';
import {
Expand Down Expand Up @@ -55,6 +56,15 @@ const allergyFormSchema = z.object({
nonCodedAllergicReaction: z.string().optional(),
severityOfWorstReaction: z.string(),
comment: z.string().optional(),
onsetDate: z.date().refine(
(date) => {
const currentDate = new Date(date);
return currentDate <= new Date();
},
{
message: 'Date cannot be in the future',
Copy link
Member

Choose a reason for hiding this comment

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

Wrap this in a t function for i18n.

},
),
});

type AllergyFormData = {
Expand All @@ -64,6 +74,7 @@ type AllergyFormData = {
nonCodedAllergicReaction: string;
severityOfWorstReaction: string;
comment: string;
onsetDate: Date;
};

interface AllergyFormProps extends DefaultPatientWorkspaceProps {
Expand Down Expand Up @@ -133,6 +144,7 @@ function AllergyForm(props: AllergyFormProps) {
nonCodedAllergicReaction: '',
severityOfWorstReaction: null,
comment: '',
onsetDate: new Date(),
};
if (formContext === 'editing') {
defaultAllergy.allergen = allergens?.find((a) => allergy?.display === a?.display);
Expand All @@ -150,7 +162,7 @@ function AllergyForm(props: AllergyFormProps) {
watch,
getValues,
setValue,
formState: { isDirty },
formState: { errors, isDirty },
} = useForm<AllergyFormData>({
mode: 'all',
resolver: zodResolver(allergyFormSchema),
Expand Down Expand Up @@ -190,6 +202,7 @@ function AllergyForm(props: AllergyFormProps) {
nonCodedAllergicReaction,
allergicReactions,
severityOfWorstReaction,
onsetDate,
} = data;

const selectedAllergicReactions = allergicReactions.filter((value) => value !== '');
Expand Down Expand Up @@ -401,6 +414,26 @@ function AllergyForm(props: AllergyFormProps) {
/>
</FormGroup>
</div>
<div>
<ResponsiveWrapper>
<Controller
name="onsetDate"
control={control}
render={({ field: { onBlur, onChange, value } }) => (
<OpenmrsDatePicker
id="onsetDate"
label={t('DateofOnset', 'Date of Onset ')}
onChange={(selectedDate) => {
return onChange(selectedDate);
}}
onBlur={onBlur}
value={value}
/>
)}
/>
</ResponsiveWrapper>
</div>

<div>
<ResponsiveWrapper>
<Controller
Expand All @@ -410,7 +443,7 @@ function AllergyForm(props: AllergyFormProps) {
<TextArea
id="comments"
invalidText={t('invalidComment', 'Invalid comment, try again')}
Copy link
Member

@denniskigen denniskigen Jan 17, 2025

Choose a reason for hiding this comment

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

Suggested change
invalidText={t('invalidComment', 'Invalid comment, try again')}
invalid={Boolean(errors?.comments)}
invalidText={errors?.comments?.message)}

labelText={t('dateOfOnsetAndComments', 'Date of onset and comments')}
labelText={t('comments', 'comments')}
onChange={onChange}
placeholder={t('typeAdditionalComments', 'Type any additional comments here')}
onBlur={onBlur}
Expand Down