Commit 81f58ea2 authored by Thor-Herman's avatar Thor-Herman
Browse files

Add actions from project 3 #2

parent e2331f47
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 _ from 'lodash';
const applyFilter = (action: OrderingAction | FilterAction): AppThunk =>
(dispatch, getState) => {
dispatch(action);
return new Promise((resolve) => {
dispatch(searchMovies(true));
resolve();
})
}
export const changeOrdering = (newOrder: OrderingActionPayload) =>
applyFilter({type: CHANGE_ORDERING, payload: newOrder});
export const changeFilters = (data: FilterFormData) => {
return applyFilter({type: CHANGE_FILTER, payload: {
genres: {
..._.omit(data, ["to", "from"]) // Will return every genre and not to and from properties
},
year: {to: data.to, from: data.from}
}});
}
\ No newline at end of file
export * from './movieActions';
export * from './filterActions';
export * from './pageActions';
\ No newline at end of file
import AppThunk from '../types';
import {ADD_MOVIE, SEARCH_TITLES, Movie, MovieActionTypes, UPDATE_MOVIES, MoviePage} from "../types/movies";
import axiosREST from "../api/axiosREST";
import {updatePages} from "./pageActions";
import {decideFilters} from "./utility/decideFilters";
export const addMovie = (movie: Movie): MovieActionTypes => {
return {
type: ADD_MOVIE,
payload: movie
}
};
export const updateMovies = (movies: Array<Movie>): MovieActionTypes => {
return {
type: UPDATE_MOVIES,
payload: movies,
}
}
export const searchMovieTitles = (searchTerm: string): MovieActionTypes => {
return {
type: SEARCH_TITLES,
payload: searchTerm
}
};
export const fetchMovie = (id: number): AppThunk => {
return async (dispatch) => {
const response = await axiosREST.get<Movie>(`/movies/${id}`);
dispatch(addMovie(response.data));
};
};
export const searchMovies = (resetPages = false): AppThunk => {
return async (dispatch, getState) => {
const filtering = getState().filtering;
const searchTerm = getState().movies.searchTerm;
const currentPage = resetPages ? 1 : getState().page.current;
const filters = decideFilters(filtering);
const query = `/movies?search=${searchTerm}${filters}&page=${currentPage}`
const response = await axiosREST.get<MoviePage>(query);
dispatch(updateMovies(response.data.results));
dispatch(updatePages(response.data.count, currentPage));
}
};
\ No newline at end of file
import {CHANGE_PAGE, PAGE_SIZE, PageActions, UPDATE_TOTAL_PAGES} from "../types/page";
import AppThunk from "../types";
import {searchMovies} from "./movieActions";
export const changePage = (newCurrent: number, next: number | null, prev: number | null): PageActions => {
return {
type: CHANGE_PAGE,
payload: {current: newCurrent, next, prev}
}
};
export const updatePages = (count: number, current: number): PageActions => {
const total = Math.ceil(count / PAGE_SIZE);
return {
type: UPDATE_TOTAL_PAGES,
payload: {total, current},
}
};
export const newPage = (newCurrent: number, total: number): AppThunk => (dispatch, getState) => {
const next = newCurrent < total ? newCurrent + 1 : null;
const prev = newCurrent > 1 ? newCurrent - 1 : null;
dispatch(changePage(newCurrent, next, prev));
return new Promise((resolve) => {
dispatch(searchMovies());
resolve();
})
};
\ No newline at end of file
import { FilteringState, Genres } from '../../types/filter';
import _ from 'lodash';
export const decideFilters = ({ filter, ordering }: FilteringState) => {
let returnString = '';
if (filter.year.to && filter.year.from) {
returnString += `&year__gte=${filter.year.from}&year__lte=${filter.year.to}`;
}
const regex = decideRegex(filter.genres);
returnString += `&genre__regex=${regex}`;
if (ordering) {
const orderingPrefix = ordering.order === 'asc' ? '' : '-'; // Django needs - to sort desc
returnString += `&ordering=${orderingPrefix}${ordering.key}`;
}
return returnString;
};
const decideRegex = (filterGenres: { [key in Genres]: boolean }) => {
let regex = '(';
const checkedGenres = _.values(_.pickBy(filterGenres, (genre: boolean) => genre))
for (let i = 0; i < checkedGenres.length; i++) {
regex += checkedGenres[i].toString();
regex += i === checkedGenres.length - 1 ? '' : '|'; // Last element, don't add final |
}
regex += ')';
return regex;
};
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