61 lines
1.7 KiB
JavaScript
61 lines
1.7 KiB
JavaScript
import React from "react"
|
|
import PropTypes from "prop-types"
|
|
import { Route } from "react-router-dom"
|
|
import { connect } from "react-redux"
|
|
import { fetchMovies, getMovieExploreOptions } from "../../actions/movies"
|
|
|
|
const mapStateToProps = (state) => ({
|
|
isExplorerFetched: (state.movieStore.get("exploreOptions").size !== 0)
|
|
});
|
|
const mapDispatchToProps = { fetchMovies, getMovieExploreOptions };
|
|
|
|
const MoviesRoute = ({
|
|
component: Component,
|
|
isExplorerFetched,
|
|
fetchMovies,
|
|
getMovieExploreOptions,
|
|
...otherProps
|
|
}) => {
|
|
return (
|
|
<Route {...otherProps} render={(props) => {
|
|
let fetchUrl = "";
|
|
switch (props.match.path) {
|
|
case "/movies/polochon":
|
|
fetchUrl = "/movies/polochon";
|
|
break;
|
|
case "/movies/wishlist":
|
|
fetchUrl = "/wishlist/movies";
|
|
break;
|
|
case "/movies/search/:search":
|
|
fetchUrl = "/movies/search/" + props.match.params.search;
|
|
break;
|
|
case "/movies/explore/:source/:category":
|
|
if (!isExplorerFetched) {
|
|
getMovieExploreOptions();
|
|
}
|
|
fetchUrl = "/movies/explore?source=" +
|
|
encodeURI(props.match.params.source) +
|
|
"&category=" + encodeURI(props.match.params.category);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
if (fetchUrl != "") {
|
|
fetchMovies(fetchUrl);
|
|
}
|
|
|
|
return <Component {...props} />
|
|
}} />
|
|
)
|
|
}
|
|
MoviesRoute.propTypes = {
|
|
component: PropTypes.func,
|
|
match: PropTypes.object,
|
|
isExplorerFetched: PropTypes.bool.isRequired,
|
|
fetchMovies: PropTypes.func.isRequired,
|
|
getMovieExploreOptions: PropTypes.func.isRequired,
|
|
};
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(MoviesRoute);
|