This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a54b63
commit 273b000
Showing
12 changed files
with
196 additions
and
156 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 |
---|---|---|
@@ -1,45 +1,61 @@ | ||
import { patients } from "../../config/pouchdb"; | ||
import { patients } from '../../config/pouchdb' | ||
import Patient from 'model/Patient'; | ||
|
||
export async function getAll() { | ||
return patients.allDocs({ include_docs: true }); | ||
function mapRowToPatient(row: any): Patient { | ||
return new Patient(row.id, row.value.rev, row.doc.firstName, row.doc.lastName); | ||
} | ||
|
||
function mapDocumentToPatient(document: any): Patient { | ||
return new Patient(document._id, document._rev, document.firstName, document.lastName); | ||
} | ||
|
||
export async function getAll(): Promise<Patient[]> { | ||
const allPatients = await patients.allDocs({ | ||
include_docs: true, | ||
}); | ||
|
||
return allPatients.rows.map(r => { | ||
const row = r as any; | ||
return mapRowToPatient(row) | ||
}); | ||
} | ||
|
||
export async function deleteDocument(document: any) { | ||
return patients.remove(document); | ||
return patients.remove(document) | ||
} | ||
|
||
export async function deleteDocumentById(id: string, revId: string) { | ||
return patients.remove(id, revId); | ||
return patients.remove(id, revId) | ||
} | ||
|
||
export async function saveOrUpdate(document: any) { | ||
export async function saveOrUpdate(patient: Patient) { | ||
try { | ||
const existingDocument = await patients.get(document.id); | ||
// try and get a patient, if the patient is missing it will throw an error | ||
// and have a status of 404. | ||
await patients.get(patient.id) | ||
const { id, rev, ...restOfPatient} = patient; | ||
const updatedDcoument = { | ||
_id: existingDocument._id, | ||
_rev: existingDocument._rev, | ||
...document | ||
}; | ||
return patients.put(updatedDcoument); | ||
_id: id, | ||
_rev: rev, | ||
...restOfPatient, | ||
} | ||
|
||
return patients.put(updatedDcoument) | ||
} catch (error) { | ||
if (error.status === 404) { | ||
return save(document); | ||
if (error.status !== 404) { | ||
throw Error(error); | ||
} | ||
} | ||
|
||
return null; | ||
return save(patient) | ||
} | ||
} | ||
|
||
export async function save(document: any) { | ||
return patients.post(document); | ||
export async function save(patient: Patient): Promise<Patient> { | ||
const newPatient = await patients.post(patient) | ||
return get(newPatient.id); | ||
} | ||
|
||
export async function get(id: string) { | ||
const document = await patients.get(id); | ||
const patient = document as any; | ||
return { | ||
id: patient._id, | ||
firstName: patient.firstName, | ||
lastName: patient.lastName | ||
}; | ||
export async function get(id: string): Promise<Patient> { | ||
const document = await patients.get(id) | ||
return mapDocumentToPatient(document as any); | ||
} |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import PouchDB from "pouchdb"; | ||
import PouchDB from 'pouchdb' | ||
|
||
export const patients = new PouchDB("patients"); | ||
export const patients = new PouchDB('patients') |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import React from "react"; | ||
import React from 'react' | ||
|
||
const Dashboard: React.FC = () => <h1>Hello Dashboard</h1>; | ||
const Dashboard: React.FC = () => <h1>Hello Dashboard</h1> | ||
|
||
export default Dashboard; | ||
export default Dashboard |
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
Oops, something went wrong.