|
1 | | -/** |
2 | | - * Component to render top bar of app |
3 | | - */ |
4 | | -import React from 'react' |
| 1 | +/* global tcUniNav */ |
| 2 | +import React, { useRef, useState, useEffect } from 'react' |
5 | 3 | import PropTypes from 'prop-types' |
6 | | -import cn from 'classnames' |
7 | | -import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' |
8 | | -import { faSignInAlt } from '@fortawesome/free-solid-svg-icons' |
9 | | -import { get } from 'lodash' |
10 | | -import styles from './Topbar.module.scss' |
11 | | -import Handle from '../Handle' |
12 | | -import TopcoderLogo from '../../assets/images/topcoder-logo.png' |
13 | | -import { COMMUNITY_APP_URL } from '../../config/constants' |
14 | | - |
15 | | -const TopBar = ({ user, hideBottomLine }) => { |
16 | | - return ( |
17 | | - <div |
18 | | - className={cn(styles.topbar, { [styles['hide-line']]: hideBottomLine })} |
19 | | - > |
20 | | - <img src={TopcoderLogo} className={styles.logo} /> |
21 | | - {user && ( |
22 | | - <div className={styles.details}> |
23 | | - Welcome,{' '} |
24 | | - <Handle |
25 | | - handle={user.handle} |
26 | | - rating={get(user, 'maxRating.rating', 0)} |
27 | | - /> |
28 | | - <a href={`${COMMUNITY_APP_URL}/logout`}> |
29 | | - <FontAwesomeIcon icon={faSignInAlt} className={styles.icon} /> |
30 | | - </a> |
31 | | - </div> |
32 | | - )} |
33 | | - </div> |
34 | | - ) |
| 4 | +import { getInitials } from '../../util/url' |
| 5 | +import { COMMUNITY_APP_URL, HEADER_AUTH_URLS_HREF, HEADER_AUTH_URLS_LOCATION } from '../../config/constants' |
| 6 | +import _ from 'lodash' |
| 7 | + |
| 8 | +let uniqueId = 0 |
| 9 | + |
| 10 | +const HEADER_AUTH_URLS = { |
| 11 | + href: HEADER_AUTH_URLS_HREF, |
| 12 | + location: HEADER_AUTH_URLS_LOCATION |
| 13 | +} |
| 14 | +const BASE = COMMUNITY_APP_URL |
| 15 | + |
| 16 | +const TopBar = ({ auth }) => { |
| 17 | + const uniNavInitialized = useRef(false) |
| 18 | + const toolNameRef = useRef('Work Manager') |
| 19 | + const user = _.get(auth, 'user') || {} |
| 20 | + const authToken = _.get(auth, 'token') |
| 21 | + const isAuthenticated = !!authToken |
| 22 | + const authURLs = HEADER_AUTH_URLS |
| 23 | + const headerRef = useRef() |
| 24 | + const [headerId, setHeaderId] = useState(0) |
| 25 | + |
| 26 | + const navigationUserInfo = { |
| 27 | + ...user, |
| 28 | + initials: getInitials(user.firstName, user.lastName) |
| 29 | + } |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + uniqueId += 1 |
| 33 | + setHeaderId(uniqueId) |
| 34 | + }, []) |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + if (uniNavInitialized.current || !headerId) { |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + uniNavInitialized.current = true |
| 42 | + |
| 43 | + const regSource = window.location.pathname.split('/')[1] |
| 44 | + const retUrl = encodeURIComponent(window.location.href) |
| 45 | + tcUniNav('init', `headerNav-${headerId}`, { |
| 46 | + type: 'tool', |
| 47 | + toolName: toolNameRef.current, |
| 48 | + toolRoot: '/', |
| 49 | + user: isAuthenticated ? navigationUserInfo : null, |
| 50 | + signOut: () => { |
| 51 | + window.location = `${BASE}/logout?ref=nav` |
| 52 | + }, |
| 53 | + signIn: () => { |
| 54 | + window.location = `${authURLs.location |
| 55 | + .replace('%S', retUrl) |
| 56 | + .replace('member?', '#!/member?')}®Source=${regSource}` |
| 57 | + }, |
| 58 | + signUp: () => { |
| 59 | + window.location = `${authURLs.location |
| 60 | + .replace('%S', retUrl) |
| 61 | + .replace('member?', '#!/member?')}&mode=signUp®Source=${regSource}` |
| 62 | + } |
| 63 | + }) |
| 64 | + }, [headerId]) |
| 65 | + |
| 66 | + return <div id={`headerNav-${headerId}`} ref={headerRef} /> |
| 67 | +} |
| 68 | + |
| 69 | +TopBar.defaultProps = { |
| 70 | + auth: {} |
35 | 71 | } |
36 | 72 |
|
37 | 73 | TopBar.propTypes = { |
38 | | - user: PropTypes.object, |
39 | | - hideBottomLine: PropTypes.bool |
| 74 | + auth: PropTypes.shape() |
40 | 75 | } |
41 | 76 |
|
42 | 77 | export default TopBar |
0 commit comments