From fa73dc09c25b54b3a33c599c4257a59955655ac3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Delattre?= Date: Wed, 19 Apr 2017 13:41:34 +0200 Subject: [PATCH] Simplify the search module --- src/public/js/components/movies/list.js | 37 +++--------------------- src/public/js/components/navbar.js | 38 ++++++++++++++++++++++--- src/public/js/components/shows/list.js | 35 ++--------------------- 3 files changed, 40 insertions(+), 70 deletions(-) diff --git a/src/public/js/components/movies/list.js b/src/public/js/components/movies/list.js index c3421aa..2b43b6b 100644 --- a/src/public/js/components/movies/list.js +++ b/src/public/js/components/movies/list.js @@ -41,41 +41,12 @@ function MovieButtons(props) { } export default class MovieList extends React.Component { - handleParams(props = this.props) { - // Check if the URL to fetch is in the props - if (props.moviesUrl) { - this.props.fetchMovies(props.moviesUrl); - return - } - - // Check for URL parameters - if (!props.params) { - return - } - - // Search param - if (props.params.search && props.params.search !== "") { - props.searchMovies({ - key: props.params.search - }); - return - } + constructor(props) { + super(props); } componentWillMount() { - this.handleParams(); - } - componentWillUpdate(nextProps, nextState) { - // No params - if (!nextProps.params) { - return - } - - // Search field changed - if (this.props.params.search - && (this.props.params.search !== nextProps.params.search) - && (nextProps.params.search !== "")) { - this.handleParams(nextProps); - return + if (this.props.moviesUrl) { + this.props.fetchMovies(this.props.moviesUrl); } } render() { diff --git a/src/public/js/components/navbar.js b/src/public/js/components/navbar.js index 97cc8ac..51fbc1f 100644 --- a/src/public/js/components/navbar.js +++ b/src/public/js/components/navbar.js @@ -30,8 +30,10 @@ export default function NavBar(props) { placeholder="Search movies" router={props.router} search={props.movieStore.search} + searchFunc={props.searchMovies} path='/movies/search' pathMatch='movies' + params={props.params} /> @@ -52,14 +56,40 @@ class Search extends React.Component { constructor(props) { super(props); this.handleSearch = this.handleSearch.bind(this); + this.search(this.props); } handleSearch() { - this.props.router.push(`${this.props.path}/${encodeURI(this.props.search)}`) + if (this.props.search === "") { + return; + } + this.search(this.props); + this.props.router.push(`${this.props.path}/${encodeURI(this.props.search)}`); + } + isActive() { + const location = this.props.router.getCurrentLocation().pathname; + return (location.indexOf(this.props.pathMatch) !== -1) + } + search(props) { + if (!this.isActive()) { + return; + } + + // Search from the props if defined + if (props.search !== "") { + props.searchFunc({ key: props.search }); + return; + } + + // Search from the url params + if (props.params + && props.params.search + && props.params.search !== "") { + props.searchFunc({ key: props.params.search }); + return; + } } render() { - const location = this.props.router.getCurrentLocation().pathname; - if (location.indexOf(this.props.pathMatch) === -1) - { + if (!this.isActive()) { return null; } diff --git a/src/public/js/components/shows/list.js b/src/public/js/components/shows/list.js index e783c49..91a2e17 100644 --- a/src/public/js/components/shows/list.js +++ b/src/public/js/components/shows/list.js @@ -6,40 +6,9 @@ import Loader from '../loader/loader' import ShowButtons from './listButtons' export default class ShowList extends React.Component { - handleParams(props = this.props) { - // Check if the URL to fetch is in the props - if (props.showsUrl) { - this.props.fetchShows(props.showsUrl); - return - } - - // Check for URL parameters - if (!props.params) { - return - } - - // Search param - if (props.params.search && props.params.search !== "") { - this.props.searchShows({ - key: this.props.params.search - }); - return - } - } componentWillMount() { - this.handleParams(); - } - componentWillUpdate(nextProps, nextState) { - // No params - if (!nextProps.params) { - return - } - - // Search field changed - if (this.props.params.search - && (this.props.params.search !== nextProps.params.search) - && (nextProps.params.search !== "")) { - this.handleParams(nextProps); + if (this.props.showsUrl) { + this.props.fetchShows(this.props.showsUrl); return } }