Skip to content

Commit

Permalink
Added preferred hospital functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
paolo7 committed Jul 30, 2024
1 parent 0e7ff8d commit ccf786b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/api/patientsAPI/patientsAPI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ export default {
request(METHODS.POST, '/patient-hospital-mappings/', { params }),
dischargePatient: (params: DischargePayload) => request(METHODS.POST, `/discharges/`, { params }),
followUpPatient: (params: FollowUpPayload) => request(METHODS.POST, `/follow-ups/`, { params }),
getPreferredHospital: () => request(METHODS.GET, '/preferred-hospital/retrieve_for_current_user/', {}),
};
18 changes: 17 additions & 1 deletion src/hooks/api/patientHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
PaginationParams,
PatientAPI,
PatientsPayload,
PatientsResponse,
PatientsResponse, PreferredHospital,
RegisterEpisodePayload,
RegisterPatientPayload,
SurgeonsResponse,
Expand All @@ -43,6 +43,22 @@ export const useGetHospitals = (params?: PaginationParams) => {
);
};

export const useGetPreferredHospital = () => {
return useQuery<PreferredHospital, AxiosError, PreferredHospital>(
ReactQueryKeys.PreferredHospitalQuery,
async () => {
const { request } = patientsAPI.single.getPreferredHospital();
return await request();
},
{
onError: (error) => {
console.error("Error fetching preferred hospital:", error);
},
retry: false,
}
);
};

export const useGetHospital = (id: string) => {
return useQuery<HospitalsAPI, AxiosError, HospitalsAPI>(
[ReactQueryKeys.HospitalsQuery, id],
Expand Down
1 change: 1 addition & 0 deletions src/hooks/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export const ReactQueryKeys = {
HospitalsQuery: 'hospitalsQuery',
SurgeonsQuery: 'surgeonsQuery',
EpisodesQuery: 'episodesQuery',
PreferredHospitalQuery: 'preferredHospital',
};
6 changes: 6 additions & 0 deletions src/models/apiTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,9 @@ export type HospitalMappingPayload = {
hospital_id: number;
patient_hospital_id: string;
};

export interface PreferredHospital {
hospital: {
id: number;
};
}
23 changes: 16 additions & 7 deletions src/pages/PatientDirectory/PatientDirectory.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/** @jsxImportSource @emotion/react */
import React, { useEffect, useState } from 'react';
import React, { useEffect, useState, useRef } from 'react';

import { IconButton, Filter } from '@orfium/ictinus';
import { FilterOption } from '@orfium/ictinus/dist/components/Filter/types';
import { ReactComponent as SortIcon } from 'assets/PatientDirectory/sortIcon.svg';
import { PageWrapper, PageTitle } from 'common.style';
import { useGetHospitals, useGetPatients } from 'hooks/api/patientHooks';
import { useGetHospitals, useGetPatients, useGetPreferredHospital } from 'hooks/api/patientHooks';
import { getHospitalOptions } from 'pages/RegisterPatient/utils';
import { useHistory } from 'react-router';
import urls from 'routing/urls';
Expand All @@ -20,7 +20,6 @@ import { SortingOptionsType } from './types';
const PatientDirectory: React.FC<{ searchTerm?: string }> = ({ searchTerm }) => {
const { isDesktop } = useResponsiveLayout();
const [hospitalId, setHospitalId] = useState<number>();

const [sortingOption, setSortingOption] = useState<SortingOptionsType>(
(localStorage.getItem('sortingOption') as SortingOptionsType) || '-created_at'
);
Expand All @@ -37,12 +36,22 @@ const PatientDirectory: React.FC<{ searchTerm?: string }> = ({ searchTerm }) =>

const { data: hospitals } = useGetHospitals({ offset: 0, limit: 100 });

const { data: preferredHospital } = useGetPreferredHospital();

const isFirstLoad = useRef(true);

useEffect(() => {
if (hospitals) {
setHospitalId(hospitals?.results[0].id);
setSelectedOption(hospitals?.results[0].id);
if (hospitals && isFirstLoad.current) {
if (preferredHospital && preferredHospital.hospital) {
setHospitalId(preferredHospital.hospital.id);
setSelectedOption(preferredHospital.hospital.id);
isFirstLoad.current = false;
} else {
setHospitalId(hospitals?.results[0].id);
setSelectedOption(hospitals?.results[0].id);
}
}
}, [hospitals]);
}, [hospitals, preferredHospital]);

useEffect(() => {
if (sortingOption) {
Expand Down

0 comments on commit ccf786b

Please sign in to comment.