Skip to content

Commit

Permalink
feat: Add successfully posting forms from client
Browse files Browse the repository at this point in the history
  • Loading branch information
ezrajames committed Jul 18, 2022
1 parent 6a31f89 commit 5693c23
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
6 changes: 3 additions & 3 deletions app/api/schemas/patient_encounter.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from datetime import datetime
from typing import List
from typing import List, Optional
from pydantic import BaseModel


class PatientEncounterSchema(BaseModel):
"""
An encounter with a given patient.
"""
patient_rfid: str
patient_rfid: Optional[str]
document_num: str
location: str
handover_from: str
Expand All @@ -21,7 +21,7 @@ class PatientEncounterSchema(BaseModel):
departure_time: datetime
departure_dest: str
comment: str
qr_code: str
qr_code: Optional[str]
document_num: str

class Config:
Expand Down
6 changes: 4 additions & 2 deletions app/web/pages/forms/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
import { useAuth, ProtectedRoute } from '../../contexts/auth';
import ProtectedNavbar from "../../components/protected_navbar";

import { submit_form } from "../../utils/api";
import { submitPatientEncounterForm } from "../../utils/api";

const MFPEFormData = {
patient_rfid: '',
Expand Down Expand Up @@ -67,8 +67,9 @@ function MFPEForm() {
setError(false);
setErrorMessage("");
const data = formValues;
const token = `Bearer ${window.localStorage.getItem("auth-token")}`;

const errorMessage = await submit_form(data);
const errorMessage = await submitPatientEncounterForm(data, token);
if (!errorMessage) {
window.location.pathname = "/forms/form";
} else {
Expand Down Expand Up @@ -278,6 +279,7 @@ function MFPEForm() {
value={formValues.comment}
onChange={handleChange} />
</Grid>
{hasError && <p style={{ color: "red" }}>{errorMessage}</p>}
<Grid item xs={12}>
<Button type="submit" fullWidth={true} variant="contained">Submit</Button>
</Grid>
Expand Down
16 changes: 9 additions & 7 deletions app/web/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ export async function login(email, password) {
}
}

export async function submit_form(formData) {
export async function submitPatientEncounterForm(formData, token) {
try {
const response = await fetch(`${process.env.NEXT_PUBLIC_HOSTNAME}/create-patient-encounter`, {
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
"Content-Type": "application/json",
"Authorization": token
},
method: "POST",
mode: 'cors',
Expand All @@ -86,8 +87,9 @@ export async function submit_form(formData) {
date: formData.date,
arrival_time: formData.arrival_time,
triage_acuity: formData.triage_acuity,
on_shift: formData.on_shift,
chief_complaints: formData.chief_complaints,
// Hack to convert on_shift to a boolean
on_shift: formData.on_shift === "Yes" ? true : false,
chief_complaints: formData.chief_complaints.join(" "),
arrival_method: formData.arrival_method,
handover_too: formData.handover_too,
departure_time: formData.departure_time,
Expand All @@ -102,15 +104,15 @@ export async function submit_form(formData) {
window.localStorage.setItem("auth-token", response_data['access_token']);
return "";
} else {
console.error("Malformed request from server: ", response_data);
return "Malformed request from server, please try again and contact support if issue persists.";
console.error("Malformed response from server: ", response_data);
return "Malformed response from server, please try again and contact support if issue persists.";
}
} else {
console.error("Complete error received from server: ", response_data);
if (typeof response_data?.detail === "string") {
return response_data?.detail;
} else {
return response_data?.detail[0].msg;
return `${response_data?.detail[0]?.loc[1]}: ${response_data?.detail[0].msg}`;
}
}
} catch (error) {
Expand Down

0 comments on commit 5693c23

Please sign in to comment.