Move the fetch of the explorer options to its own component

This commit is contained in:
Grégoire Delattre 2020-04-02 18:43:57 +02:00
parent 937b12bb67
commit 5f7d402614
6 changed files with 35 additions and 43 deletions

View File

@ -1,10 +1,20 @@
import React from "react"; import React, { useEffect } from "react";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import { withRouter } from "react-router-dom"; import { withRouter } from "react-router-dom";
import { Form, FormGroup, FormControl, FormLabel } from "react-bootstrap"; import { Form, FormGroup, FormControl, FormLabel } from "react-bootstrap";
const ExplorerOptions = ({ display, params, options, type, history }) => { const ExplorerOptions = ({
// Should this componennt be displayed display,
params,
options,
type,
history,
fetch,
}) => {
useEffect(() => {
fetch();
}, [fetch]);
if (!display) { if (!display) {
return null; return null;
} }
@ -103,6 +113,7 @@ ExplorerOptions.propTypes = {
type: PropTypes.string, type: PropTypes.string,
options: PropTypes.object, options: PropTypes.object,
display: PropTypes.bool, display: PropTypes.bool,
fetch: PropTypes.func,
}; };
export default withRouter(ExplorerOptions); export default withRouter(ExplorerOptions);

View File

@ -55,6 +55,7 @@ const ListPosters = (props) => {
type={props.type} type={props.type}
display={displayExplorerOptions} display={displayExplorerOptions}
params={props.params} params={props.params}
fetch={props.exploreFetchOptions}
options={props.exploreOptions} options={props.exploreOptions}
/> />
<Posters <Posters
@ -81,6 +82,7 @@ ListPosters.propTypes = {
placeHolder: PropTypes.string.isRequired, placeHolder: PropTypes.string.isRequired,
updateFilter: PropTypes.func.isRequired, updateFilter: PropTypes.func.isRequired,
filter: PropTypes.string, filter: PropTypes.string,
exploreFetchOptions: PropTypes.func,
}; };
export default ListPosters; export default ListPosters;

View File

@ -6,6 +6,8 @@ import {
selectMovie, selectMovie,
updateFilter, updateFilter,
movieWishlistToggle, movieWishlistToggle,
fetchMovies,
getMovieExploreOptions,
} from "../../actions/movies"; } from "../../actions/movies";
import ListDetails from "../list/details"; import ListDetails from "../list/details";
@ -31,6 +33,8 @@ const mapDispatchToProps = {
selectMovie, selectMovie,
updateFilter, updateFilter,
movieWishlistToggle, movieWishlistToggle,
fetchMovies,
getMovieExploreOptions,
}; };
const MovieList = (props) => { const MovieList = (props) => {
@ -45,6 +49,7 @@ const MovieList = (props) => {
data={props.movies} data={props.movies}
type="movies" type="movies"
placeHolder="Filter movies..." placeHolder="Filter movies..."
exploreFetchOptions={props.getMovieExploreOptions}
exploreOptions={props.exploreOptions} exploreOptions={props.exploreOptions}
selectedImdbId={props.selectedImdbId} selectedImdbId={props.selectedImdbId}
updateFilter={props.updateFilter} updateFilter={props.updateFilter}
@ -96,6 +101,8 @@ MovieList.propTypes = {
selectedImdbId: PropTypes.string, selectedImdbId: PropTypes.string,
filter: PropTypes.string, filter: PropTypes.string,
loading: PropTypes.bool, loading: PropTypes.bool,
fetchMovies: PropTypes.func,
getMovieExploreOptions: PropTypes.func,
updateFilter: PropTypes.func, updateFilter: PropTypes.func,
movieWishlistToggle: PropTypes.func, movieWishlistToggle: PropTypes.func,
selectMovie: PropTypes.func, selectMovie: PropTypes.func,

View File

@ -2,20 +2,9 @@ import React from "react";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import { Route } from "react-router-dom"; import { Route } from "react-router-dom";
import { connect } from "react-redux"; import { connect } from "react-redux";
import { fetchMovies, getMovieExploreOptions } from "../../actions/movies"; import { fetchMovies } from "../../actions/movies";
const mapStateToProps = (state) => ({ const MoviesRoute = ({ component: Component, fetchMovies, ...otherProps }) => {
isExplorerFetched: state.movieStore.get("exploreOptions").size !== 0,
});
const mapDispatchToProps = { fetchMovies, getMovieExploreOptions };
const MoviesRoute = ({
component: Component,
isExplorerFetched,
fetchMovies,
getMovieExploreOptions,
...otherProps
}) => {
return ( return (
<Route <Route
{...otherProps} {...otherProps}
@ -32,9 +21,6 @@ const MoviesRoute = ({
fetchUrl = "/movies/search/" + props.match.params.search; fetchUrl = "/movies/search/" + props.match.params.search;
break; break;
case "/movies/explore/:source/:category": case "/movies/explore/:source/:category":
if (!isExplorerFetched) {
getMovieExploreOptions();
}
fetchUrl = fetchUrl =
"/movies/explore?source=" + "/movies/explore?source=" +
encodeURI(props.match.params.source) + encodeURI(props.match.params.source) +
@ -57,9 +43,8 @@ const MoviesRoute = ({
MoviesRoute.propTypes = { MoviesRoute.propTypes = {
component: PropTypes.object, component: PropTypes.object,
match: PropTypes.object, match: PropTypes.object,
isExplorerFetched: PropTypes.bool.isRequired,
fetchMovies: PropTypes.func.isRequired, fetchMovies: PropTypes.func.isRequired,
getMovieExploreOptions: PropTypes.func.isRequired, getMovieExploreOptions: PropTypes.func.isRequired,
}; };
export default connect(mapStateToProps, mapDispatchToProps)(MoviesRoute); export default connect(null, { fetchMovies })(MoviesRoute);

View File

@ -7,6 +7,7 @@ import {
showWishlistToggle, showWishlistToggle,
getShowDetails, getShowDetails,
updateFilter, updateFilter,
getShowExploreOptions,
} from "../../actions/shows"; } from "../../actions/shows";
import { isWishlisted } from "../../utils"; import { isWishlisted } from "../../utils";
@ -28,6 +29,7 @@ const mapDispatchToProps = {
showWishlistToggle, showWishlistToggle,
getShowDetails, getShowDetails,
updateFilter, updateFilter,
getShowExploreOptions,
}; };
const ShowList = (props) => { const ShowList = (props) => {
@ -47,6 +49,7 @@ const ShowList = (props) => {
type="shows" type="shows"
placeHolder="Filter shows..." placeHolder="Filter shows..."
exploreOptions={props.exploreOptions} exploreOptions={props.exploreOptions}
exploreFetchOptions={props.getShowExploreOptions}
updateFilter={props.updateFilter} updateFilter={props.updateFilter}
selectedImdbId={props.selectedImdbId} selectedImdbId={props.selectedImdbId}
filter={props.filter} filter={props.filter}
@ -91,5 +94,6 @@ ShowList.propTypes = {
selectShow: PropTypes.func, selectShow: PropTypes.func,
getShowDetails: PropTypes.func, getShowDetails: PropTypes.func,
updateFilter: PropTypes.func, updateFilter: PropTypes.func,
getShowExploreOptions: PropTypes.func,
}; };
export default connect(mapStateToProps, mapDispatchToProps)(ShowList); export default connect(mapStateToProps, mapDispatchToProps)(ShowList);

View File

@ -2,27 +2,12 @@ import React from "react";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import { Route } from "react-router-dom"; import { Route } from "react-router-dom";
import { connect } from "react-redux"; import { connect } from "react-redux";
import { import { fetchShows, fetchShowDetails } from "../../actions/shows";
fetchShows,
fetchShowDetails,
getShowExploreOptions,
} from "../../actions/shows";
const mapStateToProps = (state) => ({
isExplorerFetched: state.showsStore.get("exploreOptions").size !== 0,
});
const mapDispatchToProps = {
fetchShows,
fetchShowDetails,
getShowExploreOptions,
};
const ShowsRoute = ({ const ShowsRoute = ({
component: Component, component: Component,
isExplorerFetched,
fetchShows, fetchShows,
fetchShowDetails, fetchShowDetails,
getShowExploreOptions,
...otherProps ...otherProps
}) => { }) => {
return ( return (
@ -41,9 +26,6 @@ const ShowsRoute = ({
fetchUrl = "/shows/search/" + props.match.params.search; fetchUrl = "/shows/search/" + props.match.params.search;
break; break;
case "/shows/explore/:source/:category": case "/shows/explore/:source/:category":
if (!isExplorerFetched) {
getShowExploreOptions();
}
fetchUrl = fetchUrl =
"/shows/explore?source=" + "/shows/explore?source=" +
encodeURI(props.match.params.source) + encodeURI(props.match.params.source) +
@ -67,10 +49,11 @@ const ShowsRoute = ({
ShowsRoute.propTypes = { ShowsRoute.propTypes = {
component: PropTypes.object, component: PropTypes.object,
match: PropTypes.object, match: PropTypes.object,
isExplorerFetched: PropTypes.bool.isRequired,
fetchShows: PropTypes.func.isRequired, fetchShows: PropTypes.func.isRequired,
fetchShowDetails: PropTypes.func.isRequired, fetchShowDetails: PropTypes.func.isRequired,
getShowExploreOptions: PropTypes.func.isRequired,
}; };
export default connect(mapStateToProps, mapDispatchToProps)(ShowsRoute); export default connect(null, {
fetchShows,
fetchShowDetails,
})(ShowsRoute);