Simplify the search module

This commit is contained in:
Grégoire Delattre 2017-04-19 13:41:34 +02:00
parent ed46bdcf5e
commit fa73dc09c2
3 changed files with 40 additions and 70 deletions

View File

@ -41,41 +41,12 @@ function MovieButtons(props) {
} }
export default class MovieList extends React.Component { export default class MovieList extends React.Component {
handleParams(props = this.props) { constructor(props) {
// Check if the URL to fetch is in the props super(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
}
} }
componentWillMount() { componentWillMount() {
this.handleParams(); if (this.props.moviesUrl) {
} this.props.fetchMovies(this.props.moviesUrl);
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
} }
} }
render() { render() {

View File

@ -30,8 +30,10 @@ export default function NavBar(props) {
placeholder="Search movies" placeholder="Search movies"
router={props.router} router={props.router}
search={props.movieStore.search} search={props.movieStore.search}
searchFunc={props.searchMovies}
path='/movies/search' path='/movies/search'
pathMatch='movies' pathMatch='movies'
params={props.params}
/> />
<Search <Search
model="showStore" model="showStore"
@ -39,8 +41,10 @@ export default function NavBar(props) {
placeholder="Search shows" placeholder="Search shows"
router={props.router} router={props.router}
search={props.showStore.search} search={props.showStore.search}
searchFunc={props.searchShows}
path='/shows/search' path='/shows/search'
pathMatch='shows' pathMatch='shows'
params={props.params}
/> />
</Navbar.Collapse> </Navbar.Collapse>
</Navbar> </Navbar>
@ -52,14 +56,40 @@ class Search extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.handleSearch = this.handleSearch.bind(this); this.handleSearch = this.handleSearch.bind(this);
this.search(this.props);
} }
handleSearch() { 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() { render() {
const location = this.props.router.getCurrentLocation().pathname; if (!this.isActive()) {
if (location.indexOf(this.props.pathMatch) === -1)
{
return null; return null;
} }

View File

@ -6,40 +6,9 @@ import Loader from '../loader/loader'
import ShowButtons from './listButtons' import ShowButtons from './listButtons'
export default class ShowList extends React.Component { 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() { componentWillMount() {
this.handleParams(); if (this.props.showsUrl) {
} this.props.fetchShows(this.props.showsUrl);
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 return
} }
} }