1
0
mirror of https://github.com/ascribe/onion.git synced 2025-01-05 11:25:09 +01:00
onion/js/components/header.js

209 lines
7.1 KiB
JavaScript
Raw Normal View History

'use strict';
2015-05-20 16:19:40 +02:00
import React from 'react';
import { Link } from 'react-router';
import Nav from 'react-bootstrap/lib/Nav';
import Navbar from 'react-bootstrap/lib/Navbar';
import MenuItem from 'react-bootstrap/lib/MenuItem';
import NavDropdown from 'react-bootstrap/lib/NavDropdown';
2015-10-01 14:00:56 +02:00
import NavItem from 'react-bootstrap/lib/NavItem';
import LinkContainer from 'react-router-bootstrap/lib/LinkContainer';
2016-02-05 10:38:59 +01:00
import PieceListStore from '../stores/piece_list_store';
import AclProxy from './acl_proxy';
import withContext from './context/with_context';
import HeaderNotifications from './header_notifications';
import HeaderNotificationDebug from './header_notification_debug';
import NavRoutesLinks from './nav_routes_links';
import { currentUserShape, whitelabelShape } from './prop_types';
2015-06-11 15:35:18 +02:00
import { constructHead } from '../utils/dom';
import { safeMerge } from '../utils/general';
import { getLangText } from '../utils/lang';
2015-05-20 16:19:40 +02:00
let Header = React.createClass({
2015-07-14 21:06:11 +02:00
propTypes: {
routes: React.PropTypes.arrayOf(React.PropTypes.object).isRequired,
// Injected through HOCs
currentUser: currentUserShape.isRequired, // eslint-disable-line react/sort-prop-types
isLoggedIn: React.PropTypes.bool.isRequired, // eslint-disable-line react/sort-prop-types
whitelabel: whitelabelShape.isRequired // eslint-disable-line react/sort-prop-types
2015-05-20 16:19:40 +02:00
},
2016-02-05 10:38:59 +01:00
getInitialState() {
return safeMerge(
PieceListStore.getState(),
{
expandedCollapse: false
}
);
2016-02-05 10:38:59 +01:00
},
2015-05-20 16:19:40 +02:00
componentDidMount() {
2016-02-05 10:38:59 +01:00
// Listen to the piece list store, but don't fetch immediately to avoid
// conflicts with routes that may need to wait to load the piece list
PieceListStore.listen(this.onChange);
2015-06-03 11:49:39 +02:00
},
componentWillUnmount() {
2016-02-05 10:38:59 +01:00
PieceListStore.unlisten(this.onChange);
2015-05-20 16:19:40 +02:00
},
2016-02-05 10:38:59 +01:00
onChange(state) {
this.setState(state);
},
collapseNav() {
if (this.state.expandedCollapse) {
this.onToggleCollapse(false);
}
},
onToggleCollapse(expandedCollapse) {
this.setState({ expandedCollapse });
},
renderLogo() {
const { whitelabel } = this.props;
2015-10-14 17:29:14 +02:00
if (whitelabel.head) {
constructHead(whitelabel.head);
2015-09-29 16:48:46 +02:00
}
if (whitelabel.subdomain && whitelabel.subdomain !== 'www' && whitelabel.logo) {
return (
<Link to="/collection">
<img alt="Whitelabel brand" className="img-brand" src={whitelabel.logo} />
</Link>
);
} else {
return (
<Link to="/collection">
<span className="icon-ascribe-logo" />
</Link>
);
2015-06-29 15:58:47 +02:00
}
},
renderPoweredBy() {
2015-09-29 16:48:46 +02:00
return (
<a className="pull-left ascribe-powered-by" href="https://www.ascribe.io/" target="_blank">
<span>{getLangText('powered by')} </span>
<span className="icon-ascribe-logo"></span>
</a>
2015-09-29 16:48:46 +02:00
);
2015-06-29 15:58:47 +02:00
},
2015-09-29 16:48:46 +02:00
2015-05-20 16:19:40 +02:00
render() {
const { currentUser, isLoggedIn, routes, whitelabel } = this.props;
2016-02-05 10:38:59 +01:00
const { unfilteredPieceListCount } = this.state;
2015-08-28 15:29:30 +02:00
let account;
let signup;
let navRoutesLinks;
if (isLoggedIn) {
2015-06-15 12:36:27 +02:00
account = (
<NavDropdown
2016-02-05 10:38:59 +01:00
id="nav-route-user-dropdown"
title={currentUser.username}>
<LinkContainer to="/settings">
<MenuItem>
2015-10-01 14:00:56 +02:00
{getLangText('Account Settings')}
</MenuItem>
</LinkContainer>
<AclProxy
aclName="acl_view_settings_contract"
aclObject={currentUser.acl}>
<LinkContainer to="/contract_settings">
<MenuItem>
2015-10-01 14:00:56 +02:00
{getLangText('Contract Settings')}
</MenuItem>
</LinkContainer>
</AclProxy>
2015-06-22 10:50:22 +02:00
<MenuItem divider />
<LinkContainer to="/logout">
<MenuItem>
2015-10-01 14:00:56 +02:00
{getLangText('Log out')}
</MenuItem>
</LinkContainer>
</NavDropdown>
2015-06-15 12:36:27 +02:00
);
// Let's assume that if the piece list hasn't loaded yet (ie. when
// unfilteredPieceListCount === -1) then the user has pieces.
2016-02-05 10:38:59 +01:00
// FIXME: this doesn't work that well as the user may not load their piece list
// until much later, so we would show the 'Collection' header as available until
// they actually click on it and get redirected to piece registration.
navRoutesLinks = (
<NavRoutesLinks
navbar
2016-05-04 16:44:39 +02:00
pullRight
2016-02-05 10:38:59 +01:00
hasPieces={!!unfilteredPieceListCount}
onSelect={this.collapseNav}
2016-02-05 10:38:59 +01:00
routes={routes}
userAcl={currentUser.acl} />
);
} else {
2015-10-01 14:00:56 +02:00
account = (
<LinkContainer to="/login">
2015-10-01 14:00:56 +02:00
<NavItem>
{getLangText('LOGIN')}
</NavItem>
</LinkContainer>
);
signup = (
<LinkContainer to="/signup">
2015-10-01 14:00:56 +02:00
<NavItem>
{getLangText('SIGNUP')}
</NavItem>
</LinkContainer>
);
2015-06-15 15:28:53 +02:00
}
2015-06-22 10:50:22 +02:00
2015-06-29 15:58:47 +02:00
return (
<div>
<Navbar
2015-12-23 09:36:08 +01:00
ref="navbar"
fixedTop
className="hidden-print"
expanded={this.state.expandedCollapse}
onToggle={this.onToggleCollapse}>
2016-05-04 16:44:39 +02:00
<Navbar.Header>
<Navbar.Brand>
{this.renderLogo()}
2016-05-04 16:44:39 +02:00
</Navbar.Brand>
<AclProxy
aclName="acl_view_powered_by"
aclObject={whitelabel}>
{this.renderPoweredBy()}
</AclProxy>
2016-06-08 17:09:06 +02:00
<Navbar.Toggle />
2016-05-04 16:44:39 +02:00
</Navbar.Header>
<Navbar.Collapse>
<Nav navbar pullRight onSelect={this.collapseNav}>
2015-06-29 15:58:47 +02:00
{account}
{signup}
</Nav>
<Nav navbar pullRight onSelect={this.collapseNav}>
<HeaderNotificationDebug show={false} />
<HeaderNotifications />
</Nav>
2015-08-28 15:29:30 +02:00
{navRoutesLinks}
2016-05-04 16:44:39 +02:00
</Navbar.Collapse>
2015-06-29 15:58:47 +02:00
</Navbar>
2015-12-23 09:36:08 +01:00
<p className="ascribe-print-header visible-print">
<span className="icon-ascribe-logo" />
</p>
2015-06-29 15:58:47 +02:00
</div>
2015-05-21 15:20:02 +02:00
);
2015-05-20 16:19:40 +02:00
}
});
export default withContext(Header, 'currentUser', 'isLoggedIn', 'whitelabel');