Skip to content

Commit 42266a6

Browse files
author
whosyourtaco
committed
feat: get patient hospital ids from hospital mapping object
1 parent b190016 commit 42266a6

File tree

5 files changed

+39
-22
lines changed

5 files changed

+39
-22
lines changed

src/hooks/api/patientHooks.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ export const useGetHospitals = (params?: PaginationParams) => {
1919
ReactQueryKeys.HospitalsQuery,
2020
async () => {
2121
const { request } = patientsAPI.single.getHospitals(params);
22-
const data = await request();
23-
24-
return data;
22+
return await request();
2523
},
2624
{
2725
onError: (errors) => {
@@ -44,10 +42,11 @@ export const useGetPatients = (params?: PatientsPayload) => {
4442
params?.ordering,
4543
],
4644
async () => {
45+
if (params?.hospital_id === undefined) {
46+
return undefined;
47+
}
4748
const { request } = patientsAPI.single.getPatients(params);
48-
const data = await request();
49-
50-
return data;
49+
return await request();
5150
},
5251
{
5352
onError: (errors) => {

src/models/apiTypes.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ export interface LoginResponse {
3030

3131
export type HospitalsAPI = {
3232
id: number;
33-
name: string;
34-
address: string;
35-
patient_hospital_id: number;
33+
hospital_id?: number;
34+
name?: string;
35+
address?: string;
36+
patient_hospital_id?: number;
3637
};
3738

3839
export interface HospitalsResponse extends PaginationResponse, PaginationParams {
@@ -68,7 +69,7 @@ export type PatientAPI = {
6869
phone_1: string;
6970
phone_2: string;
7071
address: string;
71-
hospitals: HospitalsAPI[];
72+
hospital_mappings: HospitalsAPI[];
7273
};
7374

7475
export interface PatientsResponse extends PaginationResponse, PaginationParams {

src/pages/PatientDirectory/PatientDirectory.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,21 @@ const PatientDirectory: React.FC<{ searchTerm?: string }> = ({ searchTerm }) =>
2828
ordering: sortingOption,
2929
});
3030

31-
const [showSortingOptions, setshowSortingOptions] = useState(false);
31+
const [showSortingOptions, setShowSortingOptions] = useState(false);
3232

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

3535
useEffect(() => {
3636
if (hospitals) {
3737
setHospitalId(hospitals?.results[0].id);
38+
setSelectedOption(hospitals?.results[0].id);
3839
}
3940
}, [hospitals]);
4041

4142
const history = useHistory();
4243

4344
const filterOptions = getHospitalOptions(hospitals?.results || []);
44-
const [selectedOption, setSelectedOption] = useState(filterOptions?.[0]?.value);
45+
const [selectedOption, setSelectedOption] = useState<number>();
4546

4647
return (
4748
<>
@@ -51,26 +52,26 @@ const PatientDirectory: React.FC<{ searchTerm?: string }> = ({ searchTerm }) =>
5152
<Filter
5253
label="Center"
5354
items={filterOptions}
54-
defaultValue={filterOptions.length > 0 ? filterOptions[0] : { label: '', value: '' }}
55+
defaultValue={filterOptions.length > 0 ? filterOptions[0] : { label: '', value: -1 }}
5556
selectedItem={filterOptions.find((option) => option.value === selectedOption)}
5657
onSelect={(option: FilterOption) => {
57-
setSelectedOption(option.value);
58+
setSelectedOption(option.value as number);
5859
setHospitalId(parseInt(option.value.toString()));
5960
}}
6061
styleType="transparent"
6162
buttonType="primary"
6263
/>
63-
<SortIcon onClick={() => setshowSortingOptions(!showSortingOptions)} />
64+
<SortIcon onClick={() => setShowSortingOptions(!showSortingOptions)} />
6465
</OptionsWrapper>
6566

6667
{patients && (
6768
<PatientsList>
6869
{patients.results.map((patient) => (
6970
<div
70-
key={`patient_${patient.national_id}_${patient.hospitals?.[0]?.id}`}
71+
key={`patient_${patient.national_id}_${selectedOption}`}
7172
css={{ marginBottom: '8px' }}
7273
>
73-
<PatientCard {...patient} />
74+
<PatientCard {...patient} selectedHospital={selectedOption} />
7475
</div>
7576
))}
7677
</PatientsList>
@@ -92,7 +93,7 @@ const PatientDirectory: React.FC<{ searchTerm?: string }> = ({ searchTerm }) =>
9293
title={'Sort by options:'}
9394
sortingOption={sortingOption}
9495
onSortingOptionChange={(option: SortingOptionsType) => setSortingOption(option)}
95-
onClose={() => setshowSortingOptions(false)}
96+
onClose={() => setShowSortingOptions(false)}
9697
/>
9798
)}
9899
</>

src/pages/PatientDirectory/components/PatientCard/PatientCard.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@ import { PatientAPI } from 'models/apiTypes';
66

77
import { CardContainer, IdLabel, IdValue, Subtitle, Title } from './PatientCard.style';
88

9-
const PatientCard: React.FC<PatientAPI> = ({ full_name, gender, age, national_id, hospitals }) => {
9+
type Props = PatientAPI & { selectedHospital?: number };
10+
11+
const PatientCard: React.FC<Props> = ({
12+
full_name,
13+
gender,
14+
age,
15+
national_id,
16+
hospital_mappings,
17+
selectedHospital,
18+
}) => {
19+
console.log(selectedHospital);
20+
console.log(hospital_mappings);
1021
const theme = useTheme();
1122
return (
1223
<CardContainer key={'patient_' + national_id}>
@@ -20,7 +31,12 @@ const PatientCard: React.FC<PatientAPI> = ({ full_name, gender, age, national_id
2031
</Subtitle>
2132
<div css={{ display: 'flex', justifyContent: 'space-between', marginBottom: '8px' }}>
2233
<IdLabel>Patient Hospital ID:</IdLabel>
23-
<IdValue>{hospitals?.[0]?.patient_hospital_id}</IdValue>
34+
<IdValue>
35+
{
36+
hospital_mappings.find((hospital) => hospital?.hospital_id === selectedHospital)
37+
?.patient_hospital_id
38+
}
39+
</IdValue>
2440
</div>
2541
<div css={{ display: 'flex', justifyContent: 'space-between' }}>
2642
<IdLabel>National ID:</IdLabel>

src/pages/RegisterPatient/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const REQUIRED_FIELD_MSG = 'This field is required';
77

88
export const getHospitalOptions = (hospitals: HospitalsAPI[]): FilterOption[] => {
99
return hospitals.map((hospital) => ({
10-
label: hospital.name,
11-
value: hospital.id,
10+
label: hospital?.name ?? '',
11+
value: hospital?.id,
1212
}));
1313
};
1414

0 commit comments

Comments
 (0)