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

Add more comments #14

parent 810187c4
import {
CHANGE_ORDERING,
OrderingAction,
OrderingActionPayload,
} from '../types/ordering';
import AppThunk from '../types';
import { searchMovies, searchMovieTitles } from './movieActions';
import { CHANGE_FILTER, FilterAction, FilterFormData } from '../types/filter';
import { CHANGE_FILTER, FilterFormData } from '../types/filter';
import _ from 'lodash';
export const applyFilter = (action: OrderingAction | FilterAction): AppThunk => (
dispatch,
getState
) => {
dispatch(action);
return new Promise((resolve) => {
dispatch(searchMovies(true));
resolve();
});
};
export const changeOrdering = (newOrder: OrderingActionPayload) => {
return { type: CHANGE_ORDERING, payload: newOrder };
};
......@@ -28,7 +14,7 @@ export const changeFilters = (data: FilterFormData) => {
type: CHANGE_FILTER,
payload: {
genres: {
..._.omit(data, ['to', 'from']), // Will return every genre and not to and from properties
..._.omit(data, ['to', 'from']), // Will return every genre and not to and from properties. TODO: Remove
},
year: { to: data.to, from: data.from },
},
......
......@@ -4,7 +4,7 @@ import axiosREST from "../api/axiosREST";
import {updatePages} from "./pageActions";
import {decideFilters} from "./utility/decideFilters";
// Add a single movie to the current collection
export const addMovie = (movie: Movie): MovieActionTypes => {
return {
type: ADD_MOVIE,
......@@ -12,6 +12,7 @@ export const addMovie = (movie: Movie): MovieActionTypes => {
}
};
// Replaces what movies are stored
export const updateMovies = (movies: Array<Movie>): MovieActionTypes => {
return {
type: UPDATE_MOVIES,
......@@ -19,6 +20,7 @@ export const updateMovies = (movies: Array<Movie>): MovieActionTypes => {
}
}
// Adds movies to currently stored
export const addMovies = (movies: Array<Movie>): MovieActionTypes => {
return {
type: ADD_MOVIES,
......@@ -26,6 +28,7 @@ export const addMovies = (movies: Array<Movie>): MovieActionTypes => {
}
}
// Updates search term
export const searchMovieTitles = (searchTerm: string): MovieActionTypes => {
return {
type: SEARCH_TITLES,
......@@ -33,6 +36,7 @@ export const searchMovieTitles = (searchTerm: string): MovieActionTypes => {
}
};
// Fetches a single movie
export const fetchMovie = (id: number): AppThunk => {
return async (dispatch) => {
const response = await axiosREST.get<Movie>(`/movies/${id}`);
......@@ -40,6 +44,7 @@ export const fetchMovie = (id: number): AppThunk => {
};
};
// Executes a REST query to backend with the current search term and filters
export const searchMovies = (resetPages = false, update = true): AppThunk => {
return async (dispatch, getState) => {
const filtering = getState().filtering;
......@@ -48,10 +53,10 @@ export const searchMovies = (resetPages = false, update = true): AppThunk => {
const filters = decideFilters(filtering);
const query = `/movies?search=${searchTerm}${filters}&page=${currentPage}`
const response = await axiosREST.get<MoviePage>(query);
if (update) {
if (update) { // Should current selection be replaced or added to?
dispatch(updateMovies(response.data.results));
}
else {
else { // This is the case when loading more results
dispatch(addMovies(response.data.results));
}
dispatch(updatePages(response.data.count, currentPage));
......
......@@ -18,11 +18,11 @@ export const updatePages = (count: number, current: number): PageActions => {
}
};
export const newPage = (newCurrent: number, total: number): AppThunk => (dispatch, getState) => {
export const newPage = (newCurrent: number, total: number): AppThunk => (dispatch) => {
const next = newCurrent < total ? newCurrent + 1 : null;
const prev = newCurrent > 1 ? newCurrent - 1 : null;
dispatch(changePage(newCurrent, next, prev));
return new Promise((resolve) => {
return new Promise((resolve) => { // Done in order to dispatch sequentially
dispatch(searchMovies(false, false));
resolve();
})
......
import { FilteringState, Genres } from '../../types/filter';
import _ from 'lodash';
// Helper method. Converts the selected filters to REST query
export const decideFilters = ({ filter, ordering }: FilteringState) => {
let returnString = '';
if (filter.year.to && filter.year.from) {
if (filter.year.to && filter.year.from) { // TODO: Remove
returnString += `&year__gte=${filter.year.from}&year__lte=${filter.year.to}`;
}
const regex = decideRegex(filter.genres);
......@@ -15,6 +16,7 @@ export const decideFilters = ({ filter, ordering }: FilteringState) => {
return returnString;
};
// Helper method. Decides the RegEx in REST query for genres
const decideRegex = (filterGenres: { [key in Genres]: boolean }) => {
let regex = '(';
const checkedGenres = _.keys(_.pickBy(filterGenres, (genre: boolean) => genre))
......
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