Here’s the complete implementation of src/components/soap-note-form.component.tsx. This component provides a form for adding new SOAP notes, following the structure of the Condition App's "Add" functionality:
import React, { useState } from 'react';
import { useTranslation } from 'react-i18next';
import {
Button,
ButtonSet,
Form,
FormGroup,
TextArea,
InlineNotification
} from '@carbon/react';
import {
showNotification,
useConfig,
useSession,
useLayoutType
} from '@openmrs/esm-framework';
import { usePatient } from '@openmrs/esm-patient-common-lib';
import { createSoapNoteEncounter } from '../api/soap-note.resource';
import type { Config, SoapNoteFormData } from '../types';
import styles from './soap-note-form.scss';
interface SoapNoteFormProps {
patientUuid: string;
closeWorkspace: () => void;
}
const SoapNoteForm: React.FC<SoapNoteFormProps> = ({ patientUuid, closeWorkspace }) => {
const { t } = useTranslation();
const config = useConfig<Config>();
const { patient } = usePatient(patientUuid);
const { user } = useSession();
const isTablet = useLayoutType() === 'tablet';
const [formData, setFormData] = useState<SoapNoteFormData>({
subjective: '',
objective: '',
assessment: '',
plan: '',
});
const [isSubmitting, setIsSubmitting] = useState(false);
const [error, setError] = useState<Error | null>(null);
const handleInputChange = (field: keyof SoapNoteFormData) => (
event: React.ChangeEvent<HTMLTextAreaElement>
) => {
setFormData({ ...formData, [field]: event.target.value });
};
const handleSubmit = async (event: React.FormEvent) => {
event.preventDefault();
setIsSubmitting(true);
setError(null);
try {
await createSoapNoteEncounter(
patientUuid,
config.soapNoteEncounterTypeUuid,
formData,
config.concepts,
user?.uuid || '',
user?.sessionLocation?.uuid || ''
);
showNotification({
title: t('soapNoteAdded', 'SOAP Note Added'),
kind: 'success',
description: t('soapNoteSuccessfullyAdded', 'The SOAP note has been successfully added.'),
});
closeWorkspace();
} catch (err) {
setError(err as Error);
showNotification({
title: t('errorAddingSoapNote', 'Error Adding SOAP Note'),
kind: 'error',
description: err instanceof Error ? err.message : t('unknownError', 'An unknown error occurred.'),
});
} finally {
setIsSubmitting(false);
}
};
return (
<Form onSubmit={handleSubmit} className={styles.form}>
{error && (
<InlineNotification
kind="error"
subtitle={error.message}
title={t('errorSavingSoapNote', 'Error saving SOAP note')}
onClose={() => setError(null)}
/>
)}
<FormGroup legendText={t('soapNoteDetails', 'SOAP Note Details')}>
<TextArea
id="subjective"
labelText={t('subjective', 'Subjective')}
value={formData.subjective}
onChange={handleInputChange('subjective')}
rows={isTablet ? 4 : 3}
/>
<TextArea
id="objective"
labelText={t('objective', 'Objective')}
value={formData.objective}
onChange={handleInputChange('objective')}
rows={isTablet ? 4 : 3}
/>
<TextArea
id="assessment"
labelText={t('assessment', 'Assessment')}
value={formData.assessment}
onChange={handleInputChange('assessment')}
rows={isTablet ? 4 : 3}
/>
<TextArea
id="plan"
labelText={t('plan', 'Plan')}
value={formData.plan}
onChange={handleInputChange('plan')}
rows={isTablet ? 4 : 3}
/>
</FormGroup>
<ButtonSet className={styles.buttonSet}>
<Button
kind="secondary"
onClick={closeWorkspace}
disabled={isSubmitting}
>
{t('cancel', 'Cancel')}
</Button>
<Button
type="submit"
disabled={isSubmitting}
>
{isSubmitting ? t('saving', 'Saving...') : t('save', 'Save')}
</Button>
</ButtonSet>
</Form>
);
};
export default SoapNoteForm;
Key Features:
- Form Fields: Separate
TextArea components for Subjective, Objective, Assessment, and Plan.
- Error Handling: Displays errors using Carbon's
InlineNotification.
- Responsive Design: Adjusts textarea rows based on screen size (tablet vs desktop).
- Integration: Uses the
createSoapNoteEncounter API function to save the SOAP note as an encounter.
- Notifications: Shows success/error notifications using OpenMRS's
showNotification.
Dependencies:
- Ensure
createSoapNoteEncounter (from soap-note.resource.ts) is correctly implemented.
- The
Config type should include the necessary UUIDs for encounter type and concepts.
Let me know if you need help with any specific part!