Created
May 19, 2018 21:15
-
-
Save Belobobr/7d1138431efb28a78027bc90703d5b12 to your computer and use it in GitHub Desktop.
Combining Reducers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import {combineReducers} from 'redux'; | |
| import {createApiReducer} from './utils'; | |
| import { | |
| CATEGORIES, | |
| CHANNELS, | |
| COUNTRIES, | |
| filterLoadedAction, | |
| filterLoadingAction, | |
| filterLoadingErrorAction, | |
| GENRES, | |
| LANGUAGES, | |
| MOVIES | |
| } from '../constants/actionTypes' | |
| import {CATEGORY, COUNTRY, GENRE, LANGUAGE} from './../constants/filterTypes'; | |
| import type {CategoryFilter, CountryFilter, GenreFilter, LanguageFilter} from "../entities"; | |
| export type FilterState<T> = { | |
| data: Array<T> | |
| } | |
| export type EntityFiltersState = { | |
| category: FilterState<CategoryFilter>, | |
| genre: FilterState<GenreFilter>, | |
| language: FilterState<LanguageFilter>, | |
| country: FilterState<CountryFilter> | |
| } | |
| export type FiltersState = { | |
| movies: EntityFiltersState, | |
| channels: EntityFiltersState | |
| } | |
| function createFilterReducer(contentType, filterType) { | |
| return createApiReducer([ | |
| filterLoadingAction(contentType, filterType), | |
| filterLoadedAction(contentType, filterType), | |
| filterLoadingErrorAction(contentType, filterType) | |
| ]) | |
| } | |
| const filterReducer = combineReducers({ | |
| movies: combineReducers({ | |
| [CATEGORY]: createFilterReducer(MOVIES, CATEGORIES), | |
| [GENRE]: createFilterReducer(MOVIES, GENRES), | |
| [LANGUAGE]: createFilterReducer(MOVIES, LANGUAGES), | |
| [COUNTRY]: createFilterReducer(MOVIES, COUNTRIES) | |
| }), | |
| channels: combineReducers({ | |
| [CATEGORY]: createFilterReducer(CHANNELS, CATEGORIES), | |
| [GENRE]: createFilterReducer(CHANNELS, GENRES), | |
| [LANGUAGE]: createFilterReducer(CHANNELS, LANGUAGES), | |
| [COUNTRY]: createFilterReducer(CHANNELS, COUNTRIES) | |
| }) | |
| }); | |
| export default filterReducer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment