From 069c2aa9cda8778a2f5381e71a6e4dbb98fd6010 Mon Sep 17 00:00:00 2001 From: bhavanakarwade Date: Wed, 4 Dec 2024 18:30:15 +0530 Subject: [PATCH] handled undefined values Signed-off-by: bhavanakarwade --- .../Authentication/SignInUserPasskey.tsx | 3 +- .../Authentication/SignInUserPassword.tsx | 3 +- src/components/Profile/DisplayProfileImg.tsx | 31 +++++-------------- src/components/Profile/EditUserProfile.tsx | 3 +- src/components/Profile/UserProfile.tsx | 3 +- src/components/User/UserDashBoard.tsx | 3 +- src/components/organization/OrgDropDown.tsx | 9 ++---- .../organization/OrganizationsList.tsx | 3 +- 8 files changed, 16 insertions(+), 42 deletions(-) diff --git a/src/components/Authentication/SignInUserPasskey.tsx b/src/components/Authentication/SignInUserPasskey.tsx index 95412c854..f1aca8a2a 100644 --- a/src/components/Authentication/SignInUserPasskey.tsx +++ b/src/components/Authentication/SignInUserPasskey.tsx @@ -89,8 +89,7 @@ const SignInUserPasskey = (signInUserProps: signInUserProps) => { } await setToLocalStorage(storageKeys.PERMISSIONS, permissionArray); - // await setToLocalStorage(storageKeys.USER_PROFILE, userProfile); - await setToLocalStorage(storageKeys.USER_PROFILE, JSON.stringify(userProfile)); + await setToLocalStorage(storageKeys.USER_PROFILE, userProfile); await setToLocalStorage(storageKeys.USER_EMAIL, data?.data?.email); return { role: role?.orgRole || "" diff --git a/src/components/Authentication/SignInUserPassword.tsx b/src/components/Authentication/SignInUserPassword.tsx index bf6cc35d8..dec3d7768 100644 --- a/src/components/Authentication/SignInUserPassword.tsx +++ b/src/components/Authentication/SignInUserPassword.tsx @@ -63,8 +63,7 @@ const SignInUserPassword = (signInUserProps: SignInUser3Props) => { id, profileImg, firstName, email, } - // await setToLocalStorage(storageKeys.USER_PROFILE, userProfile); - await setToLocalStorage(storageKeys.USER_PROFILE, JSON.stringify(userProfile)); + await setToLocalStorage(storageKeys.USER_PROFILE, userProfile); await setToLocalStorage(storageKeys.USER_EMAIL, data?.data?.email); return { role: role?.orgRole ?? '', diff --git a/src/components/Profile/DisplayProfileImg.tsx b/src/components/Profile/DisplayProfileImg.tsx index 58b3477c1..36e80fef8 100644 --- a/src/components/Profile/DisplayProfileImg.tsx +++ b/src/components/Profile/DisplayProfileImg.tsx @@ -7,33 +7,16 @@ import { storageKeys } from "../../config/CommonConstant.ts"; const DisplayProfileImg = () => { const [userObj, setUserObj] = useState(null) - // const getUserDetails = async () => { - // const userProfile = await getFromLocalStorage(storageKeys.USER_PROFILE) - // const orgRoles = await getFromLocalStorage(storageKeys.ORG_ROLES) - // const parsedUser = userProfile ? JSON.parse(userProfile) : null; - - // if (parsedUser) { - // parsedUser.roles = orgRoles; - // setUserObj(parsedUser); - // } - // } - const getUserDetails = async () => { - try { - const userProfile = await getFromLocalStorage(storageKeys.USER_PROFILE); - const orgRoles = await getFromLocalStorage(storageKeys.ORG_ROLES); - const parsedUser = userProfile ? JSON.parse(userProfile) : null; + const userProfile = await getFromLocalStorage(storageKeys.USER_PROFILE) + const orgRoles = await getFromLocalStorage(storageKeys.ORG_ROLES) + const parsedUser = userProfile ? JSON.parse(userProfile) : null; - if (parsedUser) { - parsedUser.roles = orgRoles || []; // Default to an empty array if orgRoles is null - setUserObj(parsedUser); - } else { - console.warn("User profile is not available in local storage."); - } - } catch (error) { - console.error("Error fetching user details:", error); + if (parsedUser) { + parsedUser.roles = orgRoles; + setUserObj(parsedUser); } - }; + } useEffect(() => { getUserDetails() diff --git a/src/components/Profile/EditUserProfile.tsx b/src/components/Profile/EditUserProfile.tsx index 540ec89f6..8287afd8f 100644 --- a/src/components/Profile/EditUserProfile.tsx +++ b/src/components/Profile/EditUserProfile.tsx @@ -179,8 +179,7 @@ const EditUserProfile = ({ toggleEditProfile, userProfileInfo, updateProfile }: } updateProfile(userData); - // await setToLocalStorage(storageKeys.USER_PROFILE, updatedUserData); - await setToLocalStorage(storageKeys.USER_PROFILE, JSON.stringify(updatedUserData)); + await setToLocalStorage(storageKeys.USER_PROFILE, updatedUserData); window.location.reload(); setLoading(false) } diff --git a/src/components/Profile/UserProfile.tsx b/src/components/Profile/UserProfile.tsx index 72159e3bd..b894c29d8 100644 --- a/src/components/Profile/UserProfile.tsx +++ b/src/components/Profile/UserProfile.tsx @@ -27,8 +27,7 @@ const UserProfile = ({ noBreadcrumb }: { noBreadcrumb?: boolean }) => { const userProfile = { id, profileImg, firstName, email, } - // await setToLocalStorage(storageKeys.USER_PROFILE, userProfile) - await setToLocalStorage(storageKeys.USER_PROFILE, JSON.stringify(userProfile)) + await setToLocalStorage(storageKeys.USER_PROFILE, userProfile) await setToLocalStorage(storageKeys.USER_EMAIL, data?.data?.email) } } catch (error) { diff --git a/src/components/User/UserDashBoard.tsx b/src/components/User/UserDashBoard.tsx index 40c66a2b7..045dfc18b 100644 --- a/src/components/User/UserDashBoard.tsx +++ b/src/components/User/UserDashBoard.tsx @@ -354,8 +354,7 @@ const UserDashBoard = () => { await setToLocalStorage(storageKeys.ORG_ID, org.id.toString()); const roles: string[] = org?.userOrgRoles.map((role) => role.orgRole.name); - // await setToLocalStorage(storageKeys.ORG_ROLES, roles?.toString()); - await setToLocalStorage(storageKeys.ORG_ROLES, JSON.stringify(roles)); + await setToLocalStorage(storageKeys.ORG_ROLES, roles?.toString()); const { id, name, description, logoUrl } = org || {}; const orgInfo = { diff --git a/src/components/organization/OrgDropDown.tsx b/src/components/organization/OrgDropDown.tsx index b9c896b49..2b6faeb3b 100644 --- a/src/components/organization/OrgDropDown.tsx +++ b/src/components/organization/OrgDropDown.tsx @@ -67,9 +67,7 @@ const OrgDropDown = () => { ? org?.userOrgRoles.map((role) => role?.orgRole?.name) : []; if (roles.length > 0) { // Added check - // await setToLocalStorage(storageKeys.ORG_ROLES, roles?.toString()); - await setToLocalStorage(storageKeys.ORG_ROLES, JSON.stringify(roles)); - + await setToLocalStorage(storageKeys.ORG_ROLES, roles?.toString()); } }; @@ -93,9 +91,8 @@ const OrgDropDown = () => { setActiveOrg(activeOrgDetails); - await setToLocalStorage(storageKeys.ORG_ROLES, JSON.stringify(roles)); - - // await setToLocalStorage(storageKeys.ORG_ROLES, roles?.toString()); + + await setToLocalStorage(storageKeys.ORG_ROLES, roles?.toString()); } if (activeOrgDetails) { diff --git a/src/components/organization/OrganizationsList.tsx b/src/components/organization/OrganizationsList.tsx index 97418a775..1d53a8f79 100644 --- a/src/components/organization/OrganizationsList.tsx +++ b/src/components/organization/OrganizationsList.tsx @@ -123,8 +123,7 @@ const OrganizationsList = () => { id, name, description, logoUrl, roles } await setToLocalStorage(storageKeys.ORG_INFO, orgInfo) - // await setToLocalStorage(storageKeys.ORG_ROLES, roles?.toString()); - await setToLocalStorage(storageKeys.ORG_ROLES, JSON.stringify(roles)); + await setToLocalStorage(storageKeys.ORG_ROLES, roles?.toString()); window.location.href = pathRoutes.organizations.dashboard; }; let content: React.JSX.Element = <>;