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

O3-3067: (fix) Incorrect parameters passed to react hook when filtering queues by service uuid #1095

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -68,6 +68,7 @@ import DefaultQueueTable from '../queue-table/default-queue-table.component';
import { type QueueEntry } from '../types';
import { mapVisitQueueEntryProperties, type MappedVisitQueueEntry } from './active-visits-table.resource';
import styles from './active-visits-table.scss';
import uniqBy from 'lodash-es/uniqBy';

/**
* FIXME Temporarily moved here
Expand All @@ -92,10 +93,10 @@ interface PaginationData {
}

function ActiveVisitsTable() {
const selectedQueueUuid = useSelectedServiceUuid();
const selectedServiceUuid = useSelectedServiceUuid();
const currentLocationUuid = useSelectedQueueLocationUuid();
const { queueEntries, isLoading, error } = useQueueEntries({
queue: selectedQueueUuid,
service: selectedServiceUuid,
location: currentLocationUuid,
isEnded: false,
});
Expand Down Expand Up @@ -126,13 +127,13 @@ function OldQueueTable({ queueEntries }: { queueEntries: QueueEntry[] }) {
const { t } = useTranslation();
const currentServiceName = useSelectedServiceName();
const currentQueueLocation = useSelectedQueueLocationUuid();
const { queues } = useQueues(currentQueueLocation);
const { visitQueueNumberAttributeUuid } = useConfig<ConfigObject>();
const currentServiceUuid = useSelectedServiceUuid();
const visitQueueEntries = queueEntries.map((entry) =>
mapVisitQueueEntryProperties(entry, visitQueueNumberAttributeUuid),
);
const queues = uniqBy(queueEntries?.flatMap((entry) => entry.queue) ?? [], 'uuid');
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this will make the dropdown list display only queues that have at least one queue entry. Are we sure we want that? The order of those queues can be unpredictable based on the order of the returned queue entries.


Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't think this filter is needed at all. We should just be able to use queueEntries as-is, since it is already filtering by both the selected service and location. So let's get rid of this visitQueueEntries and just use the queueEntries returned from the hook.

const currentServiceUuid = useSelectedServiceUuid();
const [showOverlay, setShowOverlay] = useState(false);
const [viewState, setViewState] = useState<{ selectedPatientUuid: string }>(null);
const layout = useLayoutType();
Expand All @@ -144,7 +145,7 @@ function OldQueueTable({ queueEntries }: { queueEntries: QueueEntry[] }) {
new Date(localStorage.getItem('lastUpdatedQueueRoomTimestamp')),
);
const { queueLocations } = useQueueLocations();
const { rooms, isLoading: loading } = useQueueRooms(queueLocations[0]?.id, currentServiceUuid);
const { rooms, isLoading: loading } = useQueueRooms(queueLocations[0]?.id, currentQueueLocation);

const isPermanentProviderQueueRoom = useIsPermanentProviderQueueRoom();
const pageSizes = [10, 20, 30, 40, 50];
Expand Down Expand Up @@ -199,8 +200,11 @@ function OldQueueTable({ queueEntries }: { queueEntries: QueueEntry[] }) {
);

const handleServiceChange = ({ selectedItem }) => {
updateSelectedServiceUuid(selectedItem.uuid);
updateSelectedServiceName(selectedItem.display);
const {
service: { display, uuid },
} = selectedItem ?? {};
updateSelectedServiceUuid(uuid);
updateSelectedServiceName(display);
};

const tableRows = useMemo(() => {
Expand Down
Expand Up @@ -20,12 +20,12 @@ import { type MappedQueueEntry } from '../types';
import { updateQueueEntry } from './active-visits-table.resource';
import { useQueueLocations } from '../patient-search/hooks/useQueueLocations';
import styles from './change-status-dialog.scss';
import { useQueues } from '../helpers/useQueues';
import { useForm, Controller } from 'react-hook-form';
import { useForm, Controller, useWatch } from 'react-hook-form';
import { z } from 'zod';
import { zodResolver } from '@hookform/resolvers/zod';
import { type ConfigObject } from '../config-schema';
import { useMutateQueueEntries } from '../hooks/useMutateQueueEntries';
import { useQueues } from '../hooks/useQueues';

interface ChangeStatusDialogProps {
queueEntry: MappedQueueEntry;
Expand Down Expand Up @@ -59,8 +59,8 @@ const ChangeStatus: React.FC<ChangeStatusDialogProps> = ({ queueEntry, closeModa
defaultValues: { priority: allowedPriorities[1]?.uuid },
resolver: zodResolver(schema),
});

const { queues } = useQueues(queueEntry?.queueLocation);
const selectedLocation = useWatch({ control, name: 'location' });
const { queues } = useQueues(selectedLocation ?? queueEntry?.queueLocation);
const { queueLocations } = useQueueLocations();
const { mutateQueueEntries } = useMutateQueueEntries();

Expand Down
Expand Up @@ -61,7 +61,7 @@ const AddProviderQueueRoom: React.FC<AddProviderQueueRoomProps> = ({ providerUui

const { mutate } = useProvidersQueueRoom(providerUuid);
const { queues } = useQueues(currentLocationUuid);
const { rooms } = useQueueRooms(currentLocationUuid, currentServiceUuid);
const { rooms } = useQueueRooms(currentLocationUuid);
const { queueLocations } = useQueueLocations();
const [isMissingQueueRoom, setIsMissingQueueRoom] = useState(false);

Expand Down
Expand Up @@ -2,7 +2,7 @@ import { openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
import useSWR from 'swr';
import { type ProvidersQueueRoom, type QueueRoom } from '../types';

export function useQueueRooms(location: string, queueUuid: string) {
export function useQueueRooms(location: string, queueUuid: string = '') {
Copy link
Contributor

Choose a reason for hiding this comment

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

This technically works, but we should use null to denote absence of a value instead of empty string.

const apiUrl = queueUuid
? `${restBaseUrl}/queueroom?location=${location}&queue=${queueUuid}`
: `${restBaseUrl}/queueroom?location=${location}`;
Expand Down
2 changes: 1 addition & 1 deletion packages/esm-service-queues-app/src/helpers/useQueues.ts
Expand Up @@ -5,7 +5,7 @@ import { type QueueServiceInfo } from '../types';
// deprecated. use useQueues from ../hooks
export function useQueues(locationUuid?: string) {
const customRepresentation =
'custom:(uuid,display,name,description,allowedPriorities:(uuid,display),allowedStatuses:(uuid,display),location:(uuid,display))';
'custom:(uuid,display,name,description,service:(uuid,display),allowedPriorities:(uuid,display),allowedStatuses:(uuid,display),location:(uuid,display))';
const apiUrl = `${restBaseUrl}/queue?v=${customRepresentation}` + (locationUuid ? `&location=${locationUuid}` : '');

const { data } = useSWRImmutable<{ data: { results: Array<QueueServiceInfo> } }, Error>(apiUrl, openmrsFetch);
Expand Down
2 changes: 1 addition & 1 deletion packages/esm-service-queues-app/src/hooks/useQueues.ts
Expand Up @@ -4,7 +4,7 @@ import { type Queue } from '../types';

export function useQueues(locationUuid?: string) {
const customRepresentation =
'custom:(uuid,display,name,description,allowedPriorities:(uuid,display),allowedStatuses:(uuid,display),location:(uuid,display))';
'custom:(uuid,display,name,description,service:(uuid,display),allowedPriorities:(uuid,display),allowedStatuses:(uuid,display),location:(uuid,display))';
const apiUrl = `${restBaseUrl}/queue?v=${customRepresentation}` + (locationUuid ? `&location=${locationUuid}` : '');

const { data, ...rest } = useSWRImmutable<{ data: { results: Array<Queue> } }, Error>(apiUrl, openmrsFetch);
Expand Down
Expand Up @@ -13,8 +13,9 @@ import {
import { useActiveVisits, useAverageWaitTime } from './clinic-metrics.resource';
import { useServiceMetricsCount } from './queue-metrics.resource';
import styles from './clinic-metrics.scss';
import { useQueues } from '../helpers/useQueues';
import { useQueueEntries } from '../hooks/useQueueEntries';
import unionBy from 'lodash/unionBy';
import { useQueues } from '../hooks/useQueues';

export interface Service {
uuid: string;
Expand All @@ -26,6 +27,7 @@ function ClinicMetrics() {

const currentQueueLocation = useSelectedQueueLocationUuid();
const { queues } = useQueues(currentQueueLocation);
const uniqueQueues = unionBy(queues ?? [], ({ service }) => service?.uuid);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm guessing we need this because there can be multiple queues for the same service?

const currentServiceUuid = useSelectedServiceUuid();
const currentServiceName = useSelectedServiceName();
const { serviceCount } = useServiceMetricsCount(currentServiceUuid, currentQueueLocation);
Expand All @@ -39,15 +41,15 @@ function ClinicMetrics() {
}
});
const { queueEntries, totalCount } = useQueueEntries({
queue: currentServiceUuid,
service: currentServiceUuid,
location: currentQueueLocation,
isEnded: false,
});
const { activeVisitsCount, isLoading: loading } = useActiveVisits();
const { waitTime } = useAverageWaitTime(currentServiceUuid, '');

const handleServiceChange = ({ selectedItem }) => {
updateSelectedServiceUuid(selectedItem.uuid);
updateSelectedServiceUuid(selectedItem?.service?.uuid);
updateSelectedServiceName(selectedItem.display);
if (selectedItem.uuid == undefined) {
setInitialSelectItem(true);
Expand Down Expand Up @@ -77,7 +79,7 @@ function ClinicMetrics() {
id="inline"
type="inline"
label={currentServiceName ?? `${t('all', 'All')}`}
items={[{ display: `${t('all', 'All')}` }, ...queues]}
items={[{ display: `${t('all', 'All')}` }, ...uniqueQueues]}
itemToString={(item) => (item ? item.display : '')}
onChange={handleServiceChange}
/>
Expand Down