diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 61aa914c..65688afb 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -9,7 +9,7 @@ "version": "0.1.0", "dependencies": { "@aws-amplify/ui-react": "^5.0.0", - "@bcgov-nr/nr-fsa-theme": "^1.0.7", + "@bcgov-nr/nr-fsa-theme": "^1.0.10", "@carbon/react": "^1.27.0", "@testing-library/jest-dom": "^6.1.2", "@testing-library/react": "^14.0.0", @@ -7767,9 +7767,9 @@ } }, "node_modules/@bcgov-nr/nr-fsa-theme": { - "version": "1.1.5", - "resolved": "https://registry.npmjs.org/@bcgov-nr/nr-fsa-theme/-/nr-fsa-theme-1.1.5.tgz", - "integrity": "sha512-gQ7k76PXcTlWDOo6lrYNujgc5t7cY4r6gVS8m4bvkIbbgKQNux4kV5BuEtx616fubl61FZp+zDxRSMfrZz1Dog==" + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/@bcgov-nr/nr-fsa-theme/-/nr-fsa-theme-1.0.10.tgz", + "integrity": "sha512-3YGie8ITDyCUNWRMJhJNNEa5W61WotxjaeMLiBXCsNwrWh+9BeBvlLBN8etxqnCyCbx1QkUAlADGZf05gSYwnQ==" }, "node_modules/@bcoe/v8-coverage": { "version": "0.2.3", diff --git a/frontend/package.json b/frontend/package.json index 173cd333..6cc122b3 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -4,7 +4,7 @@ "private": true, "dependencies": { "@aws-amplify/ui-react": "^5.0.0", - "@bcgov-nr/nr-fsa-theme": "^1.0.7", + "@bcgov-nr/nr-fsa-theme": "^1.0.10", "@carbon/react": "^1.27.0", "@testing-library/jest-dom": "^6.1.2", "@testing-library/react": "^14.0.0", diff --git a/frontend/src/actions/userAction.ts b/frontend/src/actions/userAction.ts index 792c315e..5eef46f6 100644 --- a/frontend/src/actions/userAction.ts +++ b/frontend/src/actions/userAction.ts @@ -1,21 +1,26 @@ import { USER_DETAILS_REQUEST, USER_DETAILS_SUCCESS, USER_DETAILS_FAIL } from '../constants/userConstants' import { isCurrentAuthUser } from '../services/AuthService' +const FAM_LOGIN_USER = 'famLoginUser'; export const getUserDetails = () => async (dispatch: any) => { try { dispatch({ - type: USER_DETAILS_REQUEST - }) - // readt the localStorage Here - const data = await isCurrentAuthUser() + type: USER_DETAILS_REQUEST, + }); + //first call the isCurrent and only after that extract the JSON + const data = await isCurrentAuthUser(); + + const userJSON = localStorage.getItem(FAM_LOGIN_USER); // Retrieve the JSON string from local storage + const user = userJSON ? JSON.parse(userJSON) : null; // Parse the JSON string to a JavaScript object + dispatch({ type: USER_DETAILS_SUCCESS, - payload: { TestUser: 'Jazz as always', isLoggedIn: data } - }) + payload: { ...user, isLoggedIn: data}, + }); } catch (error) { dispatch({ type: USER_DETAILS_FAIL, - payload: { error } - }) + payload: { error: error }, + }); } -} +}; \ No newline at end of file diff --git a/frontend/src/components/AvatarImage/AvatarImage.scss b/frontend/src/components/AvatarImage/AvatarImage.scss index e48ff6d4..32db062c 100644 --- a/frontend/src/components/AvatarImage/AvatarImage.scss +++ b/frontend/src/components/AvatarImage/AvatarImage.scss @@ -1,5 +1,12 @@ .profile-image { border-radius: 50%; + display: flex; + justify-content: center; + align-items: center; + font-weight: bold; + background-color: #c7d4e7; + color: #072251; + font-size: 1.4rem; } .small { @@ -10,4 +17,5 @@ .large { width: 4rem; height: 4rem; -} \ No newline at end of file +} + diff --git a/frontend/src/components/AvatarImage/index.tsx b/frontend/src/components/AvatarImage/index.tsx index a1e79c19..b7603fa8 100644 --- a/frontend/src/components/AvatarImage/index.tsx +++ b/frontend/src/components/AvatarImage/index.tsx @@ -1,18 +1,32 @@ import React from 'react'; -import emptyUser from '../../assets/img/emptyUser.jpg'; import './AvatarImage.scss'; type Size = 'small' | 'large'; interface AvatarImageProps { userName: string; - source?: string; size: Size; } -const AvatarImage = ({ userName, source, size }: AvatarImageProps) => ( - {`${userName}`} -); +const getInitials = (userName: string) => { + const nameParts = userName.split(' '); + if (nameParts.length >= 2) { + return nameParts[0][0] + nameParts[1][0]; + } else if (nameParts.length === 1) { + return nameParts[0][0]; + } + return ''; +}; + +const AvatarImage = ({ userName, size }: AvatarImageProps) => { + const initials = getInitials(userName); + + return ( +
+
{initials}
+
+ ); +}; export default AvatarImage; diff --git a/frontend/src/components/MyProfile/index.tsx b/frontend/src/components/MyProfile/index.tsx index ab46f1ba..ba711d06 100644 --- a/frontend/src/components/MyProfile/index.tsx +++ b/frontend/src/components/MyProfile/index.tsx @@ -1,6 +1,6 @@ import React, { useCallback, useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; - +import { useSelector } from 'react-redux'; import { SideNavLink } from '@carbon/react'; @@ -16,8 +16,9 @@ import { logout } from '../../services/AuthService'; const MyProfile = () => { const { theme, setTheme } = useThemePreference(); - const userData = {firstName:'Jazz', lastName:"Grewal", idirUsername:"Jasgrewal", email:"jazz@test.com"}; - + const userData = {firstName:'Catherine', lastName:"Meng", idirUsername:"Jasgrewal", email:"jazz@test.com"}; + const userDetails = useSelector((state:any) => state.userDetails) + const [goToURL, setGoToURL] = useState(''); const [goTo, setGoTo] = useState(false); @@ -45,12 +46,13 @@ const MyProfile = () => { <>
- +
-

{`${userData.firstName} ${userData.lastName}`}

-

{`IDIR: ${userData.idirUsername}`}

-

{userData.email}

+

{`${userDetails.user.firstName} ${userDetails.user.lastName}`}

+

{`IDIR: ${userDetails.user.userName}`}

+

{`Email:${userDetails.user.email}`}

+

diff --git a/frontend/src/index.tsx b/frontend/src/index.tsx index 9c276de4..aad7aa40 100644 --- a/frontend/src/index.tsx +++ b/frontend/src/index.tsx @@ -12,7 +12,7 @@ const root = createRoot(container); root.render( - + diff --git a/frontend/src/services/AuthService.ts b/frontend/src/services/AuthService.ts index 95953418..2041e870 100644 --- a/frontend/src/services/AuthService.ts +++ b/frontend/src/services/AuthService.ts @@ -63,13 +63,11 @@ export const handlePostLogin = async () => { async function refreshToken (): Promise { try { console.log('Refreshing Token...') - const currentAuthToken: CognitoUserSession = - await Auth.currentSession() - console.log('currentAuthToken: ', currentAuthToken) + const currentAuthToken: CognitoUserSession = await Auth.currentSession() + const famLoginUser = parseToken(currentAuthToken); + await storeFamUser(famLoginUser); + return famLoginUser; - const famLoginUser = parseToken(currentAuthToken) - storeFamUser(famLoginUser) - return famLoginUser } catch (error) { console.error( 'Problem refreshing token or token is invalidated:', @@ -86,20 +84,33 @@ async function refreshToken (): Promise { * Note, current user data return for 'userData.username' is matched to "cognito:username" on Cognito. * Which isn't what we really want to display. The display username is "custom:idp_username" from token. */ -function parseToken (authToken: CognitoUserSession): FamLoginUser { - const decodedIdToken = authToken.getIdToken().decodePayload() - const decodedAccessToken = authToken.getAccessToken().decodePayload() + +function parseToken(authToken: CognitoUserSession): FamLoginUser { + const decodedIdToken = authToken.getIdToken().decodePayload(); + const decodedAccessToken = authToken.getAccessToken().decodePayload(); + + // Extract the first name and last name from the displayName and remove unwanted part + const displayName = decodedIdToken['custom:idp_display_name']; + const [lastName, firstName] = displayName.split(', '); + const sanitizedFirstName = firstName.split(' ')[0].trim(); // Remove unwanted part const famLoginUser = { - username: decodedIdToken['custom:idp_username'], - idpProvider: decodedIdToken.identities.providerName, + userName: decodedIdToken['custom:idp_username'], + displayName, + email: decodedIdToken['email'], + idpProvider: decodedIdToken['identities']['providerName'], roles: decodedAccessToken['cognito:groups'], - authToken - } - return famLoginUser + authToken: authToken, + firstName: sanitizedFirstName, + lastName, // Add lastName field + }; + + return famLoginUser; } -function removeFamUser () { - storeFamUser(undefined) + +function removeFamUser() { + storeFamUser(undefined); + // clean up local storage for selected application }