Commit 9d8bc388 authored by Thor-Herman's avatar Thor-Herman
Browse files

Add ADD_MOVIES action #2 #10

Re-implemented from project 3 in order to handle infinite scrolling and maintain order of items
parent 1afa7d8f
import {combineReducers} from "redux";
import {ADD_MOVIE, MovieActionTypes, SEARCH_TITLES, UPDATE_MOVIES} from '../types/movies';
import {ADD_MOVIE, ADD_MOVIES, MovieActionTypes, SEARCH_TITLES, UPDATE_MOVIES} from '../types/movies';
import _ from 'lodash';
import { Movie } from "../types/movies";
......@@ -13,8 +13,10 @@ const byId = (state: byIdState = {}, action: MovieActionTypes) => {
switch (action.type) {
case ADD_MOVIE:
return {...state, [action.payload.id]: action.payload};
case ADD_MOVIES:
return {...state, ..._.mapKeys(action.payload, "id")};
case UPDATE_MOVIES:
return action.payload;
return _.mapKeys(action.payload, "id");
default:
return state;
}
......@@ -24,6 +26,9 @@ const allIds = (state: allIdsState = [], action: MovieActionTypes) => {
switch (action.type) {
case ADD_MOVIE:
return [...state, action.payload.id];
case ADD_MOVIES:
const Ids = action.payload.map(movie => movie.id);
return [...state, ...Ids];
case UPDATE_MOVIES:
return action.payload.map(movie => movie.id);
default:
......
export const LOG_IN = "LOG_IN";
export const LOG_OUT = "LOG_OUT";
export const LOG_IN = 'LOG_IN';
export const LOG_OUT = 'LOG_OUT';
export const ADD_MOVIE = 'ADD_MOVIE';
export const ADD_MOVIES = 'ADD_MOVIES';
export const UPDATE_MOVIES = 'UPDATE_MOVIES';
export const SEARCH_TITLES = 'SEARCH_TITLES';
export type Movie = {
id: number,
title: string,
genre: string,
year: number,
length: number,
description: string,
image: string,
}
id: number;
title: string;
genre: string;
year: number;
length: number;
description: string;
image: string;
};
export type MoviePage = {
count: number,
next: string | null,
previous: string | null,
results: Movie[],
}
count: number;
next: string | null;
previous: string | null;
results: Movie[];
};
type FetchMovieAction = {
type: typeof ADD_MOVIE,
payload: Movie
}
type: typeof ADD_MOVIE;
payload: Movie;
};
type AddMoviesAction = {
type: typeof ADD_MOVIES;
payload: Array<Movie>;
};
type UpdateMoviesAction = {
type: typeof UPDATE_MOVIES,
payload: Array<Movie>
}
type: typeof UPDATE_MOVIES;
payload: Array<Movie>;
};
type SearchTitlesAction = {
type: typeof SEARCH_TITLES;
payload: Record<Movie['id'], Movie['title']>
}
type: typeof SEARCH_TITLES;
payload: Record<Movie['id'], Movie['title']>;
};
export type MovieActionTypes = FetchMovieAction | SearchTitlesAction | UpdateMoviesAction;
export type MovieActionTypes =
| FetchMovieAction
| SearchTitlesAction
| UpdateMoviesAction
| AddMoviesAction;
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