Skip to content

Commit

Permalink
Display user initials on settings menu avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua-Douglas committed Mar 24, 2024
1 parent 91eb3cb commit 86addbe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
27 changes: 17 additions & 10 deletions app/src/components/common/AuthenticatedHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import Typography from '@mui/material/Typography';
import {Tooltip, Avatar, Menu, MenuItem, Stack} from '@mui/material';
import logo from '../../img/favicon.png';
import {useSignOutMutation} from '../../services/auth';
import {selectCurrentUser} from '../../../src/app/authSlice';
import {User} from '../../services/auth';

import {useSelector} from 'react-redux';
import {Link, useNavigate} from 'react-router-dom';
import {styled} from '@mui/system';

const userName = 'Hank Hill';

function getInitials(name: string) {
const splitName = name.split(' ');

if (splitName.length === 1) return splitName[0][0];

return splitName[0][0] + splitName[splitName.length - 1][0];
function getInitials(user: User): string {
const fi = user.firstName && user.firstName[0] || '?';
const li = user.lastName && user.lastName[0] || '?';

return fi + li;
}

interface OwnProps {
Expand Down Expand Up @@ -94,12 +94,19 @@ const AvatarDropdownMenu = () => {
setAnchorElUser(null);
};

// If the user ever fails to load, then display
// ?? as the initials for debugging purposes
const user = useSelector(selectCurrentUser) || {
email: 'unknown',
firstName: '?',
lastName: '?'
};

return (
<Box sx={{flexGrow: 0}}>
<Tooltip title="Open settings">
<IconButton onClick={handleOpenUserMenu} sx={{p: 0}}>
{/* Replace with real user name */}
<Avatar alt={userName}>{getInitials(userName)}</Avatar>
<Avatar alt={user.firstName + ' ' + user.lastName}>{getInitials(user)}</Avatar>
</IconButton>
</Tooltip>
<Menu
Expand Down
2 changes: 2 additions & 0 deletions app/src/services/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {api} from './api';

export interface User {
email: string;
firstName: string;
lastName: string;
}

export interface UserResponse {
Expand Down

0 comments on commit 86addbe

Please sign in to comment.