Commit d2d751e5 authored by Thor-Herman's avatar Thor-Herman
Browse files

Add reducers from project 3 #2

parent e8edb3b7
import {combineReducers} from "redux";
import {CHANGE_FILTER, CLEAR_FILTER, FilterAction, FilterState} from "../types/filter";
import {CHANGE_ORDERING, CLEAR_ORDERING, OrderingAction, OrderingState} from "../types/ordering";
const initialFilterState = {
genres: {
Action: false,
Romance: false,
Horror: false,
Comedy: false,
Musical: false,
Drama: false,
},
year: {to: "", from: ""}
}
const filter = (state: FilterState = initialFilterState, action: FilterAction): FilterState => {
switch (action.type) {
case CHANGE_FILTER:
return {...action.payload}
case CLEAR_FILTER:
return {...initialFilterState};
default:
return state;
}
};
const ordering = (state: OrderingState = {key: "title", order: "asc"},
action: OrderingAction): OrderingState => {
switch (action.type) {
case CHANGE_ORDERING:
return {...state, ...action.payload};
case CLEAR_ORDERING:
return {key: "", order: "asc"};
default:
return state;
}
};
export default combineReducers({
filter,
ordering,
});
\ No newline at end of file
import { combineReducers } from "redux";
import {combineReducers} from "redux";
import movies from './movieReducer';
import filtering from './filterReducer';
import page from './pageReducer';
const dummyReducer = (type: string, action: any) => {
return "State";
}
const rootReducer = combineReducers(
{movies, filtering, page}
);
export default combineReducers({
state: dummyReducer,
});
\ No newline at end of file
export type RootState = ReturnType<typeof rootReducer>;
export default rootReducer;
\ No newline at end of file
import {combineReducers} from "redux";
import {ADD_MOVIE, MovieActionTypes, SEARCH_TITLES, UPDATE_MOVIES} from '../types/movies';
import _ from 'lodash';
import { Movie } from "../types/movies";
type byIdState = {
[key: number]: Movie
}
type allIdsState = Array<Movie>;
const byId = (state: byIdState = {}, action: MovieActionTypes) => {
switch (action.type) {
case ADD_MOVIE:
return {...state, [action.payload.id]: action.payload};
case UPDATE_MOVIES:
return action.payload;
default:
return state;
}
};
const allIds = (state: allIdsState = [], action: MovieActionTypes) => {
switch (action.type) {
case ADD_MOVIE:
return [...state, action.payload.id];
case UPDATE_MOVIES:
return action.payload.map(movie => movie.id);
default:
return state;
}
};
const searchTerm = (state = "", action: MovieActionTypes) => {
switch (action.type){
case SEARCH_TITLES:
return action.payload
default:
return state;
}
}
export default combineReducers({
byId,
allIds,
searchTerm,
})
import {CHANGE_PAGE, PAGE_SIZE, ChangePageAction, PageState, UPDATE_TOTAL_PAGES, PageActions} from "../types/page";
const initialPageState = {
total: 1,
current: 1,
next: null,
prev: null,
}
const page = (state: PageState = initialPageState, action: PageActions) => {
switch(action.type) {
case CHANGE_PAGE:
case UPDATE_TOTAL_PAGES:
return {...state, ...action.payload}
default:
return state;
}
};
export default page;
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment