From d4c43683304bd8802565259aab7d1de9847bb6ec Mon Sep 17 00:00:00 2001 From: Nithin Date: Thu, 22 Aug 2024 15:13:53 +0530 Subject: [PATCH 1/6] Improve validation check for phone number in patient list page #8189 --- src/Components/Patient/ManagePatients.tsx | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Components/Patient/ManagePatients.tsx b/src/Components/Patient/ManagePatients.tsx index 1177053dee4..731e80ab7bb 100644 --- a/src/Components/Patient/ManagePatients.tsx +++ b/src/Components/Patient/ManagePatients.tsx @@ -112,19 +112,22 @@ export const PatientManager = () => { const setPhoneNum = (phone_number: string) => { setPhoneNumber(phone_number); - if (phone_number.length >= 13) { - setPhoneNumberError(""); - updateQuery({ phone_number }); - return; - } - if (phone_number === "+91" || phone_number === "") { + if (phone_number.length >= 13) { + if (isValidPhoneNumber(phone_number)) { + setPhoneNumberError(""); + updateQuery({ phone_number }); + } else { + setPhoneNumberError("Enter a valid number"); + } + } else { setPhoneNumberError(""); - qParams.phone_number && updateQuery({ phone_number: null }); - return; } + }; - setPhoneNumberError("Enter a valid number"); + const isValidPhoneNumber = (phoneNumber: string) => { + const phoneNumberRegex = /^\+91[0-9]{10}$/; + return phoneNumberRegex.test(phoneNumber); }; const setEmergencyPhoneNum = (emergency_phone_number: string) => { From 36aa19ec57c08ad9714e746c88de798375919058 Mon Sep 17 00:00:00 2001 From: Nithin Date: Sat, 24 Aug 2024 17:45:05 +0530 Subject: [PATCH 2/6] Fix issueImprove validation check for phone number in patient list --- src/Components/Patient/ManagePatients.tsx | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/Components/Patient/ManagePatients.tsx b/src/Components/Patient/ManagePatients.tsx index 731e80ab7bb..dcf4872ae72 100644 --- a/src/Components/Patient/ManagePatients.tsx +++ b/src/Components/Patient/ManagePatients.tsx @@ -112,22 +112,17 @@ export const PatientManager = () => { const setPhoneNum = (phone_number: string) => { setPhoneNumber(phone_number); - if (phone_number.length >= 13) { - if (isValidPhoneNumber(phone_number)) { - setPhoneNumberError(""); - updateQuery({ phone_number }); - } else { - setPhoneNumberError("Enter a valid number"); - } - } else { setPhoneNumberError(""); + updateQuery({ phone_number }); + return; } - }; - const isValidPhoneNumber = (phoneNumber: string) => { - const phoneNumberRegex = /^\+91[0-9]{10}$/; - return phoneNumberRegex.test(phoneNumber); + if (phone_number === "+91" || phone_number === "") { + setPhoneNumberError(""); + qParams.phone_number && updateQuery({ phone_number: null }); + return; + } }; const setEmergencyPhoneNum = (emergency_phone_number: string) => { @@ -144,8 +139,6 @@ export const PatientManager = () => { updateQuery({ emergency_phone_number: null }); return; } - - setEmergencyPhoneNumberError("Enter a valid number"); }; const tabValue = From 367f65082f6f3bf9d0b080de8e02637d8764ef9b Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Mon, 30 Sep 2024 17:37:39 +0530 Subject: [PATCH 3/6] refactor and cleanup validation checks --- .../Form/FormFields/PhoneNumberFormField.tsx | 2 +- src/Components/Patient/ManagePatients.tsx | 77 ++++++------------- 2 files changed, 26 insertions(+), 53 deletions(-) diff --git a/src/Components/Form/FormFields/PhoneNumberFormField.tsx b/src/Components/Form/FormFields/PhoneNumberFormField.tsx index c46d2690ad3..e2e34a6ef65 100644 --- a/src/Components/Form/FormFields/PhoneNumberFormField.tsx +++ b/src/Components/Form/FormFields/PhoneNumberFormField.tsx @@ -100,7 +100,7 @@ export default function PhoneNumberFormField(props: Props) { ), diff --git a/src/Components/Patient/ManagePatients.tsx b/src/Components/Patient/ManagePatients.tsx index 5fe84b8fba2..ef70a01130f 100644 --- a/src/Components/Patient/ManagePatients.tsx +++ b/src/Components/Patient/ManagePatients.tsx @@ -53,6 +53,7 @@ import { import { ICD11DiagnosisModel } from "../Diagnosis/types.js"; import { getDiagnosesByIds } from "../Diagnosis/utils.js"; import Tabs from "../Common/components/Tabs.js"; +import { PhoneNumberValidator } from "../Form/FieldValidators.js"; const Loading = lazy(() => import("../Common/Loading")); @@ -104,40 +105,28 @@ export const PatientManager = () => { const [diagnoses, setDiagnoses] = useState([]); const [showDialog, setShowDialog] = useState<"create" | "list-discharged">(); const [showDoctors, setShowDoctors] = useState(false); - const [phone_number, setPhoneNumber] = useState(""); - const [phoneNumberError, setPhoneNumberError] = useState(""); - const [emergency_phone_number, setEmergencyPhoneNumber] = useState(""); - const [emergencyPhoneNumberError, setEmergencyPhoneNumberError] = - useState(""); + const [phoneNumber, _setPhoneNumber] = useState(""); + const [emergencyPhoneNumber, _setEmergencyPhoneNumber] = useState(""); - const setPhoneNum = (phone_number: string) => { - setPhoneNumber(phone_number); - if (phone_number.length >= 13) { - setPhoneNumberError(""); - updateQuery({ phone_number }); - return; + const setPhoneNum = (value: string) => { + _setPhoneNumber(value); + const error = PhoneNumberValidator()(value); + if (value.length >= 13 && !error) { + updateQuery({ phone_number: value }); } - - if (phone_number === "+91" || phone_number === "") { - setPhoneNumberError(""); - qParams.phone_number && updateQuery({ phone_number: null }); - return; + if ((value === "+91" || value === "") && qParams.phone_number) { + updateQuery({ phone_number: null }); } }; - const setEmergencyPhoneNum = (emergency_phone_number: string) => { - setEmergencyPhoneNumber(emergency_phone_number); - if (emergency_phone_number.length >= 13) { - setEmergencyPhoneNumberError(""); - updateQuery({ emergency_phone_number }); - return; + const setEmergencyPhoneNum = (value: string) => { + _setEmergencyPhoneNumber(value); + const error = PhoneNumberValidator()(value); + if (value.length >= 13 && !error) { + updateQuery({ emergency_phone_number: value }); } - - if (emergency_phone_number === "+91" || emergency_phone_number === "") { - setEmergencyPhoneNumberError(""); - qParams.emergency_phone_number && - updateQuery({ emergency_phone_number: null }); - return; + if ((value === "+91" || value === "") && qParams.emergency_phone_number) { + updateQuery({ emergency_phone_number: null }); } }; @@ -335,10 +324,10 @@ export const PatientManager = () => { query: params, onResponse: () => { if (!params.phone_number) { - setPhoneNumber("+91"); + _setPhoneNumber("+91"); } if (!params.emergency_phone_number) { - setEmergencyPhoneNumber("+91"); + _setEmergencyPhoneNumber("+91"); } }, }); @@ -989,43 +978,27 @@ export const PatientManager = () => { setPhoneNum(e.value)} - error={phoneNumberError} types={["mobile", "landline"]} className="w-full grow" + error={((phoneNumber || "+91") === "+91" && "") || undefined} /> setEmergencyPhoneNum(e.value)} - error={emergencyPhoneNumberError} types={["mobile", "landline"]} className="w-full" + error={ + ((emergencyPhoneNumber || "+91") === "+91" && "") || undefined + } /> - {/*!qParams.last_consultation__consent_types && - (patientsWithNoConsents || 0) > 0 && ( -
- -

