From e457038362da55cdc547414230a9436b9415a50b Mon Sep 17 00:00:00 2001 From: shivansh chourasia Date: Thu, 27 Jun 2024 16:16:20 +0530 Subject: [PATCH] Fetched movie data from the OMDB API --- frontend/src/pages/Movies/index.js | 94 ++++++++++++++++++++++++------ 1 file changed, 75 insertions(+), 19 deletions(-) diff --git a/frontend/src/pages/Movies/index.js b/frontend/src/pages/Movies/index.js index 6083f71c1..8ac64e390 100644 --- a/frontend/src/pages/Movies/index.js +++ b/frontend/src/pages/Movies/index.js @@ -3,26 +3,52 @@ import { connect } from "react-redux"; import _ from "lodash"; import { search, categorize, filterRating } from "../../utils"; -import { MoviesTable, Pagination } from "../../components"; +import { Pagination } from "../../components"; import { Input, Loading, ListGroup } from "../../components/common"; -import { getMovies } from "../../actions/moviesAction"; import { getGenres } from "../../actions/genreAction"; class Movies extends Component { state = { genres: [], + movies: [], pageSize: 12, currentPage: 1, currentGenre: "All", searchFilter: "", rating: 0, + loading: true, + totalResults: 0, }; componentDidMount() { - this.props.getMovies(); + this.fetchMovies(); this.props.getGenres(); } + + + fetchMovies = async (page = 1) => { + const apiKey = '5e221987'; + const url = `http://www.omdbapi.com/?s=all&apikey=${apiKey}`; + this.setState({ loading: true }); + + try { + const response = await fetch(url); + const data = await response.json(); + if (data.Response === "True") { + this.setState((prevState) => ({ + movies: [...prevState.movies, ...data.Search], + loading: false, + totalResults: parseInt(data.totalResults, 10), + })); + } else { + this.setState({ loading: false }); + } + } catch (error) { + console.error("Error fetching movies:", error); + this.setState({ loading: false }); + } + }; handleChange = (name, value) => { this.setState({ [name]: value, currentPage: 1 }); @@ -32,6 +58,11 @@ class Movies extends Component { this.setState({ currentPage: page }); }; + handleLoadMore = () => { + const nextPage = Math.ceil(this.state.movies.length / 10) + 1; + this.fetchMovies(nextPage); + }; + render() { const { currentGenre, @@ -39,11 +70,14 @@ class Movies extends Component { searchFilter, pageSize, rating, + loading, + movies, + totalResults, } = this.state; - const { movies, genres, loggedIn } = this.props; + const { genres, loggedIn } = this.props; - if (_.isEmpty(movies)) { + if (loading && _.isEmpty(movies)) { return (
@@ -51,13 +85,24 @@ class Movies extends Component { ); } + if (_.isEmpty(movies) && !loading) { + return ( +
+

No movies found.

+
+ ); + } + let filteredMovies = []; /* Checking for searched item if nothing searched it will just set it to allMovies*/ - filteredMovies = search(movies, searchFilter, "title"); + filteredMovies = search(movies, searchFilter, "Title"); filteredMovies = categorize(filteredMovies, currentGenre); filteredMovies = filterRating(filteredMovies, rating); + const startIndex = (currentPage - 1) * pageSize; + const moviesToDisplay = filteredMovies.slice(startIndex, startIndex + pageSize); + return (
@@ -81,8 +126,6 @@ class Movies extends Component { type="number" iconClass="fas fa-star" /> - {/* { loggedIn && Add Movie } */} - {/* this.handleChange('rating', val)}/> */}
@@ -99,16 +142,31 @@ class Movies extends Component { movies found.

- {!!filteredMovies ? ( - - ) : ( -

No Movies

+
+ {moviesToDisplay.map((movie) => ( +
+
+ {movie.Title} +
+
{movie.Title}
+

Year: {movie.Year}

+
+
+
+ ))} +
+ + {movies.length < totalResults && ( +
+ +
)} -
{ return { - movies: state.movie.movies, genres: state.genre.genres, loggedIn: state.auth.loggedIn, }; @@ -134,7 +191,6 @@ const mapStateToProps = (state) => { const mapDispatchToProps = (dispatch) => { return { - getMovies: () => dispatch(getMovies()), getGenres: () => dispatch(getGenres()), }; };