Skip to content

Commit

Permalink
O3-2812: Appointments: Refactoring and Code Cleanup (#965)
Browse files Browse the repository at this point in the history
* O3-2812: Appointments: Refactoring and Code Cleanup

* O3-2812: Appointments: Refactoring and Code Cleanup

* O3-2812: Appointments: Refactoring and Code Cleanup

* O3-2812: Appointments: Refactoring and Code Cleanup
  • Loading branch information
mogoodrich authored Feb 2, 2024
1 parent 05b99df commit 11ec51f
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { ExtensionSlot, formatDate, formatDatetime } from '@openmrs/esm-framewor
import { downloadAppointmentsAsExcel } from '../../helpers/excel';
import { Download } from '@carbon/react/icons';
import styles from './calenar-patient-list.scss';
import { useAppointments } from '../../appointments/appointments-table.resource';
import { useAppointments } from '../../hooks/useAppointments';

interface CalendarPatientListProps {}

Expand Down
4 changes: 2 additions & 2 deletions packages/esm-appointments-app/src/appointments.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import AppointmentsHeader from './appointments-header/appointments-header.compon
import AppointmentMetrics from './appointments-metrics/appointments-metrics.component';
import Overlay from './overlay.component';

const ClinicalAppointments: React.FC = () => {
const Appointments: React.FC = () => {
const { t } = useTranslation();
const [appointmentServiceType, setAppointmentServiceType] = useState<string>('');

Expand All @@ -19,4 +19,4 @@ const ClinicalAppointments: React.FC = () => {
);
};

export default ClinicalAppointments;
export default Appointments;

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ export function useServices() {
isValidating,
};
}

export function getAppointmentService(uuid: string) {
const abortController = new AbortController();

return openmrsFetch(`/ws/rest/v1/appointmentService?uuid=` + uuid, {
signal: abortController.signal,
});
}

export function useProviders() {
const { data, error, isLoading } = useSWR<{ data: { results: Array<Provider> } }, Error>(
`/ws/rest/v1/provider`,
Expand Down Expand Up @@ -86,6 +77,8 @@ export const useAppointmentSummary = (fromDate: Date, serviceUuid: string): Arra
}))
.sort((dateA, dateB) => new Date(dateA.date).getTime() - new Date(dateB.date).getTime());
};

