diff --git a/src/public/js/app.js b/src/public/js/app.js index 366c3b8..cea0025 100644 --- a/src/public/js/app.js +++ b/src/public/js/app.js @@ -50,7 +50,11 @@ import TorrentList from './components/torrents/list' function Main(props) { return (
- +
{React.cloneElement(props.children, props)} diff --git a/src/public/js/components/navbar.js b/src/public/js/components/navbar.js index 1c6e8bc..8a7193e 100644 --- a/src/public/js/components/navbar.js +++ b/src/public/js/components/navbar.js @@ -2,41 +2,100 @@ import React from 'react' import { Nav, Navbar, NavItem, NavDropdown, MenuItem } from 'react-bootstrap' import { LinkContainer } from 'react-router-bootstrap' -export default function NavBar(props) { - return ( -
- - - - Canapé - - - - - - - - - - - - - -
- ); +export default class NavBar extends React.Component { + constructor(props) { + super(props); + this.state = { + userLoggedIn: (props.username !== ""), + displayMoviesSearch: this.shouldDisplayMoviesSearch(props.router), + displayShowsSearch: this.shouldDisplayShowsSearch(props.router), + }; + } + shouldComponentUpdate(nextProps, nextState) { + if (nextProps.username !== this.props.username) { + return true; + } + if (nextProps.torrentsCount !== this.props.torrentsCount) { + return true; + } + if (nextState.displayMoviesSearch !== this.state.displayMoviesSearch) { + return true; + } + if (nextState.displayShowsSearch !== this.state.displayShowsSearch) { + return true; + } + if (nextState.userLoggedIn !== this.state.userLoggedIn) { + return true; + } + + return false; + } + componentWillReceiveProps(nextProps) { + // Update the state based on the next props + const shouldDisplayMoviesSearch = this.shouldDisplayMoviesSearch(nextProps.router); + const shouldDisplayShowsSearch = this.shouldDisplayShowsSearch(nextProps.router); + if ((this.state.displayMoviesSearch !== shouldDisplayMoviesSearch) + || (this.state.displayShowsSearch !== shouldDisplayShowsSearch)) { + this.setState({ + userLoggedIn: (nextProps.username !== ""), + displayMoviesSearch: shouldDisplayMoviesSearch, + displayShowsSearch: shouldDisplayShowsSearch, + }); + } + } + shouldDisplayMoviesSearch(router) { + return this.matchPath(router, 'movies'); + } + shouldDisplayShowsSearch(router) { + return this.matchPath(router, 'shows'); + } + matchPath(router, keyword) { + const location = router.getCurrentLocation().pathname; + return (location.indexOf(keyword) !== -1) + } + render() { + return ( +
+ + + + Canapé + + + + + {this.state.userLoggedIn && + + } + {this.state.userLoggedIn && + + } + {this.state.userLoggedIn && + + } + {this.state.userLoggedIn && + + } + + {(this.state.displayMoviesSearch && this.state.userLoggedIn) && + + } + {(this.state.displayShowsSearch && this.state.userLoggedIn) && + + } + + +
+ ); + } } class Search extends React.Component { @@ -48,15 +107,7 @@ class Search extends React.Component { ev.preventDefault(); this.props.router.push(`${this.props.path}/${encodeURI(this.input.value)}`); } - isActive() { - const location = this.props.router.getCurrentLocation().pathname; - return (location.indexOf(this.props.pathMatch) !== -1) - } render() { - if (!this.isActive()) { - return null; - } - return(
this.handleSearch(ev)}> @@ -72,9 +123,6 @@ class Search extends React.Component { } function MoviesDropdown(props) { - if (props.username === "") { - return null; - } return(