From 1d13532b689cc1b70ce8f76736f65107fb3eb451 Mon Sep 17 00:00:00 2001 From: Jack Meyer Date: Sun, 9 Aug 2020 16:31:43 -0500 Subject: [PATCH] chore: add hooks for use patient and use patients --- src/patients/hooks/usePatient.tsx | 12 ++++++++++++ src/patients/hooks/usePatients.tsx | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/patients/hooks/usePatient.tsx create mode 100644 src/patients/hooks/usePatients.tsx diff --git a/src/patients/hooks/usePatient.tsx b/src/patients/hooks/usePatient.tsx new file mode 100644 index 0000000000..861a8a3bf6 --- /dev/null +++ b/src/patients/hooks/usePatient.tsx @@ -0,0 +1,12 @@ +import { useQuery } from 'react-query' + +import PatientRepository from '../../shared/db/PatientRepository' +import Patient from '../../shared/model/Patient' + +function fetchPatient(_: any, id: string): Promise { + return PatientRepository.find(id) +} + +export default function usePatient(id: string) { + return useQuery(['patient', id], fetchPatient) +} diff --git a/src/patients/hooks/usePatients.tsx b/src/patients/hooks/usePatients.tsx new file mode 100644 index 0000000000..64f42804f0 --- /dev/null +++ b/src/patients/hooks/usePatients.tsx @@ -0,0 +1,20 @@ +import { useQuery } from 'react-query' + +import PatientRepository from '../../shared/db/PatientRepository' +import Patient from '../../shared/model/Patient' +import PatientSearchRequest from '../models/PatientSearchRequest' + +interface PatientsResult { + totalCount: number + patients: Patient[] +} + +async function fetchPatients(_: any, searchRequest: PatientSearchRequest): Promise { + const patients = await PatientRepository.search(searchRequest.queryString) + const totalCount = await PatientRepository.count() + return { totalCount, patients } +} + +export default function usePatients(searchRequest: PatientSearchRequest) { + return useQuery(['patients', searchRequest], fetchPatients) +}