AppTopBar.tsx 8.09 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
import {
  AppBar,
  Avatar,
  Button,
  Link,
  ListItemIcon,
  ListItemText,
  Menu,
  MenuItem,
  Toolbar,
  Typography,
12
13
  IconButton,
  Theme,
14
15
16
17
18
19
} 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';
20
import { makeStyles, createStyles } from '@material-ui/core/styles';
21
22
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
import MenuIcon from '@material-ui/icons/Menu';
23
24
import { useState } from 'react';
import { useEffect } from 'react';
25
26
import efGRYLogo from '../../../assets/logos/ef-gry.svg';
import { api_prefix, darkOrange, END_POINT, FETCH_HEADER, themeBlack } from '../../../Constants/Constants';
27
import { logout } from '../../../Utils/formFunctionHelpers';
28

29
const useStyles = makeStyles((theme: Theme) =>
30
31
  createStyles({
    appBar: {
32
33
      position: 'relative',
      height: 110,
34
35
      backgroundColor: '#fff',
      boxShadow: '0px 0px 16px rgba(0, 0, 0, 0.05)',
36
37
38
39
40
41
42
43
44
45
46
47
48
      [theme.breakpoints.up('md')]: {
        height: 70,
        position: 'fixed',
      },
      [theme.breakpoints.down('sm')]: {
        // Has to use !important to overwrite the padding-right 17px added by MUI when hamburger menu is open
        // This can avoid making user dropdown menu move left and right
        padding: '0 !important',
      },
      transition: theme.transitions.create('height', {
        easing: theme.transitions.easing.easeInOut,
        duration: theme.transitions.duration.leavingScreen,
      }),
49
50
    },
    toolbarCtn: {
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
      height: '100%',
    },
    iconCtn: {
      width: '100%',
      display: 'flex',
      alignItems: 'center',
      justifyContent: 'space-between',
      position: 'absolute',
      left: 0,
      right: 0,
      bottom: 0,
      [theme.breakpoints.up('md')]: {
        display: 'none',
      },
    },
    efLogo: {
      width: 120,
    },
    hamburgerIcon: {
      marginRight: theme.spacing(0.5),
      color: themeBlack,
    },
    userInfoCtn: {
74
      display: 'flex',
75
76
77
78
79
80
81
82
83
      position: 'absolute',
      top: theme.spacing(1),
      right: theme.spacing(1),
      height: 40,
      alignItems: 'center',
      [theme.breakpoints.up('md')]: {
        top: theme.spacing(1.5),
        right: theme.spacing(2),
      },
84
85
    },
    verticalDivider: {
86
      display: 'none',
87
      backgroundColor: '#EBEBF2',
88
      height: 30,
89
      width: 1,
90
91
92
93
      marginRight: theme.spacing(2),
      [theme.breakpoints.up('md')]: {
        display: 'block',
      },
94
95
    },
    username: {
96
      color: themeBlack,
97
      marginBottom: 0,
98
      marginRight: theme.spacing(1),
99
    },
100
    dropDownBtn: {
101
      minWidth: 40,
102
103
104
      height: 30,
      padding: 0,
    },
105
106
107
    dropDownIcon: {
      color: '#A4AFB7',
    },
108
109
110
    dropDownItemIcon: {
      minWidth: 30,
    },
111
    avatarCtn: {
112
113
114
115
116
117
118
      display: 'none',
      width: 40,
      height: 40,
      marginLeft: theme.spacing(1),
      [theme.breakpoints.up('md')]: {
        display: 'flex',
      },
119
      backgroundColor: darkOrange,
120
    },
121
122
123
124
125
126
127
    anchorTag: {
      textDecoration: 'none',
      color: 'inherit',
      '&:hover ': {
        textDecoration: 'none',
        color: 'inherit',
      },
128
129
130
131
132
    },
  })
);

interface UserInfo {
133
134
  first_name: string;
  last_name: string;
135
136
137
138
  full_name: string;
  picture: string;
}

