import React, {Component} from 'react'; import PropTypes from 'prop-types'; import { withRouter } from "react-router"; import './Header.scss'; import { Navbar, NavbarBrand, Nav, NavItem, NavLink, UncontrolledDropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'reactstrap'; import {Colors} from "./common/Colors"; class Header extends Component { showLogInButton(isLoggedIn) { if (!isLoggedIn) { return ( Log in ); } else { return null; } } shouldHaveDropdown(menu, onLogout) { return this.shouldShowMenu(menu) || onLogout; } shouldShowMenu(menu) { return Array.isArray(menu) && menu.length > 0; } showMenuItems(menu) { if (!this.shouldShowMenu(menu)) { return null; } return menu.map(item => { return this.props.history.push(item.href)} > {item.text} ; }); } showUserMenu(isLoggedIn, loginName, menu, onLogout) { if (isLoggedIn) { const dropdownAttr = this.shouldHaveDropdown(menu, onLogout) ? {nav: true, caret: true} : {nav: true}; return ( {loginName} { this.shouldHaveDropdown(menu, onLogout) && {this.showMenuItems(menu)} {onLogout && Log out } } ); } else { return null; } } createThemeObject(theme) { if (theme === 'dark') { return { dark: true, // color: 'dark',// will use dark-gray with !important style // Instead of customizing bootstrap (which is difficult to find right variable and avoid side effect) style: { backgroundColor: Colors.SQUIDINK } }; } else { return { color: 'light', light: true }; } } render() { const {customerName, customerLogoUrl, loginName, theme, logoHeight, menu, onLogout} = this.props; const hideLoginComponent = (loginName === undefined || loginName === null); const isLoggedIn = !!loginName; let customTheme = this.createThemeObject(theme); return ( { customerLogoUrl && Customer logo } {customerName} {/* Divide left and right alignment*/}
{ !hideLoginComponent && } ); } } Header.propTypes = { customerLogoUrl: PropTypes.string, customerName: PropTypes.string, loginName: PropTypes.string, menu: PropTypes.array, onLogout: PropTypes.func, theme: PropTypes.oneOf(['light', 'dark']), logoHeight: PropTypes.number, }; export default withRouter(Header)