Commit 259fbe66 authored by Thor-Herman's avatar Thor-Herman
Browse files

Add addMovies action. Add parameter to search #10

This way you can change whether you want to update the search term or load more results
parent 0bea8e1c
import AppThunk from '../types';
import {ADD_MOVIE, SEARCH_TITLES, Movie, MovieActionTypes, UPDATE_MOVIES, MoviePage} from "../types/movies";
import {ADD_MOVIE, SEARCH_TITLES, Movie, MovieActionTypes, UPDATE_MOVIES, MoviePage, ADD_MOVIES} from "../types/movies";
import axiosREST from "../api/axiosREST";
import {updatePages} from "./pageActions";
import {decideFilters} from "./utility/decideFilters";
......@@ -19,6 +19,13 @@ export const updateMovies = (movies: Array<Movie>): MovieActionTypes => {
}
}
export const addMovies = (movies: Array<Movie>): MovieActionTypes => {
return {
type: ADD_MOVIES,
payload: movies,
}
}
export const searchMovieTitles = (searchTerm: string): MovieActionTypes => {
return {
type: SEARCH_TITLES,
......@@ -33,7 +40,7 @@ export const fetchMovie = (id: number): AppThunk => {
};
};
export const searchMovies = (resetPages = false): AppThunk => {
export const searchMovies = (resetPages = false, update = true): AppThunk => {
return async (dispatch, getState) => {
const filtering = getState().filtering;
const searchTerm = getState().movies.searchTerm;
......@@ -41,7 +48,12 @@ export const searchMovies = (resetPages = false): AppThunk => {
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));
if (update) {
dispatch(updateMovies(response.data.results));
}
else {
dispatch(addMovies(response.data.results));
}
dispatch(updatePages(response.data.count, currentPage));
}
};
\ No newline at end of file
......@@ -23,7 +23,7 @@ export const newPage = (newCurrent: number, total: number): AppThunk => (dispatc
const prev = newCurrent > 1 ? newCurrent - 1 : null;
dispatch(changePage(newCurrent, next, prev));
return new Promise((resolve) => {
dispatch(searchMovies());
dispatch(searchMovies(false, false));
resolve();
})
};
\ 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