Commit 58d35bf8 authored by Thor-Herman's avatar Thor-Herman
Browse files

Add comments and remove unused code for reducers #15

parent a45f7405
import {combineReducers} from "redux";
import {CHANGE_FILTER, CLEAR_FILTER, FilterAction, FilterState} from "../types/filter";
import {CHANGE_ORDERING, CLEAR_ORDERING, OrderingAction, OrderingState} from "../types/ordering";
import {CHANGE_FILTER, FilterAction, FilterState} from "../types/filter";
import {CHANGE_ORDERING, OrderingAction, OrderingState} from "../types/ordering";
const initialFilterState = {
genres: {
......@@ -11,28 +11,25 @@ const initialFilterState = {
Musical: false,
Drama: false,
},
year: {to: "", from: ""}
year: {to: "", from: ""} // TODO: Remove
}
// Stores which genres are currently checked
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;
}
};
// Stores what the ordering term is and if it is ascending or descending
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;
}
......
......@@ -3,6 +3,7 @@ import movies from './movieReducer';
import filtering from './filterReducer';
import page from './pageReducer';
// Combines the different states into one root reducer
const rootReducer = combineReducers(
{movies, filtering, page}
);
......
......@@ -9,6 +9,7 @@ type byIdState = {
type allIdsState = Array<Movie>;
// An object containing each Movie object, where their Id is the key
const byId = (state: byIdState = {}, action: MovieActionTypes) => {
switch (action.type) {
case ADD_MOVIE:
......@@ -22,6 +23,7 @@ const byId = (state: byIdState = {}, action: MovieActionTypes) => {
}
};
// An array containing all the currently stored Ids of movies
const allIds = (state: allIdsState = [], action: MovieActionTypes) => {
switch (action.type) {
case ADD_MOVIE:
......@@ -36,6 +38,7 @@ const allIds = (state: allIdsState = [], action: MovieActionTypes) => {
}
};
// Stores the current search term
const searchTerm = (state = "", action: MovieActionTypes) => {
switch (action.type){
case SEARCH_TITLES:
......
import {CHANGE_PAGE, PAGE_SIZE, ChangePageAction, PageState, UPDATE_TOTAL_PAGES, PageActions} from "../types/page";
import {CHANGE_PAGE, PageState, UPDATE_TOTAL_PAGES, PageActions} from "../types/page";
const initialPageState = {
total: 1,
......
......@@ -5,6 +5,8 @@ import { Movie } from "../types/movies";
const selectMoviesById = (state: RootState): Record<number, Movie> => state.movies.byId;
const selectMoviesAllIds = (state: RootState): Array<number> => state.movies.allIds;
// Selects movies using the allIds ordering, which is the correct ordering received from backend
// byId is unordered since it is an object
export const selectMoviesByAllIdsOrdering = createSelector(
selectMoviesById,
selectMoviesAllIds,
......
import { compose, createStore, applyMiddleware } from 'redux';
import { createStore, applyMiddleware } from 'redux';
import reducers from '../reducers';
import thunk from 'redux-thunk';
import {composeWithDevTools} from 'redux-devtools-extension';
......
......@@ -12,7 +12,6 @@ export type FilterFormData = {
// Action types
export const CHANGE_FILTER = "CHANGE_FILTER";
export const CLEAR_FILTER = "CLEAR_FILTER";
// Redux action
export type FilterAction = { type: string, payload: FilterState };
......
export const CHANGE_ORDERING = "CHANGE_ORDERING";
export const CLEAR_ORDERING = "CLEAR_ORDERING";
export type OrderingOptions = "" | "title" | "length" | "year"
......
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