|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +// <NavBarSnippet> |
| 5 | +import React from 'react'; |
| 6 | +import { NavLink as RouterNavLink } from 'react-router-dom'; |
| 7 | +import { |
| 8 | + Button, |
| 9 | + Collapse, |
| 10 | + Container, |
| 11 | + Navbar, |
| 12 | + NavbarToggler, |
| 13 | + NavbarBrand, |
| 14 | + Nav, |
| 15 | + NavItem, |
| 16 | + NavLink, |
| 17 | + UncontrolledDropdown, |
| 18 | + DropdownToggle, |
| 19 | + DropdownMenu, |
| 20 | + DropdownItem } from 'reactstrap'; |
| 21 | +import '@fortawesome/fontawesome-free/css/all.css'; |
| 22 | + |
| 23 | +interface NavBarProps { |
| 24 | + isAuthenticated : boolean; |
| 25 | + authButtonMethod : any; |
| 26 | + user : any; |
| 27 | +} |
| 28 | + |
| 29 | +interface NavBarState { |
| 30 | + isOpen : boolean; |
| 31 | +} |
| 32 | + |
| 33 | +function UserAvatar(props: any) { |
| 34 | + // If a user avatar is available, return an img tag with the pic |
| 35 | + if (props.user.avatar) { |
| 36 | + return <img |
| 37 | + src={props.user.avatar} alt="user" |
| 38 | + className="rounded-circle align-self-center mr-2" |
| 39 | + style={{width: '32px'}}></img>; |
| 40 | + } |
| 41 | + |
| 42 | + // No avatar available, return a default icon |
| 43 | + return <i |
| 44 | + className="far fa-user-circle fa-lg rounded-circle align-self-center mr-2" |
| 45 | + style={{width: '32px'}}></i>; |
| 46 | +} |
| 47 | + |
| 48 | +function AuthNavItem(props: NavBarProps) { |
| 49 | + // If authenticated, return a dropdown with the user's info and a |
| 50 | + // sign out button |
| 51 | + if (props.isAuthenticated) { |
| 52 | + return ( |
| 53 | + <UncontrolledDropdown> |
| 54 | + <DropdownToggle nav caret> |
| 55 | + <UserAvatar user={props.user}/> |
| 56 | + </DropdownToggle> |
| 57 | + <DropdownMenu right> |
| 58 | + <h5 className="dropdown-item-text mb-0">{props.user.displayName}</h5> |
| 59 | + <p className="dropdown-item-text text-muted mb-0">{props.user.email}</p> |
| 60 | + <DropdownItem divider /> |
| 61 | + <DropdownItem onClick={props.authButtonMethod}>Sign Out</DropdownItem> |
| 62 | + </DropdownMenu> |
| 63 | + </UncontrolledDropdown> |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + // Not authenticated, return a sign in link |
| 68 | + return ( |
| 69 | + <NavItem> |
| 70 | + <Button |
| 71 | + onClick={props.authButtonMethod} |
| 72 | + className="btn-link nav-link border-0" |
| 73 | + color="link">Sign In</Button> |
| 74 | + </NavItem> |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +export default class NavBar extends React.Component<NavBarProps, NavBarState> { |
| 79 | + constructor(props: NavBarProps) { |
| 80 | + super(props); |
| 81 | + |
| 82 | + this.toggle = this.toggle.bind(this); |
| 83 | + this.state = { |
| 84 | + isOpen: false |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + toggle() { |
| 89 | + this.setState({ |
| 90 | + isOpen: !this.state.isOpen |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + render() { |
| 95 | + // Only show calendar nav item if logged in |
| 96 | + let calendarLink = null; |
| 97 | + if (this.props.isAuthenticated) { |
| 98 | + calendarLink = ( |
| 99 | + <NavItem> |
| 100 | + <RouterNavLink to="/calendar" className="nav-link" exact>Calendar</RouterNavLink> |
| 101 | + </NavItem> |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + return ( |
| 106 | + <div> |
| 107 | + <Navbar color="dark" dark expand="md" fixed="top"> |
| 108 | + <Container> |
| 109 | + <NavbarBrand href="/">React Graph Tutorial</NavbarBrand> |
| 110 | + <NavbarToggler onClick={this.toggle} /> |
| 111 | + <Collapse isOpen={this.state.isOpen} navbar> |
| 112 | + <Nav className="mr-auto" navbar> |
| 113 | + <NavItem> |
| 114 | + <RouterNavLink to="/" className="nav-link" exact>Home</RouterNavLink> |
| 115 | + </NavItem> |
| 116 | + {calendarLink} |
| 117 | + </Nav> |
| 118 | + <Nav className="justify-content-end" navbar> |
| 119 | + <NavItem> |
| 120 | + <NavLink href="https://developer.microsoft.com/graph/docs/concepts/overview" target="_blank"> |
| 121 | + <i className="fas fa-external-link-alt mr-1"></i> |
| 122 | + Docs |
| 123 | + </NavLink> |
| 124 | + </NavItem> |
| 125 | + <AuthNavItem |
| 126 | + isAuthenticated={this.props.isAuthenticated} |
| 127 | + authButtonMethod={this.props.authButtonMethod} |
| 128 | + user={this.props.user} /> |
| 129 | + </Nav> |
| 130 | + </Collapse> |
| 131 | + </Container> |
| 132 | + </Navbar> |
| 133 | + </div> |
| 134 | + ); |
| 135 | + } |
| 136 | +} |
| 137 | +// </NavBarSnippet> |
0 commit comments