Skip to content

Commit 350d16e

Browse files
authored
Merge pull request #47 from swiftss-org/feat/patient-directory-api
feat(patients-directory): api implementation
2 parents 9d63abc + 1d0437a commit 350d16e

File tree

8 files changed

+105
-122
lines changed

8 files changed

+105
-122
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
2-
import { PaginationParams, RegisterPatientPayload } from '../../models/apiTypes';
2+
import { RegisterPatientPayload, PatientsPayload, PaginationParams } from '../../models/apiTypes';
33
import { METHODS, request } from '../axiosInstances';
44

55
export default {
66
getHospitals: (params?: PaginationParams) => request(METHODS.GET, '/hospitals/', { params }),
7+
getPatients: (params?: PatientsPayload) => request(METHODS.GET, '/patients/', { params }),
78
registerPatient: (params: RegisterPatientPayload) =>
89
request(METHODS.POST, '/patients/', { params }),
910
};

src/hooks/api/patientHooks.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { useMutation, useQuery } from 'react-query';
44
import { useHistory } from 'react-router-dom';
55

66
import patientsAPI from '../../api/patientsAPI';
7-
import { HospitalsResponse, PaginationParams, RegisterPatientPayload } from '../../models/apiTypes';
7+
import {
8+
HospitalsResponse,
9+
PaginationParams,
10+
PatientsPayload,
11+
PatientsResponse,
12+
RegisterPatientPayload,
13+
} from '../../models/apiTypes';
814
import { RegisterPatientFormType } from '../../pages/RegisterPatient/types';
915
import urls from '../../routing/urls';
1016

@@ -27,6 +33,31 @@ export const useGetHospitals = (params?: PaginationParams) => {
2733
);
2834
};
2935

