From add6af561b720f9c61926cd8469eb05ba4d7410f Mon Sep 17 00:00:00 2001 From: bhavanakarwade Date: Wed, 11 Dec 2024 16:36:40 +0530 Subject: [PATCH] fix: org roles error --- src/components/Profile/DisplayUser.tsx | 129 ++++++++++++++++++------- 1 file changed, 96 insertions(+), 33 deletions(-) diff --git a/src/components/Profile/DisplayUser.tsx b/src/components/Profile/DisplayUser.tsx index a1897f16..713b4925 100644 --- a/src/components/Profile/DisplayUser.tsx +++ b/src/components/Profile/DisplayUser.tsx @@ -1,43 +1,108 @@ -import { useEffect, useState } from "react"; +// import { useEffect, useState } from "react"; + +// import { TextTittlecase } from "../../utils/TextTransform.ts"; +// import { getFromLocalStorage } from "../../api/Auth.ts"; +// import { storageKeys } from "../../config/CommonConstant.ts"; + +// const DisplayUser = () => { + +// const [userObj, setUserObj] = useState(null) + +// let timer:any= null +// const getUserDetails = async () => { +// const userProfile = await getFromLocalStorage(storageKeys.USER_PROFILE) +// console.log("🚀 ~ getUserDetails ~ userProfile11111111111:", userProfile) +// const orgRoles = await getFromLocalStorage(storageKeys.ORG_ROLES) +// userProfile.roles = orgRoles; +// setUserObj(userProfile); +// } +// useEffect(() => { +// const fetchData = async () => { +// await getUserDetails(); +// }; +// if (userObj === null && timer === null) { +// timer = setTimeout(fetchData, 1000); +// } +// return () => { +// clearTimeout(timer); +// timer = null; +// }; +// }, [userObj]); + +// return ( +//
+// { +// userObj && +// <> +//

+// {userObj?.['firstName']} +//

+//

+// {userObj?.['email']} +//

+//

+// {TextTittlecase(userObj['roles'])} +//

+// +// } + + +//
+// ) +// } + +// export default DisplayUser +import { useEffect, useState } from "react"; import { TextTittlecase } from "../../utils/TextTransform.ts"; import { getFromLocalStorage } from "../../api/Auth.ts"; import { storageKeys } from "../../config/CommonConstant.ts"; const DisplayUser = () => { + const [userObj, setUserObj] = useState(null); + let timer: any = null; - const [userObj, setUserObj] = useState(null) - - let timer:any= null const getUserDetails = async () => { - const userProfile = await getFromLocalStorage(storageKeys.USER_PROFILE) - console.log("🚀 ~ getUserDetails ~ userProfile11111111111:", userProfile) - const orgRoles = await getFromLocalStorage(storageKeys.ORG_ROLES) - // const parsedUser = userProfile ? JSON.parse(userProfile) : null - // console.log("🚀 ~ getUserDetails ~ parsedUser:", parsedUser) - - // if (parsedUser) { - userProfile.roles = orgRoles; + // Retrieve user profile from localStorage and parse it safely + const userProfile = await getFromLocalStorage(storageKeys.USER_PROFILE); + console.log("🚀 ~ getUserDetails ~ userProfile:", userProfile); + + // Check if userProfile is a valid object + if (userProfile && typeof userProfile === 'object') { + const orgRoles = await getFromLocalStorage(storageKeys.ORG_ROLES); + userProfile.roles = orgRoles || []; // Fallback if orgRoles is null or undefined setUserObj(userProfile); - // } - } + } else { + console.error("Invalid user profile:", userProfile); + } + }; + useEffect(() => { - const fetchData = async () => { - await getUserDetails(); - }; - if (userObj === null && timer === null) { - timer = setTimeout(fetchData, 1000); - } - return () => { - clearTimeout(timer); - timer = null; - }; - }, [userObj]); + const fetchData = async () => { + await getUserDetails(); + }; + + // Only fetch user details if userObj is null and timer is not set + if (userObj === null && timer === null) { + timer = setTimeout(fetchData, 1000); + } + + // Cleanup timeout when component unmounts + return () => { + clearTimeout(timer); + timer = null; + }; + }, [userObj]); return (
- { - userObj && + {userObj && ( <>

{ {TextTittlecase(userObj['roles'])}

- } - - + )}
- ) -} + ); +}; -export default DisplayUser +export default DisplayUser;