File size: 1,858 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import {
    GET_POPULAR_MOVIES,
    SEARCH_MOVIES,
    LOAD_MORE_MOVIES,
    CLEAR_MOVIES,
    SHOW_LOADING_SPINNER
} from '../actions/index'


const defaultState = {
    movies: [],
    heroImage: null,
    loading: false,
    currentPage: 0,
    totalPages: 0,
    searchTerm: ''
}

// It will take the present state and action will update the state based on the action in the reducer
export default function (state = defaultState, action) {
    switch(action.type) {
        case GET_POPULAR_MOVIES:
            return {
                ...state,
                movies: action.payload.results,
                heroImage: state.heroImage || action.payload.results[0],// It will grab the most popular movie and put the backdrop image in our hero image
                loading: false,
                currentPage: action.payload.page,
                totalPages: action.payload.total_pages,
                searchTerm: ""
            }
        case LOAD_MORE_MOVIES:
            return {
                ...state,
                movies: [...state.movies, ...action.payload.results],
                loading: false,
                currentPage: action.payload.page,
                totalPages: action.payload.total_pages
            }

        case SEARCH_MOVIES:
            return {
                ...state,
                movies: action.payload.results,
                loading: false,
                currentPage: action.payload.page,
                totalPages: action.payload.total_pages,
                searchTerm: action.payload.searchTerm
            }
        case CLEAR_MOVIES:
            return {
                ...state,
                movies: []
            }
        case SHOW_LOADING_SPINNER:
            return {
                ...state,
                loading: true
            }
        default:
            return state;
    }
}