139
140
141
142
143
interface AppTopBarProps {
  handleDrawerToggle: () => void;
}

const AppTopBar: React.FC<AppTopBarProps> = ({ handleDrawerToggle }) => {
144
  const classes = useStyles();
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
  const [userInfo, setUserInfo] = useState<UserInfo>({
    first_name: '',
    last_name: '',
    full_name: '',
    picture: '',
  });
  const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);

  const handleDropdownBtnClicked = (event: React.MouseEvent<HTMLButtonElement>) => {
    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 = () => (
    <Menu id="simple-menu" anchorEl={anchorEl} keepMounted open={Boolean(anchorEl)} onClose={handleClose}>
      {userInfo?.full_name && (
        <MenuItem onClick={() => handleOptionClicked('https://www.eclipse.org/user')}>
          <ListItemIcon className={classes.dropDownItemIcon}>
            <PersonIcon fontSize="small" />
          </ListItemIcon>
          <ListItemText primary="View Profile" />
        </MenuItem>
      )}

      {userInfo?.full_name && (
        <MenuItem onClick={() => handleOptionClicked('https://accounts.eclipse.org/user/edit')}>
          <ListItemIcon className={classes.dropDownItemIcon}>
            <EditIcon fontSize="small" />
          </ListItemIcon>
          <ListItemText primary="Edit Profile" />
        </MenuItem>
      )}

      <MenuItem className="toolbar-manage-cookies" onClick={handleClose}>
        <ListItemIcon className={classes.dropDownItemIcon}>
          <SettingsIcon fontSize="small" />
        </ListItemIcon>
        <ListItemText primary="Manage Cookies" />
      </MenuItem>

      {userInfo?.full_name ? (
        <MenuItem onClick={logout}>
          <ListItemIcon className={classes.dropDownItemIcon}>
            <ExitToAppIcon fontSize="small" />
          </ListItemIcon>
          <ListItemText primary="Log Out" />
        </MenuItem>
      ) : (
        <Link href="/api/login" className={classes.anchorTag}>
          <MenuItem>
            <ListItemIcon className={classes.dropDownItemIcon}>
              <AccountCircleIcon fontSize="small" />
            </ListItemIcon>
            <ListItemText primary="Log In" />
          </MenuItem>
        </Link>
      )}
    </Menu>
  );
218
219
220
221
222
223
224

  useEffect(() => {
    const getUserFullInfo = (username: String) => {
      fetch(`https://api.eclipse.org/account/profile/${username}`)
        .then((res) => res.json())
        .then((data) => {
          setUserInfo({
225
226
            first_name: data.first_name,
            last_name: data.last_name,
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
            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 (
    <AppBar position="fixed" className={classes.appBar}>
      <Toolbar className={classes.toolbarCtn}>
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
        <div className={classes.iconCtn}>
          <img src={efGRYLogo} alt="Eclipse Foundation logo" className={classes.efLogo}></img>
          <IconButton
            color="inherit"
            aria-label="open drawer"
            edge="start"
            className={classes.hamburgerIcon}
            onClick={handleDrawerToggle}
          >
            <MenuIcon />
          </IconButton>
        </div>

        <div className={classes.userInfoCtn}>
          <div className={classes.verticalDivider}></div>
          <Typography component="p" className={classes.username}>
            {userInfo?.full_name || 'Anonymous'}
          </Typography>

          <Button className={classes.dropDownBtn} onClick={handleDropdownBtnClicked}>
            <ExpandMoreIcon className={classes.dropDownIcon} />
          </Button>

          {renderDropdownMenu()}
          {userInfo?.picture ? (
            <Avatar className={classes.avatarCtn} alt="user avatar" src={userInfo.picture} />
          ) : (
            <Avatar className={classes.avatarCtn}>{getInitials(userInfo.first_name, userInfo.last_name) || 'A'}</Avatar>
          )}
        </div>
284
285
286
      </Toolbar>
    </AppBar>
  );
287
288
289
};

export default AppTopBar;