Skip to content

Commit

Permalink
Merge pull request #58 from its-kios09/new-error
Browse files Browse the repository at this point in the history
fix: bug fixed on the error handling on the hie confirm modal
  • Loading branch information
patryllus authored Nov 26, 2024
2 parents 629133a + 2b2349d commit 4ef5864
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { type RegistrationConfig } from '../../config-schema';
import { useForm, Controller, type SubmitHandler } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { fetchPatientFromHIE, mapHIEPatientToFormValues } from './hie-resource';
import { type HIEPatientResponse, type HIEPatient } from './hie-types';
import { type HIEPatientResponse, type HIEPatient, type ErrorResponse } from './hie-types';

type HIEClientRegistryProps = {
props: FormikProps<FormValues>;
Expand Down Expand Up @@ -39,11 +39,34 @@ const HIEClientRegistry: React.FC<HIEClientRegistryProps> = ({ setInitialFormVal
hieClientRegistry: { identifierTypes },
} = useConfig<RegistrationConfig>();

const isHIEPatientResponse = (
response: HIEPatientResponse | ErrorResponse | undefined,
): response is HIEPatientResponse => {
return response?.resourceType === 'Bundle' && 'total' in response;
};

const isOperationOutcome = (response: HIEPatientResponse | ErrorResponse | undefined): response is ErrorResponse => {
return response?.resourceType === 'OperationOutcome' && 'issue' in response;
};

const onSubmit: SubmitHandler<HIEFormValues> = async (data: HIEFormValues, event: React.BaseSyntheticEvent) => {
try {
const hieClientRegistry = await fetchPatientFromHIE(data.identifierType, data.identifierValue);

if (hieClientRegistry && hieClientRegistry.resourceType === 'Bundle') {
if (isHIEPatientResponse(hieClientRegistry)) {
if (hieClientRegistry.total === 0) {
const dispose = showModal('empty-client-registry-modal', {
onConfirm: () => dispose(),
close: () => dispose(),
title: t('clientRegistryEmpty', 'Create & Post Patient'),
message: t(
'patientNotFound',
`No patient found with the provided ${data?.identifierType}. Proceed to register.`,
),
});
return;
}

const dispose = showModal('hie-confirmation-modal', {
patient: hieClientRegistry,
closeModal: () => dispose(),
Expand All @@ -52,15 +75,20 @@ const HIEClientRegistry: React.FC<HIEClientRegistryProps> = ({ setInitialFormVal
mapHIEPatientToFormValues(hieClientRegistry as unknown as HIEPatientResponse, props.values),
),
});
}

if (hieClientRegistry && hieClientRegistry?.resourceType === 'OperationOutcome') {
const issueMessage = hieClientRegistry?.['issue']?.map((issue) => issue.diagnostics).join(', ');
} else if (isOperationOutcome(hieClientRegistry)) {
const issueMessage = hieClientRegistry?.issue?.map((issue) => issue.diagnostics).join(', ');
const dispose = showModal('empty-client-registry-modal', {
onConfirm: () => dispose(),
close: () => dispose(),
title: t('clientRegistryEmpty', 'Create & Post Patient'),
message: issueMessage,
message: issueMessage || t('errorOccurred', ' There was an error processing the request. Try again later'),
});
} else {
showSnackbar({
title: t('unexpectedResponse', 'Unexpected Response'),
subtitle: t('contactAdmin', 'Please contact the administrator.'),
kind: 'error',
isLowContrast: true,
});
}
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ const mapperConfig: MapperConfig = {
};

// Create instances
const hieApiClient = new HealthInformationExchangeClient<HIEPatient | ErrorResponse>();
const hieApiClient = new HealthInformationExchangeClient<HIEPatientResponse | ErrorResponse>();
const patientMapper = new PatientMapper(mapperConfig);

// Exported functions
export const fetchPatientFromHIE = async (
identifierType: string,
identifierValue: string,
): Promise<HIEPatient | ErrorResponse> => {
return hieApiClient.fetchResource('Patient', { [identifierType]: identifierValue });
): Promise<HIEPatientResponse | ErrorResponse> => {
return hieApiClient.fetchResource('Bundle', { [identifierType]: identifierValue });
};

export const mapHIEPatientToFormValues = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export type HIEPatient = fhir.Patient & {
};

export interface HIEPatientResponse {
resourceType: string;
id: string;
total: number;
meta: Metadata;
link: Link[];
entry: Entry[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const HIEPatientDetailPreview: React.FC<HIEPatientDetailPreviewProps> = ({ patie
state={{ patientName: `${maskData(givenName)} . ${maskData(middleName)} . ${maskData(familyName)}` }}
/>
<div className={styles.patientInfoContainer}>
<PatientInfo label={t('healthID', 'HealthID')} value={getidentifier('SHA-number')?.value} />
<PatientInfo label={t('healthID', 'HealthID')} value={getidentifier('sha-number')?.value} />
<PatientInfo
label={t('patientName', 'Patient name')}
customValue={
Expand Down
3 changes: 3 additions & 0 deletions packages/esm-patient-registration-app/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"confirmDiscardChangesBody": "Your unsaved changes will be lost if you proceed to discard the form",
"confirmDiscardChangesTitle": "Are you sure you want to discard these changes?",
"confirmIdentifierDeletionText": "Are you sure you want to remove this identifier?",
"contactAdmin": "Please contact the administrator.",
"contactSection": "Contact Details",
"continue": "Continue",
"createNewPatient": "Create new patient",
Expand Down Expand Up @@ -59,6 +60,7 @@
"errorFetchingCodedCausesOfDeath": "Error fetching coded causes of death",
"errorFetchingOrderedFields": "Error occured fetching ordered fields for address hierarchy",
"errorFetchingPatient": "Error fetching patient",
"errorOccurred": " There was an error processing the request. Try again later",
"estimatedAgeInMonthsLabelText": "Estimated age in months",
"estimatedAgeInYearsLabelText": "Estimated age in years",
"familyNameLabelText": "Family Name",
Expand Down Expand Up @@ -145,6 +147,7 @@
"timeFormat": "Time Format",
"timeOfDeathInputLabel": "Time of death (hh:mm)",
"unableToFetch": "Unable to fetch person attribute type - {{personattributetype}}",
"unexpectedResponse": "Unexpected Response",
"unknown": "Unknown",
"unknownPatientAttributeType": "Patient attribute type has unknown format {{personAttributeTypeFormat}}",
"unknownRelationship": "Unknown",
Expand Down

0 comments on commit 4ef5864

Please sign in to comment.