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

160 lines
3.5 KiB
JavaScript
Raw Normal View History

2015-07-24 21:54:19 -07:00
import React, { PropTypes } from 'react';
2015-10-27 15:40:04 -07:00
import { LinkContainer } from 'react-router-bootstrap';
2015-07-23 22:58:44 -07:00
import {
Col,
2015-07-24 21:54:19 -07:00
CollapsibleNav,
2015-07-23 22:58:44 -07:00
Nav,
2015-11-08 22:28:05 -08:00
NavBrand,
2015-07-23 22:58:44 -07:00
Navbar,
NavItem
} from 'react-bootstrap';
import navLinks from './links.json';
import FCCNavItem from './NavItem.jsx';
2015-06-04 10:53:01 -07:00
2015-11-19 22:45:31 -08:00
const win = typeof window !== 'undefined' ? window : {};
const fCClogo = 'https://s3.amazonaws.com/freecodecamp/freecodecamp_logo.svg';
const logoElement = (
<a href='/'>
<img
alt='learn to code javascript at Free Code Camp logo'
className='img-responsive nav-logo'
src={ fCClogo } />
</a>
);
2015-07-23 22:58:44 -07:00
const toggleButton = (
<button className='hamburger'>
<Col xs={ 12 }>
<span className='hamburger-text'>Menu</span>
</Col>
</button>
);
2015-11-19 22:45:31 -08:00
function getDashedName() {
let challengeDashedName;
if (typeof win.localStorage !== 'undefined') {
challengeDashedName = win.localStorage.getItem('currentDashedName');
}
return challengeDashedName && challengeDashedName !== 'undefined' ?
challengeDashedName :
'';
}
2015-10-27 15:40:04 -07:00
export default React.createClass({
displayName: 'Nav',
2015-06-04 10:53:01 -07:00
2015-10-27 15:40:04 -07:00
propTypes: {
2015-07-24 21:54:19 -07:00
points: PropTypes.number,
picture: PropTypes.string,
signedIn: PropTypes.bool,
username: PropTypes.string
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 + 1 }
key={ content }
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 }
target={ target || null }>
2015-10-27 15:40:04 -07:00
{ content }
</NavItem>
);
});
},
2015-06-04 10:53:01 -07:00
2015-11-19 22:45:31 -08:00
renderLearnBtn() {
return (
<NavItem
href='#'
onClick={ () => {
const challengeDashedName = getDashedName();
const goTo = challengeDashedName ?
'/challenges/' + challengeDashedName :
'/map';
win.location = goTo;
}}>
Learn
</NavItem>
);
},
2015-07-24 21:54:19 -07:00
renderPoints(username, points) {
if (!username) {
return null;
}
return (
<NavItem
href={ '/' + username }>
[ { points } ]
</NavItem>
);
2015-10-27 15:40:04 -07:00
},
2015-07-24 21:54:19 -07:00
renderSignin(username, picture) {
if (username) {
2015-06-04 10:53:01 -07:00
return (
2015-07-24 21:54:19 -07:00
<div
className='hidden-xs hidden-sm'
2015-06-04 10:53:01 -07:00
eventKey={ 2 }>
2015-07-24 21:54:19 -07:00
<a href={ '/' + username }>
2015-07-28 11:18:11 -07:00
<img
className='profile-picture float-right'
src={ picture } />
2015-07-24 21:54:19 -07:00
</a>
</div>
2015-06-04 10:53:01 -07:00
);
} else {
return (
<FCCNavItem
2015-06-29 09:50:25 -07:00
className='btn signup-btn signup-btn-nav'
2015-06-04 10:53:01 -07:00
eventKey={ 2 }
2015-06-17 21:04:28 -07:00
href='/login'>
Sign In
</FCCNavItem>
2015-06-04 10:53:01 -07:00
);
}
2015-10-27 15:40:04 -07:00
},
2015-06-04 10:53:01 -07:00
2015-06-17 21:04:28 -07:00
render() {
2015-07-24 21:54:19 -07:00
const { username, points, picture } = 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'
2015-06-04 10:53:01 -07:00
fixedTop={ true }
2015-07-23 22:58:44 -07:00
toggleButton={ toggleButton }
2015-06-17 21:04:28 -07:00
toggleNavKey={ 0 }>
2015-11-08 22:28:05 -08:00
<NavBrand>{ logoElement }</NavBrand>
2015-07-24 21:54:19 -07:00
<CollapsibleNav eventKey={ 0 }>
<Nav
className='hamburger-dropdown'
navbar={ true }
right={ true }>
2015-11-19 22:45:31 -08:00
{ this.renderLearnBtn() }
2015-10-27 15:40:04 -07:00
{ this.renderLinks() }
{ this.renderPoints(username, points) }
2015-07-24 21:54:19 -07:00
{ this.renderSignin(username, picture) }
</Nav>
</CollapsibleNav>
2015-06-04 10:53:01 -07:00
</Navbar>
);
}
2015-10-27 15:40:04 -07:00
});