Skip to content

Commit

Permalink
Revert "Delay patient phone duplicate check"
Browse files Browse the repository at this point in the history
This reverts commit 1fc93a7.
  • Loading branch information
Ashesh3 committed Sep 27, 2023
1 parent 1fc93a7 commit 3ca1237
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 50 deletions.
5 changes: 2 additions & 3 deletions src/Components/Form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Props<T extends FormDetails> = {
defaults: T;
asyncGetDefaults?: (() => Promise<T>) | false;
onlyChild?: boolean;
validate?: (form: T) => FormErrors<T> | Promise<FormErrors<T>>;
validate?: (form: T) => FormErrors<T>;
onSubmit: (form: T) => Promise<FormErrors<T> | void>;
onCancel?: () => void;
noPadding?: true;
Expand Down Expand Up @@ -47,8 +47,7 @@ const Form = <T extends FormDetails>({
event.stopPropagation();

if (validate) {
const validationResult = await Promise.resolve(validate(state.form));
const errors = omitBy(validationResult, isEmpty) as FormErrors<T>;
const errors = omitBy(validate(state.form), isEmpty) as FormErrors<T>;

if (Object.keys(errors).length) {
dispatch({ type: "set_errors", errors });
Expand Down
98 changes: 51 additions & 47 deletions src/Components/Patient/PatientRegister.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,19 @@ import TextAreaFormField from "../Form/FormFields/TextAreaFormField";
import TextFormField from "../Form/FormFields/TextFormField";
import TransferPatientDialog from "../Facility/TransferPatientDialog";
import countryList from "../../Common/static/countries.json";
import { debounce } from "lodash";

import useAppHistory from "../../Common/hooks/useAppHistory";
import useConfig from "../../Common/hooks/useConfig";
import { useDispatch } from "react-redux";
import { validatePincode } from "../../Common/validation";
import { FormContextValue } from "../Form/FormContext.js";

const Loading = lazy(() => import("../Common/Loading"));
const PageTitle = lazy(() => import("../Common/PageTitle"));

// const debounce = require("lodash.debounce");

interface PatientRegisterProps extends PatientModel {
facilityId: string;
}
Expand Down Expand Up @@ -547,53 +552,53 @@ export const PatientRegister = (props: PatientRegisterProps) => {
fetchFacilityName();
}, [dispatchAction, facilityId]);

const validateForm = async (form: any) => {
const validateForm = (form: any) => {
const errors: Partial<Record<keyof any, FieldError>> = {};

const insuranceDetailsError = insuranceDetails
.map((policy) => HCXPolicyValidator(policy, enable_hcx))
.find((error) => !!error);
setInsuranceDetailsError(insuranceDetailsError);

for (const field of Object.keys(form)) {
Object.keys(form).forEach((field) => {
let phoneNumber, emergency_phone_number;
switch (field) {
case "address":
case "name":
case "gender":
case "date_of_birth":
errors[field] = RequiredFieldValidator()(form[field]);
break;
return;
case "permanent_address":
if (!form.sameAddress) {
errors[field] = RequiredFieldValidator()(form[field]);
}
break;
return;
case "local_body":
if (form.nationality === "India" && !Number(form[field])) {
errors[field] = "Please select a localbody";
}
break;
return;
case "district":
if (form.nationality === "India" && !Number(form[field])) {
errors[field] = "Please select district";
}
break;
return;
case "state":
if (form.nationality === "India" && !Number(form[field])) {
errors[field] = "Please enter the state";
}
break;
return;
case "pincode":
if (!validatePincode(form[field])) {
errors[field] = "Please enter valid pincode";
}
break;
return;
case "passport_no":
if (form.nationality !== "India" && !form[field]) {
errors[field] = "Please enter the passport number";
}
break;
return;
case "phone_number":
phoneNumber = parsePhoneNumber(form[field]);
if (
Expand All @@ -603,10 +608,7 @@ export const PatientRegister = (props: PatientRegisterProps) => {
) {
errors[field] = "Please enter valid phone number";
}
if (await duplicateCheck(form["phone_number"])) {
errors[field] = "Patient with same phone number already exists";
}
break;
return;
case "emergency_phone_number":
emergency_phone_number = parsePhoneNumber(form[field]);
if (
Expand All @@ -616,7 +618,7 @@ export const PatientRegister = (props: PatientRegisterProps) => {
) {
errors[field] = "Please enter valid phone number";
}
break;
return;

case "estimated_contact_date":
if (
Expand All @@ -627,7 +629,7 @@ export const PatientRegister = (props: PatientRegisterProps) => {
errors[field] = "Please enter the estimated date of contact";
}
}
break;
return;
case "cluster_name":
if (
JSON.parse(form.contact_with_confirmed_carrier) ||
Expand All @@ -637,12 +639,12 @@ export const PatientRegister = (props: PatientRegisterProps) => {
errors[field] = "Please enter the name / cluster of the contact";
}
}
break;
return;
case "blood_group":
if (!form[field]) {
errors[field] = "Please select a blood group";
}
break;
return;

case "is_vaccinated":
if (form.is_vaccinated === "true") {
Expand All @@ -659,14 +661,14 @@ export const PatientRegister = (props: PatientRegisterProps) => {
"Please enter last vaccinated date";
}
}
break;
return;

case "date_of_result":
if (form[field] < form.date_of_test) {
errors[field] =
"Date should not be before the date of sample collection";
}
break;
return;
case "disease_status":
if (form[field] === "POSITIVE") {
if (!form.date_of_test) {
Expand All @@ -676,16 +678,16 @@ export const PatientRegister = (props: PatientRegisterProps) => {
errors["date_of_result"] = "Please fill the date of result";
}
}
break;
return;
case "medical_history":
if (!form[field].length) {
errors[field] = "Please fill the medical history";
}
break;
return;
default:
break;
return;
}
}
});

const firstError = Object.keys(errors).find((e) => errors[e]);
if (firstError) {
Expand Down Expand Up @@ -962,32 +964,33 @@ export const PatientRegister = (props: PatientRegisterProps) => {
});
};

const duplicateCheck = async (phoneNo: string) => {
if (
phoneNo &&
PhoneNumberValidator()(parsePhoneNumber(phoneNo) ?? "") === undefined
) {
const query = {
phone_number: parsePhoneNumber(phoneNo),
};
const res = await dispatchAction(searchPatient(query));
if (res?.data?.results) {
const duplicateList = !id
? res.data.results
: res.data.results.filter(
(item: DupPatientModel) => item.patient_id !== id
);
if (duplicateList.length) {
setStatusDialog({
show: true,
patientList: duplicateList,
});
return true;
const duplicateCheck = useCallback(
debounce(async (phoneNo: string) => {
if (
phoneNo &&
PhoneNumberValidator()(parsePhoneNumber(phoneNo) ?? "") === undefined
) {
const query = {
phone_number: parsePhoneNumber(phoneNo),
};
const res = await dispatchAction(searchPatient(query));
if (res?.data?.results) {
const duplicateList = !id
? res.data.results
: res.data.results.filter(
(item: DupPatientModel) => item.patient_id !== id
);
if (duplicateList.length) {
setStatusDialog({
show: true,
patientList: duplicateList,
});
}
}
}
}
return false;
};
}, 300),
[]
);

const handleDialogClose = (action: string) => {
if (action === "transfer") {
Expand Down Expand Up @@ -1257,6 +1260,7 @@ export const PatientRegister = (props: PatientRegisterProps) => {
required
label="Phone Number"
onChange={(event) => {
duplicateCheck(event.value);
field("phone_number").onChange(event);
}}
types={["mobile", "landline"]}
Expand Down

0 comments on commit 3ca1237

Please sign in to comment.