redux filter movies list container

Solutions on MaxInterview for redux filter movies list container by the best coders in the world

showing results for - "redux filter movies list container"
Dexter
27 May 2019
1// MoviesContainer.js
2
3import { connect } from 'react-redux';
4import Movies from './Movies';
5
6// Getting visible movies from state.
7function getVisibleMovies(year, genre, rating, sorting, movies) {
8  return movies
9    .filter(m => {
10      return (
11        (year == 'all' || year == m.year) &&
12        (genre == 'all' || genre == m.genre) &&
13        (rating == 'all' || rating == m.rating)
14      );
15    })
16    .sort((a, b) => {
17      if (sorting == 'year') {
18        return b.year - a.year;
19      }
20      if (sorting == 'rating') {
21        return b.rating - a.rating;
22      }
23      if (sorting == 'alphabetically') {
24        return a.title > b.title ? 1 : a.title < b.title ? -1 : 0;
25      }
26    });
27}
28
29function mapStateToProps(state) {
30  const { year, genre, rating, sorting, movies } = state;
31  return {
32    movies: getVisibleMovies(year, genre, rating, sorting, movies),
33  };
34}
35
36export default connect(mapStateToProps)(Movies);
37