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

Recurrence Field and Preferred Hospital features #253

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ const FollowUps: FC<{
<Field
name="recurrence"
initialValue={
followUp?.recurrence !== undefined
followUp?.recurrence != null
? BOOLEAN_OPTIONS.find((option) =>
followUp?.recurrence ? option.label === 'Yes' : option.label === 'No'
)
Expand Down
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
Loading