File size: 2,804 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import React, { Component } from 'react';
import { connect } from 'react-redux';

import {
  getMovie,
  showLoadingSpinner,
  clearMovie
} from '../actions';

import Movie from '../components/Movie/Movie';

class MovieContainer extends Component {
  componentDidMount() {

    const { movieId } = this.props.match.params;
    this.getMovie(movieId);
  }

  getMovie = movieId => {
    this.props.clearMovie();
    this.props.showLoadingSpinner();
    this.props.getMovie(movieId);

  }

  render() {
    return (
      <Movie
        movie={this.props.movie}
        directors={this.props.directors}
        actors={this.props.actors}
        loading={this.props.loading}
      />
    )
  }
}


const mapStateToProps = state => {
  //returning movie from redux state
  return state.movie;
}

const mapDispatchToProps = {
  getMovie,
  clearMovie,
  showLoadingSpinner
}

export default connect(  // A higher order function 
  mapStateToProps,
  mapDispatchToProps
)(MovieContainer); // HomeContainer gets connect to the redux store






























// state = {
//     movie: null,
//     actors: null,
//     directors: [],
//     loading: false
//   }

//   componentDidMount() {
//     // ES6 destructuring the props
//     const { movieId } = this.props.match.params;

//     if (localStorage.getItem(`${movieId}`)) {
//       let state = JSON.parse(localStorage.getItem(`${movieId}`))
//       this.setState({ ...state })
//     } else {
//       this.setState({ loading: true })
//       // First fetch the movie ...
//       let endpoint = `${API_URL}movie/${movieId}?api_key=${API_KEY}&language=en-US`;
//       this.fetchItems(endpoint);
//     }
//   }

//   fetchItems = (endpoint) => {
//     // ES6 destructuring the props
//     const { movieId } = this.props.match.params;

//     fetch(endpoint)
//     .then(result => result.json())
//     .then(result => {

//       if (result.status_code) {
//         // If we don't find any movie
//         this.setState({ loading: false });
//       } else {
//         this.setState({ movie: result }, () => {
//           // ... then fetch actors in the setState callback function
//           let endpoint = `${API_URL}movie/${movieId}/credits?api_key=${API_KEY}`;
//           fetch(endpoint)
//           .then(result => result.json())
//           .then(result => {

//             const directors = result.crew.filter( (member) => member.job === "Director");

//             this.setState({
//               actors: result.cast,
//               directors,
//               loading: false
//             }, () => {
//               localStorage.setItem(`${movieId}`, JSON.stringify(this.state));
//             })
//           })
//         })
//       }
//     })
//     .catch(error => console.error('Error:', error))
//   }