36+
export const useGetPatients = (params?: PatientsPayload) => {
37+
return useQuery<PatientsResponse, AxiosError, PatientsResponse>(
38+
[
39+
ReactQueryKeys.PatientsQuery,
40+
params?.hospital_id,
41+
params?.limit,
42+
params?.offset,
43+
params?.search_term,
44+
],
45+
async () => {
46+
const { request } = patientsAPI.single.getPatients(params);
47+
const data = await request();
48+
49+
return data;
50+
},
51+
{
52+
onError: (errors) => {
53+
console.log(errors);
54+
},
55+
56+
retry: false,
57+
}
58+
);
59+
};
60+
3061
export const useRegisterPatient = () => {
3162
const history = useHistory();
3263

src/models/apiTypes.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,26 @@ export interface RegisterPatientPayload {
4848
address: string;
4949
hospital_id: number;
5050
}
51+
52+
export interface PatientsPayload extends PaginationParams {
53+
hospital_id?: number;
54+
search_term?: string;
55+
}
56+
57+
export type PatientAPI = {
58+
full_name: string;
59+
national_id: string;
60+
age: number;
61+
day_of_birth: string;
62+
month_of_birth: string;
63+
year_of_birth: string;
64+
gender: 'Male' | 'Female';
65+
phone_1: string;
66+
phone_2: string;
67+
address: string;
68+
hospitals: HospitalsAPI[];
69+
};
70+
71+
export interface PatientsResponse extends PaginationResponse, PaginationParams {
72+
results: PatientAPI[];
73+
}

src/pages/PatientDirectory/PatientDirectory.tsx

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import React from 'react';
22

33
import { TextField, Select } from '@orfium/ictinus';
4+
import { SelectOption } from '@orfium/ictinus/dist/components/Select/Select';
5+
import { useGetHospitals, useGetPatients } from 'hooks/api/patientHooks';
6+
import { getHospitalOptions } from 'pages/RegisterPatient/utils';
47

58
import PatientCard from './components/PatientCard';
6-
import { Patients } from './constants';
79
import {
810
PatientDirectoryContainer,
911
SearchWrapper,
@@ -12,31 +14,53 @@ import {
1214
} from './PatientDirectory.style';
1315

1416
const PatientDirectory: React.FC = () => {
17+
/** TODO: debounce search */
18+
const [searchTerm, setSearchTerm] = React.useState<string>('');
19+
const [hospitalId, setHospitalId] = React.useState<number>();
20+
21+
const { data: patients } = useGetPatients({
22+
offset: 0,
23+
limit: 100,
24+
search_term: searchTerm,
25+
hospital_id: hospitalId,
26+
});
27+
28+
const { data: hospitals } = useGetHospitals({ offset: 0, limit: 100 });
29+
1530
return (
1631
<PatientDirectoryContainer>
1732
<SearchWrapper>
1833
<TextField
1934
type={'outlined'}
2035
placeholder={'Search (Name , ID, Patient Hospital ID ...)'}
2136
leftIcon={'search'}
37+
onChange={(event: React.ChangeEvent<HTMLInputElement>) =>
38+
setSearchTerm(event.target.value)
39+
}
40+
value={searchTerm}
2241
/>
2342
<Select
2443
label="Center"
2544
styleType="outlined"
2645
size="sm"
2746
required
28-
options={[{ label: 'Hospital Number 1', value: 1 }]}
47+
options={getHospitalOptions(hospitals?.results || [])}
48+
handleSelectedOption={(option: SelectOption) =>
49+
setHospitalId(parseInt(option.value.toString()))
50+
}
2951
/>
3052
</SearchWrapper>
3153

32-
<PatientsList>
33-
{Patients.map((patient, index) => (
34-
<>
35-
<PatientCard key={'patient' + index} {...patient} />
36-
<Line />
37-
</>
38-
))}
39-
</PatientsList>
54+
{patients && (
55+
<PatientsList>
56+
{patients.results.map((patient) => (
57+
<>
58+
<PatientCard {...patient} />
59+
<Line key={'patient_line_' + patient.full_name} />
60+
</>
61+
))}
62+
</PatientsList>
63+
)}
4064
</PatientDirectoryContainer>
4165
);
4266
};

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22

33
import { Chip, Icon } from '@orfium/ictinus';
4-
import { Patient } from 'pages/PatientDirectory/types';
4+
import { PatientAPI } from 'models/apiTypes';
55

66
import {
77
CardContainer,
@@ -16,28 +16,30 @@ import {
1616
ViewMore,
1717
} from './PatientCard.style';
1818

19-
const PatientCard: React.FC<Patient> = ({ name, gender, age, hospital, patientHospitalId, id }) => {
19+
const PatientCard: React.FC<PatientAPI> = ({ full_name, gender, age, national_id, hospitals }) => {
2020
return (
21-
<CardContainer>
21+
<CardContainer key={'patient' + full_name}>
2222
<Header>
2323
<Info>
24-
{name} , {gender} , {age}
24+
{full_name} , {gender} , {age}
2525
</Info>
2626
<ChipWrapper>
2727
<Chip styleType={'filled'} size={'sm'} fill={'lightGray'} shade={300}>
28-
{hospital}
28+
{/** TODO: change this! */}
29+
{hospitals[0].name}
2930
</Chip>
3031
</ChipWrapper>
3132
</Header>
3233
<Footer>
3334
<CardItemsContainer>
3435
<CardItemContainer>
3536
<CardLabel>Patient Hospital ID</CardLabel>
36-
<CardValue>{patientHospitalId}</CardValue>
37+
{/** TODO: change this! */}
38+
<CardValue>{hospitals[0].patient_hospital_id}</CardValue>
3739
</CardItemContainer>
3840
<CardItemContainer>
3941
<CardLabel>ID</CardLabel>
40-
<CardValue>{id}</CardValue>
42+
<CardValue>{national_id}</CardValue>
4143
</CardItemContainer>
4244
</CardItemsContainer>
4345
<ViewMore onClick={() => console.log('view more')}>

src/pages/PatientDirectory/constants.ts

Lines changed: 0 additions & 92 deletions
This file was deleted.

src/pages/PatientDirectory/types.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/routing/Routes.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ import React from 'react';
33
import Login from 'pages/Login';
44
import PatientDirectory from 'pages/PatientDirectory';
55
import RegisterPatient from 'pages/RegisterPatient';
6-
import { Switch } from 'react-router-dom';
6+
import { Redirect, Switch } from 'react-router-dom';
77

8+
import PrivateRoute from './PrivateRoute';
89
import PublicRoute from './PublicRoute';
910
import urls from './urls';
1011

1112
const Routes: React.FC = () => (
1213
<Switch>
1314
<PublicRoute exact path={urls.login()} component={Login} />
14-
<PublicRoute exact path={urls.register()} component={RegisterPatient} />
15-
<PublicRoute exact path={[urls.patients(), '/']} component={PatientDirectory} />
15+
<PrivateRoute exact path={urls.register()} component={RegisterPatient} />
16+
<PrivateRoute exact path={[urls.patients()]} component={PatientDirectory} />
17+
<Redirect to={urls.patients()} />
1618
</Switch>
1719
);
1820

0 commit comments

Comments
 (0)