-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor API call for patients into separate file
- Loading branch information
Showing
2 changed files
with
93 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { useState } from 'react'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { StatusCodes } from 'http-status-codes'; | ||
import LifelineAPI from './LifelineAPI'; | ||
|
||
/** | ||
* Patient data | ||
* @typedef {object} Patient | ||
* @property {string} id - Patient ID | ||
* @property {string} name - Patient first name, middle name and last name | ||
* @property {string} createdBy - Lifeline user first name, middle name and last name | ||
* @property {string} createdAt - Date patient profile was created | ||
* @property {string} updatedBy - Lifeline user first name, middle name and last name | ||
* @property {string} updatedAt - Date patient profile was last updated | ||
*/ | ||
|
||
/** | ||
* Table headers | ||
* @typedef {object} TableHeader | ||
* @property {string} key - Table header key corresponding to the data key | ||
* @property {string} text - Table header text to be displayed | ||
*/ | ||
|
||
/** | ||
* | ||
* @returns {{ | ||
* patients: Array<Patient>, | ||
* headers: Array<TableHeader>, | ||
* search: string, | ||
* setSearch: (search: string) => void, | ||
* page: number, | ||
* setPage: (page: number) => void, | ||
* isFetching: boolean, | ||
* }} | ||
*/ | ||
export function usePatients() { | ||
const [search, setSearch] = useState(''); | ||
const [page, setPage] = useState(1); | ||
const { data: patients, isFetching } = useQuery({ | ||
queryKey: ['patients', search, page], | ||
queryFn: async () => { | ||
const res = await LifelineAPI.getPatients(search, page); | ||
if (res.status === StatusCodes.OK) { | ||
return await res.json(); | ||
} else { | ||
throw new Error('Failed to fetch patients.'); | ||
} | ||
}, | ||
}); | ||
|
||
const PATIENT_TABLE_HEADERS = [ | ||
{ key: 'name', text: 'Name' }, | ||
{ key: 'createdBy', text: 'Created by' }, | ||
{ key: 'createdAt', text: 'Date created' }, | ||
{ key: 'updatedBy', text: 'Updated by' }, | ||
{ key: 'updatedAt', text: 'Last updated' }, | ||
{ key: 'more', text: '' }, | ||
]; | ||
|
||
let formattedData = []; | ||
if (patients) { | ||
formattedData = patients.map((patient) => { | ||
return { | ||
id: patient.id, | ||
name: `${patient.firstName}${patient.middleName ? ` ${patient.middleName}` : ''} ${patient.lastName}`, | ||
createdBy: `${patient.createdBy.firstName}${patient.createdBy.middleName ? patient.createdBy.middleName + ' ' : ''}${patient.createdBy.lastName}`, | ||
createdAt: new Date(patient.createdAt).toLocaleDateString(undefined, { | ||
month: 'long', | ||
day: 'numeric', | ||
year: 'numeric', | ||
}), | ||
updatedBy: `${patient.createdBy.firstName}${patient.createdBy.middleName ? patient.createdBy.middleName + ' ' : ''}${patient.createdBy.lastName}`, | ||
updatedAt: new Date(patient.updatedAt).toLocaleDateString(undefined, { | ||
month: 'long', | ||
day: 'numeric', | ||
year: 'numeric', | ||
}), | ||
}; | ||
}); | ||
} | ||
return { | ||
patients: formattedData, | ||
headers: PATIENT_TABLE_HEADERS, | ||
search, | ||
setSearch, | ||
page, | ||
setPage, | ||
isFetching, | ||
}; | ||
} |