- {patientsWithNoConsents} patients admitted missing consent - records  - -

-
- )*/}
Date: Mon, 30 Sep 2024 17:42:40 +0530 Subject: [PATCH 4/6] make code more readable --- src/Components/Patient/ManagePatients.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Components/Patient/ManagePatients.tsx b/src/Components/Patient/ManagePatients.tsx index ef70a01130f..205e5637b29 100644 --- a/src/Components/Patient/ManagePatients.tsx +++ b/src/Components/Patient/ManagePatients.tsx @@ -108,7 +108,7 @@ export const PatientManager = () => { const [phoneNumber, _setPhoneNumber] = useState(""); const [emergencyPhoneNumber, _setEmergencyPhoneNumber] = useState(""); - const setPhoneNum = (value: string) => { + const setPhoneNumber = (value: string) => { _setPhoneNumber(value); const error = PhoneNumberValidator()(value); if (value.length >= 13 && !error) { @@ -119,7 +119,7 @@ export const PatientManager = () => { } }; - const setEmergencyPhoneNum = (value: string) => { + const setEmergencyPhoneNumber = (value: string) => { _setEmergencyPhoneNumber(value); const error = PhoneNumberValidator()(value); if (value.length >= 13 && !error) { @@ -979,7 +979,7 @@ export const PatientManager = () => { label="Search by Primary Number" {...queryField("phone_number", "+91")} value={phoneNumber} - onChange={(e) => setPhoneNum(e.value)} + onChange={(e) => setPhoneNumber(e.value)} types={["mobile", "landline"]} className="w-full grow" error={((phoneNumber || "+91") === "+91" && "") || undefined} @@ -988,7 +988,7 @@ export const PatientManager = () => { label="Search by Emergency Number" {...queryField("emergency_phone_number", "+91")} value={emergencyPhoneNumber} - onChange={(e) => setEmergencyPhoneNum(e.value)} + onChange={(e) => setEmergencyPhoneNumber(e.value)} types={["mobile", "landline"]} className="w-full" error={ From d18046c0ebd1bd8d10596e0928fe4b3ad6dd471d Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Tue, 8 Oct 2024 13:15:42 +0530 Subject: [PATCH 5/6] remove unnecessary length check --- src/Components/Patient/ManagePatients.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Components/Patient/ManagePatients.tsx b/src/Components/Patient/ManagePatients.tsx index f2e483f5f0e..af12d80c5d8 100644 --- a/src/Components/Patient/ManagePatients.tsx +++ b/src/Components/Patient/ManagePatients.tsx @@ -111,7 +111,7 @@ export const PatientManager = () => { const setPhoneNumber = (value: string) => { _setPhoneNumber(value); const error = PhoneNumberValidator()(value); - if (value.length >= 13 && !error) { + if (!error) { updateQuery({ phone_number: value }); } if ((value === "+91" || value === "") && qParams.phone_number) { @@ -122,7 +122,7 @@ export const PatientManager = () => { const setEmergencyPhoneNumber = (value: string) => { _setEmergencyPhoneNumber(value); const error = PhoneNumberValidator()(value); - if (value.length >= 13 && !error) { + if (!error) { updateQuery({ emergency_phone_number: value }); } if ((value === "+91" || value === "") && qParams.emergency_phone_number) { From f3745cb2f6f4c1a4b6e49b53ed5f5020e64d4c54 Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Fri, 18 Oct 2024 16:54:33 +0530 Subject: [PATCH 6/6] update indian landline number regex as per TRAI documentation --- src/Components/Form/FieldValidators.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Components/Form/FieldValidators.tsx b/src/Components/Form/FieldValidators.tsx index 3159c6814f1..cf10ad09a53 100644 --- a/src/Components/Form/FieldValidators.tsx +++ b/src/Components/Form/FieldValidators.tsx @@ -79,12 +79,13 @@ export const EmailValidator = (message = "Invalid email address") => { // return RegexValidator(SUPPORT_PHONE_NUMBER_REGEX, message); // }; +// References: https://trai.gov.in/sites/default/files/Recommendations_29052020.pdf const INDIAN_MOBILE_NUMBER_REGEX = /^(?=^\+91)(^\+91[6-9]\d{9}$)/; const INTERNATIONAL_MOBILE_NUMBER_REGEX = /^(?!^\+91)(^\+\d{1,3}\d{8,14}$)/; const MOBILE_NUMBER_REGEX = new RegExp( `(${INDIAN_MOBILE_NUMBER_REGEX.source})|(${INTERNATIONAL_MOBILE_NUMBER_REGEX.source})`, ); -const INDIAN_LANDLINE_NUMBER_REGEX = /^\+91[2-9]\d{7,9}$/; +const INDIAN_LANDLINE_NUMBER_REGEX = /^\+91[2-9]\d{9}$/; const INDIAN_SUPPORT_NUMBER_REGEX = /^(1800|1860)\d{6,7}$/; const PHONE_NUMBER_REGEX_MAP = {