Files
freeCodeCamp/common/app/components/Nav/Nav.jsx

249 lines
5.6 KiB
JavaScript
Raw Normal View History

2015-07-24 21:54:19 -07:00
import React, { PropTypes } from 'react';
2016-03-05 21:06:04 -08:00
import ReactDOM from 'react-dom';
2015-10-27 15:40:04 -07:00
import { LinkContainer } from 'react-router-bootstrap';
2015-07-23 22:58:44 -07:00
import {
Col,
Nav,
NavbarBrand,
2015-07-23 22:58:44 -07:00
Navbar,
NavItem
} from 'react-bootstrap';
import navLinks from './links.json';
import PointsNavItem from './Points-Nav-Item.jsx';
import AvatarNavItem from './Avatar-Nav-Item.jsx';
2015-06-04 10:53:01 -07:00
const fCClogo = 'https://s3.amazonaws.com/freecodecamp/freecodecamp_logo.svg';
const toggleButtonChild = (
2016-01-27 11:34:44 -08:00
<Col xs={ 12 }>
<span className='hamburger-text'>Menu</span>
</Col>
2015-07-23 22:58:44 -07:00
);
function handleNavLinkEvent(content) {
this.props.trackEvent({
category: 'Nav',
action: 'clicked',
label: `${content} link`
});
}
2016-01-27 11:34:44 -08:00
export default class extends React.Component {
constructor(...props) {
super(...props);
this.handleMapClickOnMap = this.handleMapClickOnMap.bind(this);
this.handleLogoClick = this.handleLogoClick.bind(this);
navLinks.forEach(({ content }) => {
this[`handle${content}Click`] = handleNavLinkEvent.bind(this, content);
});
}
2016-01-27 11:34:44 -08:00
static displayName = 'Nav';
static propTypes = {
2015-07-24 21:54:19 -07:00
points: PropTypes.number,
picture: PropTypes.string,
signedIn: PropTypes.bool,
2016-03-05 21:06:04 -08:00
username: PropTypes.string,
isOnMap: PropTypes.bool,
updateNavHeight: PropTypes.func,
2016-06-03 23:34:28 -07:00
toggleMapDrawer: PropTypes.func,
toggleMainChat: PropTypes.func,
shouldShowSignIn: PropTypes.bool,
trackEvent: PropTypes.func.isRequired,
loadCurrentChallenge: PropTypes.func.isRequired
2016-01-27 11:34:44 -08:00
};
2015-10-27 15:40:04 -07:00
2016-03-05 21:06:04 -08:00
componentDidMount() {
const navBar = ReactDOM.findDOMNode(this);
this.props.updateNavHeight(navBar.clientHeight);
}
handleMapClickOnMap(e) {
e.preventDefault();
this.props.trackEvent({
category: 'Nav',
action: 'clicked',
label: 'map clicked while on map'
});
}
handleNavClick() {
this.props.trackEvent({
category: 'Nav',
action: 'clicked',
label: 'map clicked while on map'
});
}
handleLogoClick(e) {
e.preventDefault();
this.props.loadCurrentChallenge();
}
renderMapLink(isOnMap, toggleMapDrawer) {
if (isOnMap) {
return (
<li role='presentation'>
<a
href='#'
onClick={ this.handleMapClickOnMap }
>
Map
</a>
</li>
);
}
return (
<LinkContainer
eventKey={ 1 }
to='/map'
>
<NavItem
onClick={ e => {
if (!(e.ctrlKey || e.metaKey)) {
e.preventDefault();
toggleMapDrawer();
}
}}
target='/map'
>
Map
</NavItem>
</LinkContainer>
);
}
2016-06-03 23:34:28 -07:00
renderChat(toggleMainChat) {
return (
<NavItem
eventKey={ 2 }
href='//gitter.im/freecodecamp/freecodecamp'
onClick={ e => {
if (!(e.ctrlKey || e.metaKey)) {
e.preventDefault();
toggleMainChat();
}
}}
2016-06-03 23:34:28 -07:00
target='_blank'
>
2016-06-03 23:34:28 -07:00
Chat
</NavItem>
);
}
2015-10-27 15:40:04 -07:00
renderLinks() {
return navLinks.map(({ content, link, react, target }, index) => {
2015-10-27 15:40:04 -07:00
if (react) {
return (
<LinkContainer
eventKey={ index + 2 }
2015-10-27 15:40:04 -07:00
key={ content }
onClick={ this[`handle${content}Click`] }
to={ link }
>
2015-11-13 12:54:46 -08:00
<NavItem
target={ target || null }
>
2015-10-27 15:40:04 -07:00
{ content }
</NavItem>
</LinkContainer>
);
}
return (
<NavItem
eventKey={ index + 1 }
href={ link }
2015-11-13 12:54:46 -08:00
key={ content }
onClick={ this[`handle${content}Click`] }
target={ target || null }
>
2015-10-27 15:40:04 -07:00
{ content }
</NavItem>
);
});
2016-01-27 11:34:44 -08:00
}
2015-06-04 10:53:01 -07:00
renderPoints(username, points, shouldShowSignIn) {
if (!username || !shouldShowSignIn) {
2015-07-24 21:54:19 -07:00
return null;
}
return (
<LinkContainer
eventKey={ navLinks.length + 1 }
key='points'
to='/settings'
>
<PointsNavItem
className='brownie-points-nav'
points={ points }
/>
</LinkContainer>
2015-07-24 21:54:19 -07:00
);
2016-01-27 11:34:44 -08:00
}
2015-07-24 21:54:19 -07:00
renderSignIn(username, picture, shouldShowSignIn) {
if (!shouldShowSignIn) {
return null;
}
2015-07-24 21:54:19 -07:00
if (username) {
return <AvatarNavItem picture={ picture } />;
2015-06-04 10:53:01 -07:00
} else {
return (
<NavItem
2015-06-04 10:53:01 -07:00
eventKey={ 2 }
2016-09-08 21:56:46 -07:00
href='/signup'
key='signup'
>
2016-09-08 21:56:46 -07:00
Sign Up
</NavItem>
2015-06-04 10:53:01 -07:00
);
}
2016-01-27 11:34:44 -08:00
}
2015-06-04 10:53:01 -07:00
2015-06-17 21:04:28 -07:00
render() {
const {
username,
points,
picture,
isOnMap,
2016-06-03 23:34:28 -07:00
toggleMapDrawer,
toggleMainChat,
shouldShowSignIn
} = this.props;
2015-11-19 22:45:31 -08:00
2015-06-04 10:53:01 -07:00
return (
<Navbar
2015-06-17 21:04:28 -07:00
className='nav-height'
fixedTop={ true }
>
<NavbarBrand>
<a
href='/challenges/current-challenge'
onClick={ this.handleLogoClick }
>
<img
alt='learn to code javascript at Free Code Camp logo'
className='img-responsive nav-logo'
src={ fCClogo }
/>
</a>
</NavbarBrand>
<Navbar.Toggle children={ toggleButtonChild } />
<Navbar.Collapse>
2015-07-24 21:54:19 -07:00
<Nav
className='hamburger-dropdown'
navbar={ true }
pullRight={ true }
>
{ this.renderMapLink(isOnMap, toggleMapDrawer) }
2016-06-03 23:34:28 -07:00
{ this.renderChat(toggleMainChat) }
2015-10-27 15:40:04 -07:00
{ this.renderLinks() }
{ this.renderPoints(username, points, shouldShowSignIn) }
{ this.renderSignIn(username, picture, shouldShowSignIn) }
2015-07-24 21:54:19 -07:00
</Nav>
</Navbar.Collapse>
2015-06-04 10:53:01 -07:00
</Navbar>
);
}
2016-01-27 11:34:44 -08:00
}