Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

redux filter pane

// Pane.js

import React from 'react';

function Pane({
  selectedYear,
  selectedGenre,
  selectedRating,
  years = [],
  genres = [],
  ratings = [],
  sorting,
  onYearChange,
  onGenreChange,
  onRatingChange,
  onSortingChange,
}) {
  return (
    <div>
      <div>
        Filters:
        <div>
          Year:
          <select
            defaultValue={selectedYear}
            onChange={e => onYearChange(e.target.value)}
          >
            <option value="all" >All</option>
            {years.map((y, i) =>
              <option key={i} value={y}>{y}</option>
            )}
          </select>
        </div>
        <div>
          Genre:
          <select
            defaultValue={selectedGenre}
            onChange={e => onGenreChange(e.target.value)}
          >
            <option value="all" >All</option>
            {genres.map((g, i) =>
              <option key={i} value={g}>{g}</option>
            )}
          </select>
        </div>
        <div>
          Rating:
          <select
            defaultValue={selectedRating}
            onChange={e => onRatingChange(e.target.value)}
          >
            <option value="all" >All</option>
            {ratings.map((r, i) =>
              <option key={i} value={r}>{r}</option>
            )}
          </select>
        </div>
      </div>
      <div>
        Select sorting:
        <select
          defaultValue={sorting}
          onChange={e => onSortingChange(e.target.value)}
        >
          <option value="alphabetically">Alphabetically</option>
          <option value="year">Year</option>
          <option value="rating">Rating</option>
        </select>
      </div>
    </div>
  );
}

export default Pane;
Comment

redux filter pane container

// PaneContainer.js

import { connect } from 'react-redux';
import Pane from './Pane';

// Simple helper function, which return all filters from state by given key.
function getFilters(key, movies) {
  return movies.reduce((acc, movie) => {
    if (!acc.includes(movie[key])) {
      return [...acc, movie[key]];
    }
    return acc;
  }, []);
}

function mapStateToProps(state, props) {
  const { sorting, year, genre, rating } = state;
  return {
    selectedYear: year,
    selectedGenre: genre,
    selectedRating: rating,
    years: getFilters('year', state.movies),
    genres: getFilters('genre', state.movies),
    ratings: getFilters('rating', state.movies),
    sorting,
  };
}

function mapDispatchToProps(dispatch, props) {
  return {
    // Here, we are providing callbacks with dispatching functions.
    onYearChange(year) {
      dispatch({
        type: 'SET_YEAR',
        year,
      });
    },
    onGenreChange(genre) {
      dispatch({
        type: 'SET_GENRE',
        genre,
      });
    },
    onRatingChange(rating) {
      dispatch({
        type: 'SET_RATING',
        rating,
      });
    },
    onSortingChange(sorting) {
      dispatch({
        type: 'SET_SORTING',
        sorting,
      });
    },
  };
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(Pane);
Comment

PREVIOUS NEXT
Code Example
Javascript :: in javascript advertising on website only for 5 seconds 
Javascript :: svlete each index 
Javascript :: wait in js 
Javascript :: internacionalizacion ionic 
Javascript :: material ui notify 
Javascript :: how to bind the dropdown data using ajax in .net mvc 
Javascript :: array.of 
Javascript :: Pinterest Javascript 
Javascript :: how to find dublicates in string 
Javascript :: validate url in javascript 
Javascript :: divcontainer is null 
Javascript :: angular component with attribute selector 
Javascript :: liquid - array item accessing 
Javascript :: react testing library getBy image 
Javascript :: spherical.computeheading javascript 
Javascript :: carry forward session storage one page to another page in javascript 
Javascript :: 4.6.3. Order of Operations¶ 
Javascript :: bjsmasth delete 
Javascript :: instafeeed.js pulls back unknown for image file 
Javascript :: How To Add Google Social Login Button 
Javascript :: discord.js mention 
Javascript :: javascript ligten a color 
Javascript :: import multiple packages jsp 
Javascript :: sub_total.toFixed is not a function 
Javascript :: Using Scrip as Web app 
Javascript :: regex to get first word after slash in URL 
Javascript :: vdio Javascript 
Javascript :: socket mock 
Javascript :: put text inside an object javascript 
Javascript :: return where an property eqauls 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =