Skip to content

Commit

Permalink
(feat) O3-2572: Implement the new snack bar in patient management(par…
Browse files Browse the repository at this point in the history
…t 1) (#921)

* implement snackbar in patient management

* fix tests

---------

Co-authored-by: Vineet Sharma <sharmava05@gmail.com>
  • Loading branch information
hadijahkyampeire and vasharma05 authored Jan 15, 2024
1 parent 329cf71 commit 01b06e5
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useTranslation } from 'react-i18next';
import { Form, Formik, type FormikHelpers } from 'formik';
import { validationSchema } from './appointment-services-validation';
import { useAppointmentServices } from './appointment-services-hook';
import { showNotification, showToast, useLocations } from '@openmrs/esm-framework';
import { showSnackbar, useLocations } from '@openmrs/esm-framework';
import type { AppointmentService } from '../../types';
import { closeOverlay } from '../../hooks/useOverlay';
import styles from './appointment-services.scss';
Expand All @@ -29,21 +29,20 @@ const AppointmentServices: React.FC<AppointmentServicesProps> = () => {
addNewAppointmentService(payload).then(
({ status }) => {
if (status === 200) {
showToast({
critical: true,
showSnackbar({
isLowContrast: true,
kind: 'success',
description: t('appointmentServiceCreate', 'Appointment service created successfully'),
subtitle: t('appointmentServiceCreate', 'Appointment service created successfully'),
title: t('appointmentService', 'Appointment service'),
});
closeOverlay();
}
},
(error) => {
showNotification({
showSnackbar({
title: t('errorCreatingAppointmentService', 'Error creating appointment service'),
kind: 'error',
critical: true,
description: error?.message,
subtitle: error?.message,
});
},
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { Button, Layer, TextArea } from '@carbon/react';
import { useSession, showToast, showNotification, ExtensionSlot, usePatient } from '@openmrs/esm-framework';
import { useSession, showSnackbar, ExtensionSlot, usePatient } from '@openmrs/esm-framework';
import { cancelAppointment } from '../forms.resource';
import { useSWRConfig } from 'swr';
import { useAppointmentDate } from '../../../helpers';
Expand Down Expand Up @@ -32,21 +32,20 @@ const CancelAppointment: React.FC<CancelAppointmentProps> = ({ appointment }) =>
setIsSubmitting(true);
const { status } = await cancelAppointment('Cancelled', appointment.id);
if (status === 200) {
showToast({
critical: true,
showSnackbar({
isLowContrast: true,
kind: 'success',
description: t('cancelledSuccessfully', 'It has been cancelled successfully'),
subtitle: t('cancelledSuccessfully', 'It has been cancelled successfully'),
title: t('appointmentCancelled', 'Appointment cancelled'),
});
mutate(`/ws/rest/v1/appointment/appointmentStatus?forDate=${currentAppointmentDate}&status=Scheduled`);
mutate(`/ws/rest/v1/appointment/appointmentStatus?forDate=${currentAppointmentDate}&status=Cancelled`);
closeOverlay();
} else {
showNotification({
showSnackbar({
title: t('appointmentCancelError', 'Error cancelling appointment'),
kind: 'error',
critical: true,
description: t('errorCancellingAppointment', 'Error cancelling the appointment'),
subtitle: t('errorCancellingAppointment', 'Error cancelling the appointment'),
});
setIsSubmitting(false);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import userEvent from '@testing-library/user-event';
import { screen, render } from '@testing-library/react';
import { showNotification, showToast } from '@openmrs/esm-framework';
import { showSnackbar } from '@openmrs/esm-framework';
import { cancelAppointment } from '../forms.resource';
import { mockMappedAppointmentsData } from '__mocks__';
import CancelAppointment from './cancel-appointment.component';
Expand All @@ -10,9 +10,8 @@ const testProps = {
appointment: mockMappedAppointmentsData.data[0],
};

const mockShowToast = showToast as jest.Mock;
const mockShowSnackbar = showSnackbar as jest.Mock;
const mockCancelAppointment = cancelAppointment as jest.Mock;
const mockShowNotification = showNotification as jest.Mock;

jest.mock('../forms.resource', () => {
const originalModule = jest.requireActual('../forms.resource');
Expand All @@ -32,7 +31,7 @@ jest.mock('@openmrs/esm-framework', () => {
});

describe('Cancel appointment form', () => {
it('should update appointment status to cancelled and show toast with success message', async () => {
it('should update appointment status to cancelled and show snackbar with success message', async () => {
const user = userEvent.setup();

mockCancelAppointment.mockReturnValueOnce({ status: 200, statusText: 'Appointment cancelled' });
Expand All @@ -42,12 +41,12 @@ describe('Cancel appointment form', () => {
await user.click(screen.getByRole('textbox', { name: /reason for changes/i }));
await user.click(screen.getByRole('button', { name: /cancel appointment/i }));

expect(mockShowToast).toHaveBeenCalledTimes(1);
expect(mockShowToast).toHaveBeenCalledWith({
critical: true,
expect(mockShowSnackbar).toHaveBeenCalledTimes(1);
expect(mockShowSnackbar).toHaveBeenCalledWith({
isLowContrast: true,
kind: 'success',
title: 'Appointment cancelled',
description: 'It has been cancelled successfully',
subtitle: 'It has been cancelled successfully',
});
});

Expand All @@ -62,12 +61,11 @@ describe('Cancel appointment form', () => {

await user.click(screen.getByRole('textbox', { name: /reason for changes/i }));
await user.click(screen.getByRole('button', { name: /cancel appointment/i }));
expect(mockShowNotification).toHaveBeenCalledTimes(1);
expect(mockShowNotification).toHaveBeenCalledWith({
critical: true,
expect(mockShowSnackbar).toHaveBeenCalledTimes(2);
expect(mockShowSnackbar).toHaveBeenCalledWith({
kind: 'error',
title: 'Error cancelling appointment',
description: 'Error cancelling the appointment',
subtitle: 'Error cancelling the appointment',
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ import {
} from '@carbon/react';
import {
ExtensionSlot,
showNotification,
showToast,
showSnackbar,
useConfig,
useLayoutType,
useLocations,
Expand Down Expand Up @@ -128,10 +127,10 @@ const AppointmentForm: React.FC<AppointmentFormProps> = ({ appointment, patientU
saveAppointment(appointmentPayload).then(
({ status }) => {
if (status === 200) {
showToast({
critical: true,
showSnackbar({
isLowContrast: true,
kind: 'success',
description: t('appointmentNowVisible', 'It is now visible on the appointments page'),
subtitle: t('appointmentNowVisible', 'It is now visible on the appointments page'),
title: t('appointmentScheduled', 'Appointment scheduled'),
});
setIsSubmitting(false);
Expand All @@ -145,11 +144,10 @@ const AppointmentForm: React.FC<AppointmentFormProps> = ({ appointment, patientU
}
},
(error) => {
showNotification({
showSnackbar({
title: t('appointmentFormError', 'Error scheduling appointment'),
kind: 'error',
critical: true,
description: error?.message,
subtitle: error?.message,
});
setIsSubmitting(false);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ import {
useVisitTypes,
type NewVisitPayload,
saveVisit,
toOmrsIsoString,
toDateObjectStrict,
showNotification,
showToast,
showSnackbar,
usePatient,
useConfig,
useLocations,
Expand Down Expand Up @@ -138,10 +135,11 @@ const VisitForm: React.FC<VisitFormProps> = ({ patientUuid, appointment }) => {
({ status }) => {
if (status === 201) {
mutate();
showToast({
showSnackbar({
kind: 'success',
isLowContrast: true,
title: t('visitStarted', 'Visit started'),
description: t(
subtitle: t(
'queueAddedSuccessfully',
`Patient has been added to the queue successfully.`,
`${hours} : ${minutes}`,
Expand All @@ -150,11 +148,10 @@ const VisitForm: React.FC<VisitFormProps> = ({ patientUuid, appointment }) => {
}
},
(error) => {
showNotification({
showSnackbar({
title: t('queueEntryError', 'Error adding patient to the queue'),
kind: 'error',
critical: true,
description: error?.message,
subtitle: error?.message,
});
},
);
Expand All @@ -165,11 +162,10 @@ const VisitForm: React.FC<VisitFormProps> = ({ patientUuid, appointment }) => {
}
},
(error) => {
showNotification({
showSnackbar({
title: t('startVisitError', 'Error starting visit'),
kind: 'error',
critical: true,
description: error?.message,
subtitle: error?.message,
});
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getDynamicOfflineDataEntries,
putDynamicOfflineData,
syncDynamicOfflineData,
showToast,
showSnackbar,
toOmrsIsoString,
usePagination,
navigate,
Expand Down Expand Up @@ -62,17 +62,18 @@ const AddPatient: React.FC<AddPatientProps> = ({ closeModal, patientUuid }) => {
patientList
.addPatient()
.then(() =>
showToast({
showSnackbar({
title: t('successfullyAdded', 'Successfully added'),
kind: 'success',
description: `${t('successAddPatientToList', 'Patient added to list')}: ${patientList.displayName}`,
isLowContrast: true,
subtitle: `${t('successAddPatientToList', 'Patient added to list')}: ${patientList.displayName}`,
}),
)
.catch(() =>
showToast({
showSnackbar({
title: t('error', 'Error'),
kind: 'error',
description: `${t('errorAddPatientToList', 'Patient not added to list')}: ${patientList.displayName}`,
subtitle: `${t('errorAddPatientToList', 'Patient not added to list')}: ${patientList.displayName}`,
}),
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useCallback, type SyntheticEvent, useEffect, useId, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Button, ButtonSet, Layer, TextArea, TextInput } from '@carbon/react';
import { useLayoutType, showToast, useSession, useConfig } from '@openmrs/esm-framework';
import { useLayoutType, showSnackbar, useSession, useConfig } from '@openmrs/esm-framework';
import type { ConfigSchema } from '../config-schema';
import type { NewCohortData, OpenmrsCohort } from '../api/types';
import { createPatientList, editPatientList } from '../api/api-remote';
Expand Down Expand Up @@ -47,21 +47,21 @@ const CreateEditPatientList: React.FC<CreateEditPatientListProps> = ({
if (isEditing) {
editPatientList(patientListDetails.uuid, cohortDetails)
.then(() => {
showToast({
showSnackbar({
title: t('updated', 'Updated'),
description: t('listUpdated', 'List updated successfully'),
subtitle: t('listUpdated', 'List updated successfully'),
kind: 'success',
isLowContrast: true,
});

onSuccess();
setIsSubmitting(false);
close();
})
.catch((error) => {
console.error('Error updating list: ', error);
showToast({
showSnackbar({
title: t('errorUpdatingList', 'Error updating list'),
description: t('problemUpdatingList', 'There was a problem updating the list'),
subtitle: t('problemUpdatingList', 'There was a problem updating the list'),
kind: 'error',
});
setIsSubmitting(false);
Expand All @@ -75,20 +75,20 @@ const CreateEditPatientList: React.FC<CreateEditPatientListProps> = ({
location: session?.sessionLocation?.uuid,
})
.then(() => {
showToast({
showSnackbar({
title: t('created', 'Created'),
description: `${t('listCreated', 'List created successfully')}`,
subtitle: `${t('listCreated', 'List created successfully')}`,
kind: 'success',
isLowContrast: true,
});
onSuccess();
setIsSubmitting(false);
close();
})
.catch((error) => {
console.error('Error creating list: ', error);
showToast({
showSnackbar({
title: t('errorCreatingList', 'Error creating list'),
description: t('problemCreatingList', 'There was a problem creating the list'),
subtitle: t('problemCreatingList', 'There was a problem creating the list'),
kind: 'error',
});
setIsSubmitting(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
Tile,
} from '@carbon/react';
import { ArrowLeft, TrashCan } from '@carbon/react/icons';
import { ConfigurableLink, useLayoutType, isDesktop, showToast, useDebounce } from '@openmrs/esm-framework';
import { ConfigurableLink, useLayoutType, isDesktop, showSnackbar, useDebounce } from '@openmrs/esm-framework';
import { removePatientFromList } from '../api/api-remote';
import { EmptyDataIllustration } from '../empty-state/empty-data-illustration.component';
import styles from './list-details-table.scss';
Expand Down Expand Up @@ -215,17 +215,16 @@ const ListDetailsTable: React.FC<ListDetailsTableProps> = ({
mutateListMembers();
mutateListDetails();

showToast({
critical: true,
showSnackbar({
isLowContrast: true,
kind: 'success',
description: t('listUpToDate', 'The list is now up to date'),
subtitle: t('listUpToDate', 'The list is now up to date'),
title: t('patientRemovedFromList', 'Patient removed from list'),
});
} catch (error) {
showToast({
critical: true,
showSnackbar({
kind: 'error',
description: error?.message,
subtitle: error?.message,
title: t('errorRemovingPatientFromList', 'Failed to remove patient from list'),
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useParams } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { OverflowMenuItem, Modal } from '@carbon/react';
import { OverflowMenuVertical } from '@carbon/react/icons';
import { navigate, formatDate, parseDate, showToast } from '@openmrs/esm-framework';
import { navigate, formatDate, parseDate, showSnackbar } from '@openmrs/esm-framework';
import { deletePatientList } from '../api/api-remote';
import { usePatientListDetails, usePatientListMembers } from '../api/hooks';
import CreateEditPatientList from '../create-edit-patient-list/create-edit-list.component';
Expand Down Expand Up @@ -88,18 +88,19 @@ const ListDetails = () => {
const confirmDeletePatientList = useCallback(() => {
deletePatientList(patientListUuid)
.then(() => {
showToast({
showSnackbar({
title: t('deleted', 'Deleted'),
description: `${t('deletedPatientList', 'Deleted patient list')}: ${listDetails?.name}`,
subtitle: `${t('deletedPatientList', 'Deleted patient list')}: ${listDetails?.name}`,
kind: 'success',
isLowContrast: true,
});

navigate({ to: window.getOpenmrsSpaBase() + 'home/patient-lists' });
})
.catch((e) =>
showToast({
showSnackbar({
title: t('errorDeletingList', 'Error deleting patient list'),
description: e?.message,
subtitle: e?.message,
kind: 'error',
}),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jest.mock('../api/api-remote');

jest.mock('@openmrs/esm-framework', () => ({
...jest.requireActual('@openmrs/esm-framework'),
showToast: jest.fn(),
showSnackbar: jest.fn(),
navigate: jest.fn(),
ExtensionSlot: jest.fn(),
}));
Expand Down
Loading

0 comments on commit 01b06e5

Please sign in to comment.