// NOTE: I don't think this is used anywhere?
export const checkAppointmentConflict = async (appointmentPayload: AppointmentPayload) => {
return await openmrsFetch('/ws/rest/v1/appointments/conflicts', {
method: 'POST',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import { EmptyDataIllustration } from './empty-data-illustration.component';
import { launchCheckInAppointmentModal, handleComplete } from './common';
import { SeeAllAppointmentsLink, AddAppointmentLink, ViewCalendarLink } from './links.component';
import { type Appointment, type MappedHomeAppointment } from '../types';
import { useTodaysAppointments } from './home-appointments.resource';
import { type ConfigObject } from '../config-schema';
import { useTodaysAppointments } from './appointments-table.resource';
import styles from './appointments-list.scss';

interface PaginationData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import React from 'react';
import { render, screen } from '@testing-library/react';
import { mockSession } from '__mocks__';
import { usePagination } from '@openmrs/esm-framework';
import { useTodaysAppointments } from './appointments-table.resource';
import { useTodaysAppointments } from './home-appointments.resource';
import AppointmentsBaseTable from './appointments-list.component';

const useTodaysAppointmentsMock = useTodaysAppointments as jest.Mock;
const usePaginationMock = usePagination as jest.Mock;

jest.mock('./appointments-table.resource');
jest.mock('./home-appointments.resource');

jest.mock('@openmrs/esm-framework', () => ({
...jest.requireActual('@openmrs/esm-framework'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Button, Layer, ModalBody, ModalFooter, ModalHeader, TimePicker } from '
import { useSWRConfig } from 'swr';
import { useTranslation } from 'react-i18next';
import { showNotification, showActionableNotification } from '@openmrs/esm-framework';
import { updateAppointmentStatus } from '../appointments-table.resource';
import { updateAppointmentStatus } from '../home-appointments.resource';
import { handleUndoAction } from '../common';
import styles from './check-in-modal.scss';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import React from 'react';
import userEvent from '@testing-library/user-event';
import { render, screen } from '@testing-library/react';
import { updateAppointmentStatus } from '../appointments-table.resource';
import { updateAppointmentStatus } from '../home-appointments.resource';
import { showActionableNotification, showNotification } from '@openmrs/esm-framework';
import CheckInAppointmentModal from './check-in-modal.component';

const mockUpdateAppointmentStatus = updateAppointmentStatus as jest.Mock;
const appointmentUuid = '7cd38a6d-377e-491b-8284-b04cf8b8c6d8';

jest.mock('@openmrs/esm-framework');
jest.mock('../appointments-table.resource');
jest.mock('../home-appointments.resource');

describe('CheckInAppointmentModal', () => {
afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { showModal, showNotification, showActionableNotification } from '@openmrs/esm-framework';
import { updateAppointmentStatus, undoAppointmentStatus } from './appointments-table.resource';
import { updateAppointmentStatus, undoAppointmentStatus } from './home-appointments.resource';

export const launchCheckInAppointmentModal = (appointmentUuid: string) => {
const dispose = showModal('check-in-appointment-modal', {
Expand Down
25 changes: 25 additions & 0 deletions packages/esm-appointments-app/src/hooks/useAppointments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,38 @@ import dayjs from 'dayjs';
import useSWR from 'swr';
import { omrsDateFormat } from '../constants';
import { type Appointment, DurationPeriod } from '../types';
import { getAppointment, useAppointmentDate } from '../helpers';
import isEmpty from 'lodash-es/isEmpty';
import { useMemo } from 'react';

interface AppointmentsReturnType {
isLoading: boolean;
appointments: Array<Appointment>;
error: Error;
}

export function useAppointments(status?: string, forDate?: string) {
const { currentAppointmentDate } = useAppointmentDate();
const startDate = forDate ? forDate : currentAppointmentDate;
const apiUrl = `/ws/rest/v1/appointment/appointmentStatus?forDate=${startDate}&status=${status}`;
const allAppointmentsUrl = `/ws/rest/v1/appointment/all?forDate=${startDate}`;

const { data, error, isLoading, isValidating, mutate } = useSWR<{ data: Array<Appointment> }, Error>(
isEmpty(status) ? allAppointmentsUrl : apiUrl,
openmrsFetch,
);

const appointments = useMemo(() => data?.data?.map((appointment) => getAppointment(appointment)) ?? [], [data?.data]);

return {
appointments,
isLoading,
isError: error,
isValidating,
mutate,
};
}

export const useDailyAppointments = (startDateTime: string, durationPeriod: DurationPeriod) => {
const url = `/ws/rest/v1/appointment/all?forDate=${startDateTime}`;
const { data, error, isLoading } = useSWR<{ data: Array<Appointment> }>(startDateTime ? url : null, openmrsFetch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { type amPm, convertTime12to24, useAppointmentDate } from '../../helpers'
import { closeOverlay } from '../../hooks/useOverlay';
import { saveQueueEntry } from './queue.resource';
import { type MappedAppointment } from '../../types';
import { useAppointments } from '../../appointments/appointments-table.resource';
import { useAppointments } from '../../hooks/useAppointments';
import { useDefaultLoginLocation } from '../../hooks/useDefaultLocation';
import { useVisits } from '../../hooks/useVisits';
import styles from './visit-form.scss';
Expand Down
4 changes: 2 additions & 2 deletions packages/esm-appointments-app/src/root.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { SWRConfig } from 'swr';
import { BrowserRouter, Route, Routes } from 'react-router-dom';
import AppointmentsCalendarView from './appointments-calendar/appointments-calendar-view.component';
import ClinicalAppointments from './appointments.component';
import Appointments from './appointments.component';
import CalendarPatientList from './appointments-calendar/patient-list/calendar-patient-list.component';

const swrConfiguration = {
Expand All @@ -17,7 +17,7 @@ const RootComponent: React.FC = () => {
<SWRConfig value={swrConfiguration}>
<BrowserRouter basename={appointmentsBasename}>
<Routes>
<Route path="/" element={<ClinicalAppointments />} />
<Route path="/" element={<Appointments />} />
<Route path="/calendar" element={<AppointmentsCalendarView />} />
<Route path="/list/:dateTime/:serviceName" element={<CalendarPatientList />} />
</Routes>
Expand Down

0 comments on commit 11ec51f

Please sign in to comment.