From e67e034b922503a16811d91d05f7fbcb2a5d22e2 Mon Sep 17 00:00:00 2001 From: Ashutosh Rai Date: Thu, 29 Feb 2024 02:36:18 +0530 Subject: [PATCH] fixes username validation --- src/Components/Facility/ConsultationCard.tsx | 2 +- .../Facility/ConsultationDetails/index.tsx | 2 + src/Components/Users/UserAdd.tsx | 96 ++++++++++--------- 3 files changed, 52 insertions(+), 48 deletions(-) diff --git a/src/Components/Facility/ConsultationCard.tsx b/src/Components/Facility/ConsultationCard.tsx index 10aa4c295af..cd5331245da 100644 --- a/src/Components/Facility/ConsultationCard.tsx +++ b/src/Components/Facility/ConsultationCard.tsx @@ -164,7 +164,7 @@ export const ConsultationCard = (props: ConsultationProps) => { ) } disabled={ - (itemData.discharge_date as string | undefined) != undefined + (itemData.discharge_date as string | undefined) !== undefined } authorizeFor={NonReadOnlyUsers} > diff --git a/src/Components/Facility/ConsultationDetails/index.tsx b/src/Components/Facility/ConsultationDetails/index.tsx index b422fc8867d..ce3ca6c5f3d 100644 --- a/src/Components/Facility/ConsultationDetails/index.tsx +++ b/src/Components/Facility/ConsultationDetails/index.tsx @@ -127,6 +127,8 @@ export const ConsultationDetails = (props: any) => { ); } setConsultationData(data); + console.log(consultationData); + const assetRes = data?.current_bed?.bed_object?.id ? await dispatch( listAssetBeds({ diff --git a/src/Components/Users/UserAdd.tsx b/src/Components/Users/UserAdd.tsx index b4d9e9fbca6..595931f866e 100644 --- a/src/Components/Users/UserAdd.tsx +++ b/src/Components/Users/UserAdd.tsx @@ -141,9 +141,6 @@ const user_create_reducer = (state = initialState, action: any) => { } }; -const getDate = (value: any) => - value && dayjs(value).isValid() && dayjs(value).toDate(); - export const validateRule = ( condition: boolean, content: JSX.Element | string @@ -164,6 +161,9 @@ export const validateRule = ( ); }; +const getDate = (value: any) => + value && dayjs(value).isValid() && dayjs(value).toDate(); + export const UserAdd = (props: UserProps) => { const { goBack } = useAppHistory(); const { userId } = props; @@ -183,7 +183,7 @@ export const UserAdd = (props: UserProps) => { const [passwordInputInFocus, setPasswordInputInFocus] = useState(false); const [confirmPasswordInputInFocus, setConfirmPasswordInputInFocus] = useState(false); - const [usernameInput, setUsernameInput] = useState(""); + const [usernameInput, setUsernameInput] = useState(null); const userExistsEnums = { idle: 0, @@ -326,7 +326,7 @@ export const UserAdd = (props: UserProps) => { const setFacility = (selected: FacilityModel | FacilityModel[] | null) => { setSelectedFacility(selected as FacilityModel[]); - const form = { ...state.form }; + const form: any = { ...state.form }; form.facilities = selected ? (selected as FacilityModel[]).map((i) => i.id ?? -1) : []; @@ -619,7 +619,7 @@ export const UserAdd = (props: UserProps) => { }} formData={state.form} /> -
+
Facilities { {usernameInputInFocus && (
- {usernameExists !== userExistsEnums.idle && ( - <> - {usernameExists === userExistsEnums.checking ? ( - - {" "} - checking... - - ) : ( - <> - {usernameExists === userExistsEnums.exists ? ( -
- {" "} - - Username is not available - -
- ) : ( -
- {" "} - - Username is available - -
- )} - - )} - - )} + {usernameInput && + usernameExists !== userExistsEnums.idle && ( + <> + {usernameExists === userExistsEnums.checking ? ( + + {" "} + checking... + + ) : ( + <> + {usernameExists === userExistsEnums.exists ? ( +
+ {" "} + + Username is not available + +
+ ) : ( +
+ {" "} + + Username is available + +
+ )} + + )} + + )}
{validateRule( - usernameInput.length >= 4 && usernameInput.length <= 16, + usernameInput?.length >= 4 && usernameInput?.length <= 16, "Username should be 4-16 characters long" )}
{validateRule( /^[a-z0-9._-]*$/.test(usernameInput), - "Username can only contain lowercase letters, numbers, and . _ -" + "Username can only contain lowercase letters, numbers, and . _ - " )}
{validateRule( - /^[a-z0-9].*[a-z0-9]$/i.test(usernameInput), + usernameInput && + /^[a-zA-Z0-9].*[a-zA-Z0-9]$/.test(usernameInput), "Username must start and end with a letter or number" )}
@@ -930,7 +932,7 @@ export const UserAdd = (props: UserProps) => { /> ))}
-
+
goBack()} />