|
| 1 | +import React, { Component } from "react"; |
| 2 | +import { |
| 3 | + API_URL, |
| 4 | + API_KEY, |
| 5 | + IMAGE_BASE_URL, |
| 6 | + POSTER_SIZE, |
| 7 | + BACKDROP_SIZE |
| 8 | +} from "../../config"; |
| 9 | +import HeroImage from "../elements/HeroImage/HeroImage"; |
| 10 | +import SearchBar from "../elements/SearchBar/SearchBar"; |
| 11 | +import FourColGrid from "../elements/FourColGrid/FourColGrid"; |
| 12 | +import MovieThumb from "../elements/MovieThumb/MovieThumb"; |
| 13 | +import LoadMoreBtn from "../elements/LoadMoreBtn/LoadMoreBtn"; |
| 14 | +import Spinner from "../elements/Spinner/Spinner"; |
| 15 | +import "./Home.css"; |
| 16 | + |
| 17 | +class Home extends Component { |
| 18 | + state = { |
| 19 | + movies: [], |
| 20 | + heroImage: null, |
| 21 | + loading: false, |
| 22 | + currentPage: 0, |
| 23 | + totalPages: 0, |
| 24 | + searchTerm: "" |
| 25 | + }; |
| 26 | + |
| 27 | + componentDidMount() { |
| 28 | + if (localStorage.getItem("HomeState")) { |
| 29 | + let state = JSON.parse(localStorage.getItem("HomeState")); |
| 30 | + this.setState({ ...state }); |
| 31 | + } else { |
| 32 | + this.setState({ loading: true }); |
| 33 | + const endpoint = `${API_URL}movie/popular?api_key=${API_KEY}&language=en-US&page=1`; |
| 34 | + this.fetchItems(endpoint); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + searchItems = searchTerm => { |
| 39 | + let endpoint = ""; |
| 40 | + this.setState({ |
| 41 | + movies: [], |
| 42 | + loading: true, |
| 43 | + searchTerm |
| 44 | + }); |
| 45 | + |
| 46 | + if (searchTerm === "") { |
| 47 | + endpoint = `${API_URL}movie/popular?api_key=${API_KEY}&language=en-US&page=1`; |
| 48 | + } else { |
| 49 | + endpoint = `${API_URL}search/movie?api_key=${API_KEY}&language=en-US&query=${searchTerm}`; |
| 50 | + } |
| 51 | + this.fetchItems(endpoint); |
| 52 | + }; |
| 53 | + |
| 54 | + loadMoreItems = () => { |
| 55 | + // ES6 Destructuring the state |
| 56 | + const { searchTerm, currentPage } = this.state; |
| 57 | + |
| 58 | + let endpoint = ""; |
| 59 | + this.setState({ loading: true }); |
| 60 | + |
| 61 | + if (searchTerm === "") { |
| 62 | + endpoint = `${API_URL}movie/popular?api_key=${API_KEY}&language=en-US&page=${currentPage + |
| 63 | + 1}`; |
| 64 | + } else { |
| 65 | + endpoint = `${API_URL}search/movie?api_key=${API_KEY}&language=en-US&query=${searchTerm}&page=${currentPage + |
| 66 | + 1}`; |
| 67 | + } |
| 68 | + this.fetchItems(endpoint); |
| 69 | + }; |
| 70 | + |
| 71 | + fetchItems = endpoint => { |
| 72 | + // ES6 Destructuring the state |
| 73 | + const { movies, heroImage, searchTerm } = this.state; |
| 74 | + |
| 75 | + fetch(endpoint) |
| 76 | + .then(result => result.json()) |
| 77 | + .then(result => { |
| 78 | + this.setState( |
| 79 | + { |
| 80 | + movies: [...movies, ...result.results], |
| 81 | + heroImage: heroImage || result.results[0], |
| 82 | + loading: false, |
| 83 | + currentPage: result.page, |
| 84 | + totalPages: result.total_pages |
| 85 | + }, |
| 86 | + () => { |
| 87 | + if (this.state.searchTerm === "") { |
| 88 | + localStorage.setItem("HomeState", JSON.stringify(this.state)); |
| 89 | + } |
| 90 | + } |
| 91 | + ); |
| 92 | + }) |
| 93 | + .catch(error => console.error("Error:", error)); |
| 94 | + }; |
| 95 | + |
| 96 | + render() { |
| 97 | + // ES6 Destructuring the state |
| 98 | + const { |
| 99 | + movies, |
| 100 | + heroImage, |
| 101 | + loading, |
| 102 | + currentPage, |
| 103 | + totalPages, |
| 104 | + searchTerm |
| 105 | + } = this.state; |
| 106 | + |
| 107 | + return ( |
| 108 | + <div className="rmdb-home"> |
| 109 | + {heroImage ? ( |
| 110 | + <div> |
| 111 | + <HeroImage |
| 112 | + image={`${IMAGE_BASE_URL}${BACKDROP_SIZE}${ |
| 113 | + heroImage.backdrop_path |
| 114 | + }`} |
| 115 | + title={heroImage.original_title} |
| 116 | + text={heroImage.overview} |
| 117 | + /> |
| 118 | + <SearchBar callback={this.searchItems} /> |
| 119 | + </div> |
| 120 | + ) : null} |
| 121 | + <div className="rmdb-home-grid"> |
| 122 | + <FourColGrid |
| 123 | + header={searchTerm ? "Search Result" : "Popular Movies"} |
| 124 | + loading={loading} |
| 125 | + > |
| 126 | + {movies.map((element, i) => ( |
| 127 | + <MovieThumb |
| 128 | + key={i} |
| 129 | + clickable={true} |
| 130 | + image={ |
| 131 | + element.poster_path |
| 132 | + ? `${IMAGE_BASE_URL}${POSTER_SIZE}${element.poster_path}` |
| 133 | + : "./images/no_image.jpg" |
| 134 | + } |
| 135 | + movieId={element.id} |
| 136 | + movieName={element.original_title} |
| 137 | + /> |
| 138 | + ))} |
| 139 | + </FourColGrid> |
| 140 | + {loading ? <Spinner /> : null} |
| 141 | + {currentPage <= totalPages && !loading ? ( |
| 142 | + <LoadMoreBtn text="Load More" onClick={this.loadMoreItems} /> |
| 143 | + ) : null} |
| 144 | + </div> |
| 145 | + </div> |
| 146 | + ); |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +export default Home; |
0 commit comments