Skip to content

Commit

Permalink
Switch to using year of birth for patient transfer confirmation (#7575)
Browse files Browse the repository at this point in the history
* Switch to using year of birth for patient transfer confirmation

* add max validation

* remove usages of any

* update cypress tests
  • Loading branch information
rithviknishad authored Apr 8, 2024
1 parent 12d4187 commit 4293f1d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 65 deletions.
4 changes: 2 additions & 2 deletions cypress/e2e/patient_spec/patient_registration.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ describe("Patient Creation with consultation", () => {
patientTransfer.clickAdmitPatientRecordButton();
patientTransfer.clickTransferPopupContinueButton();
patientTransfer.clickTransferPatientNameList(patientTransferName);
patientTransfer.clickTransferPatientDob(patientDateOfBirth);
patientTransfer.clickTransferPatientYOB(yearOfBirth);
patientTransfer.clickTransferSubmitButton();
patientTransfer.verifyFacilitySuccessfullMessage();
patientTransfer.clickConsultationCancelButton();
Expand All @@ -263,7 +263,7 @@ describe("Patient Creation with consultation", () => {
patientTransfer.clickAdmitPatientRecordButton();
patientTransfer.clickTransferPopupContinueButton();
patientTransfer.clickTransferPatientNameList(patientTransferName);
patientTransfer.clickTransferPatientDob(patientDateOfBirth);
patientTransfer.clickTransferPatientYOB(yearOfBirth);
patientTransfer.clickTransferSubmitButton();
patientTransfer.verifyFacilityErrorMessage();
});
Expand Down
7 changes: 3 additions & 4 deletions cypress/pageobject/Patient/PatientTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ class PatientTransfer {
cy.get("li[role=option]").contains(facilityName).click();
}

clickTransferPatientDob(dateOfBirth: string) {
cy.get("#dateofbirth-transferform").scrollIntoView();
cy.get("#dateofbirth-transferform").should("be.visible").click();
cy.get("#date-input").click().type(dateOfBirth);
clickTransferPatientYOB(yearOfBirth: string) {
cy.get("#year_of_birth").scrollIntoView();
cy.get("#year_of_birth").should("be.visible").click().type(yearOfBirth);
}

clickTransferSubmitButton() {
Expand Down
123 changes: 64 additions & 59 deletions src/Components/Facility/TransferPatientDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import * as Notification from "../../Utils/Notifications.js";

import { Cancel, Submit } from "../Common/components/ButtonV2";
import { useReducer, useState } from "react";

import DateFormField from "../Form/FormFields/DateFormField";
import { DupPatientModel } from "./models";
import { FieldLabel } from "../Form/FormFields/FormField";
import { OptionsType } from "../../Common/constants";
import { SelectFormField } from "../Form/FormFields/SelectFormField";
import { navigate } from "raviger";
import { dateQueryString } from "../../Utils/utils.js";
import dayjs from "dayjs";
import request from "../../Utils/request/request.js";
import routes from "../../Redux/api.js";
import TextFormField from "../Form/FormFields/TextFormField.js";
import { FieldChangeEvent } from "../Form/FormFields/Utils.js";

interface Props {
patientList: Array<DupPatientModel>;
Expand All @@ -21,9 +17,9 @@ interface Props {
facilityId: string;
}

const initForm: any = {
const initForm = {
patient: "",
date_of_birth: null,
year_of_birth: null,
};

const initError = Object.assign(
Expand All @@ -36,9 +32,6 @@ const initialState = {
errors: { ...initError },
};

const getDate = (value: any) =>
value && dayjs(value).isValid() && dayjs(value).toDate();

const patientFormReducer = (state = initialState, action: any) => {
switch (action.type) {
case "set_form": {
Expand Down Expand Up @@ -69,18 +62,26 @@ const TransferPatientDialog = (props: Props) => {
};
});

const handleChange = (e: any) => {
const form = { ...state.form };
form[e.name] = e.value;
dispatch({ type: "set_form", form });
};

const handleDateChange = (e: any) => {
if (dayjs(e.value).isValid()) {
const form = { ...state.form };
form[e.name] = dateQueryString(e.value);
dispatch({ type: "set_form", form });
const maxYear = new Date().getFullYear();

const handleChange = (e: FieldChangeEvent<unknown>) => {
if (
e.name === "year_of_birth" &&
parseInt((e.value as string) || "0") > maxYear
) {
dispatch({
type: "set_error",
errors: {
...state.errors,
[e.name]: `Cannot be greater than ${maxYear}`,
},
});
return;
}
dispatch({
type: "set_form",
form: { ...state.form, [e.name]: e.value },
});
};

const validateForm = () => {
Expand All @@ -94,9 +95,14 @@ const TransferPatientDialog = (props: Props) => {
invalidForm = true;
}
return;
case "date_of_birth":
case "year_of_birth":
if (!state.form[field]) {
errors[field] = "Please enter date in YYYY/MM/DD format";
errors[field] = "This field is required";
invalidForm = true;
}

if (parseInt(state.form[field] || "0") > maxYear) {
errors[field] = `Cannot be greater than ${maxYear}`;
invalidForm = true;
}
return;
Expand All @@ -108,15 +114,14 @@ const TransferPatientDialog = (props: Props) => {
return !invalidForm;
};

const handleSubmit = async (e: any) => {
e.preventDefault();
const handleSubmit = async () => {
const validForm = validateForm();
if (validForm) {
setIsLoading(true);
const { res, data } = await request(routes.transferPatient, {
body: {
facility: facilityId,
date_of_birth: dateQueryString(state.form.date_of_birth),
year_of_birth: state.form.year_of_birth,
},
pathParams: {
id: state.form.patient,
Expand Down Expand Up @@ -156,37 +161,34 @@ const TransferPatientDialog = (props: Props) => {
</p>
</div>
<div className="grid grid-cols-1 gap-4 md:grid-cols-2">
<div>
<FieldLabel required className="text-sm">
Patient
</FieldLabel>
<SelectFormField
id="patient"
name="patient"
required
placeholder="Select patient"
options={patientOptions}
optionLabel={(patient) => patient.text}
optionValue={(patient) => patient.id}
value={state.form.patient}
onChange={handleChange}
error={state.errors.patient}
/>
</div>
<div>
<DateFormField
required
id="dateofbirth-transferform"
name="date_of_birth"
label="Date of birth"
value={getDate(state.form.date_of_birth)}
disableFuture
onChange={handleDateChange}
position="LEFT"
placeholder="Entry Date"
error={state.errors.date_of_birth}
/>
</div>
<SelectFormField
id="patient"
name="patient"
required
label="Patient"
labelClassName="text-sm"
placeholder="Select patient"
options={patientOptions}
optionLabel={(patient) => patient.text}
optionValue={(patient) => patient.id}
value={state.form.patient}
onChange={handleChange}
error={state.errors.patient}
/>
<TextFormField
required
type="number"
id="year_of_birth"
name="year_of_birth"
label="Year of birth"
labelClassName="text-sm"
value={state.form.year_of_birth}
min="1900"
max={maxYear}
onChange={handleChange}
placeholder="Enter year of birth"
error={state.errors.year_of_birth}
/>
</div>
</div>
</div>
Expand All @@ -195,7 +197,10 @@ const TransferPatientDialog = (props: Props) => {
<Submit
id="submit-transferpatient"
disabled={isLoading}
onClick={handleSubmit}
onClick={(e) => {
e.preventDefault();
handleSubmit();
}}
label="Transfer Suspect / Patient"
/>
</div>
Expand Down
5 changes: 5 additions & 0 deletions src/Components/Facility/models.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,11 @@ export type InventoryLogResponse = InventorySummaryResponse & {
created_by: number;
};

export type PatientTransferRequest = {
facility: string;
year_of_birth: string;
};

export type PatientTransferResponse = {
id: string;
patient: string;
Expand Down
2 changes: 2 additions & 0 deletions src/Redux/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import {
PatientNotesEditModel,
PatientNotesModel,
PatientStatsModel,
PatientTransferRequest,
PatientTransferResponse,
StateModel,
WardModel,
Expand Down Expand Up @@ -700,6 +701,7 @@ const routes = {
transferPatient: {
path: "/api/v1/patient/{id}/transfer/",
method: "POST",
TBody: Type<PatientTransferRequest>(),
TRes: Type<PatientTransferResponse>(),
},
getPatientNotes: {
Expand Down

0 comments on commit 4293f1d

Please sign in to comment.