import { AppBar, Avatar, Button, Link, ListItemIcon, ListItemText, Menu, MenuItem, Toolbar, Typography, } from '@material-ui/core'; import PersonIcon from '@material-ui/icons/Person'; import EditIcon from '@material-ui/icons/Edit'; import SettingsIcon from '@material-ui/icons/Settings'; import ExitToAppIcon from '@material-ui/icons/ExitToApp'; import AccountCircleIcon from '@material-ui/icons/AccountCircle'; import { makeStyles, createStyles } from '@material-ui/core/styles'; import { ExpandMore as ExpandMoreIcon } from '@material-ui/icons'; import { useState } from 'react'; import { useEffect } from 'react'; import { api_prefix, darkOrange, drawerWidth, END_POINT, FETCH_HEADER } from '../../../Constants/Constants'; import { logout } from '../../../Utils/formFunctionHelpers'; const useStyles = makeStyles(() => createStyles({ appBar: { width: `calc(100% - ${drawerWidth}px)`, height: 70, marginLeft: drawerWidth, backgroundColor: '#fff', boxShadow: '0px 0px 16px rgba(0, 0, 0, 0.05)', }, toolbarCtn: { display: 'flex', justifyContent: 'flex-end', padding: '0 32px', }, verticalDivider: { backgroundColor: '#EBEBF2', height: 28, width: 1, marginRight: 21, }, username: { marginBottom: 0, marginRight: 7, }, dropDownBtn: { minWidth: 38, height: 30, padding: 0, }, dropDownIcon: { color: '#A4AFB7', }, dropDownItemIcon: { minWidth: 30, }, avatarCtn: { width: 38, height: 38, marginLeft: 11, backgroundColor: darkOrange, }, anchorTag: { textDecoration: 'none', color: 'inherit', '&:hover ': { textDecoration: 'none', color: 'inherit', }, }, }) ); interface UserInfo { first_name: string; last_name: string; full_name: string; picture: string; } export default function AppTopBar() { const classes = useStyles(); const [userInfo, setUserInfo] = useState({ first_name: '', last_name: '', full_name: '', picture: '', }); const [anchorEl, setAnchorEl] = useState(null); const handleDropdownBtnClicked = (event: React.MouseEvent) => { setAnchorEl(event.currentTarget); }; const handleOptionClicked = (destination: string) => { window.location.assign(destination); }; const handleClose = () => { setAnchorEl(null); }; const getInitials = (firstname: string, lastname: string) => { const firstnameInitial = firstname.substring(0, 1).toUpperCase(); const lastnameInitial = lastname.substring(0, 1).toUpperCase(); return firstnameInitial + lastnameInitial; }; const renderDropdownMenu = () => ( {userInfo?.full_name && ( handleOptionClicked('https://www.eclipse.org/user')}> )} {userInfo?.full_name && ( handleOptionClicked('https://accounts.eclipse.org/user/edit')}> )} {userInfo?.full_name ? ( ) : ( )} ); useEffect(() => { const getUserFullInfo = (username: String) => { fetch(`https://api.eclipse.org/account/profile/${username}`) .then((res) => res.json()) .then((data) => { setUserInfo({ first_name: data.first_name, last_name: data.last_name, full_name: data.full_name, picture: data.picture, }); }) .catch((err) => { console.log(err); }); }; const getUserBasicInfo = () => { fetch(api_prefix() + `/${END_POINT.userinfo}`, { headers: FETCH_HEADER }) .then((res) => res.json()) .then((data) => { console.log('user info: ', data); getUserFullInfo(data.name); }) .catch((err) => { console.log(err); }); }; getUserBasicInfo(); }, []); return (
{userInfo?.full_name || 'Anonymous'} {renderDropdownMenu()} {userInfo?.picture ? ( ) : ( {getInitials(userInfo.first_name, userInfo.last_name) || 'A'} )}
); }