|
| 1 | +import * as React from "react"; |
| 2 | +import * as _ from "lodash"; |
| 3 | +import AuthService from "../AuthService"; |
| 4 | +import { |
| 5 | + Container, |
| 6 | + Dropdown, |
| 7 | + Icon, |
| 8 | + Image, |
| 9 | + Menu, |
| 10 | + Sidebar, |
| 11 | + Responsive |
| 12 | + } from "semantic-ui-react"; |
| 13 | + |
| 14 | +const NavBarChildren = ({ children }) => ( |
| 15 | + <Container style={{ marginTop: "5em" }}>{children}</Container> |
| 16 | +); |
| 17 | + |
| 18 | +export default class NavBar extends React.Component<any, any> { |
| 19 | + |
| 20 | + Auth: AuthService; |
| 21 | + |
| 22 | + leftItems = [ |
| 23 | + { as: "a", content: "Terminal", key: "terminal", href: "/term" } |
| 24 | + ]; |
| 25 | + |
| 26 | + constructor(props?: any, context?: any) { |
| 27 | + super(props, context); |
| 28 | + this.Auth = new AuthService(); |
| 29 | + this.handleLogout = this.handleLogout.bind(this); |
| 30 | + this.handlePusher = this.handlePusher.bind(this); |
| 31 | + this.handleToggle = this.handleToggle.bind(this); |
| 32 | + this.state = { |
| 33 | + visible: false |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + render() { |
| 38 | + const { children } = this.props; |
| 39 | + return ( |
| 40 | + <div> |
| 41 | + <Responsive {...Responsive.onlyMobile}> |
| 42 | + <Sidebar.Pushable> |
| 43 | + <Sidebar |
| 44 | + as={Menu} |
| 45 | + animation="overlay" |
| 46 | + icon="labeled" |
| 47 | + inverted={true} |
| 48 | + items={this.leftItems} |
| 49 | + vertical={true} |
| 50 | + visible={this.state.visible} |
| 51 | + /> |
| 52 | + <Sidebar.Pusher |
| 53 | + dimmed={this.state.visible} |
| 54 | + onClick={this.handlePusher} |
| 55 | + style={{ minHeight: "100vh" }} |
| 56 | + > |
| 57 | + <Menu fixed="top" inverted={true}> |
| 58 | + <Menu.Item> |
| 59 | + <Image size="mini" src="https://react.semantic-ui.com/logo.png" /> |
| 60 | + </Menu.Item> |
| 61 | + <Menu.Item onClick={this.handleToggle}> |
| 62 | + <Icon name="sidebar" /> |
| 63 | + </Menu.Item> |
| 64 | + <Menu.Menu position="right"> |
| 65 | + <Dropdown item={true} text={this.props.user.sub}> |
| 66 | + <Dropdown.Menu> |
| 67 | + <Dropdown.Item><a href="/profile">Profile</a></Dropdown.Item> |
| 68 | + <Dropdown.Item |
| 69 | + onClick={this.handleLogout} |
| 70 | + >Log out |
| 71 | + </Dropdown.Item> |
| 72 | + </Dropdown.Menu> |
| 73 | + </Dropdown> |
| 74 | + </Menu.Menu> |
| 75 | + </Menu> |
| 76 | + <NavBarChildren>{children}</NavBarChildren> |
| 77 | + </Sidebar.Pusher> |
| 78 | + </Sidebar.Pushable> |
| 79 | + </Responsive> |
| 80 | + <Responsive minWidth={Responsive.onlyTablet.minWidth}> |
| 81 | + <Menu fixed="top" inverted={true}> |
| 82 | + <Menu.Item> |
| 83 | + <Image size="mini" src="https://react.semantic-ui.com/logo.png" /> |
| 84 | + </Menu.Item> |
| 85 | + {_.map(this.leftItems, (item) => <Menu.Item {...item} />)} |
| 86 | + <Menu.Menu position="right"> |
| 87 | + <Dropdown item={true} text={this.props.user.sub}> |
| 88 | + <Dropdown.Menu> |
| 89 | + <Dropdown.Item><a href="/profile">Profile</a></Dropdown.Item> |
| 90 | + <Dropdown.Item |
| 91 | + onClick={this.handleLogout} |
| 92 | + >Log out |
| 93 | + </Dropdown.Item> |
| 94 | + </Dropdown.Menu> |
| 95 | + </Dropdown> |
| 96 | + </Menu.Menu> |
| 97 | + </Menu> |
| 98 | + <NavBarChildren>{children}</NavBarChildren> |
| 99 | + </Responsive> |
| 100 | + </div> |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + private handleLogout() { |
| 105 | + this.Auth.logout(); |
| 106 | + this.props.history.replace("/"); |
| 107 | + } |
| 108 | + |
| 109 | + private handlePusher() { |
| 110 | + const { visible } = this.state; |
| 111 | + |
| 112 | + if (visible) { |
| 113 | + this.setState( |
| 114 | + { visible: false } |
| 115 | + ); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private handleToggle() { |
| 120 | + this.setState({ visible: !this.state.visible }); |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments