|
1 | | -import { useState } from "react"; |
| 1 | +import { useState, useEffect } from "react"; |
2 | 2 | import { MovieCard } from "../movie-card/movie-card"; |
3 | 3 | import { MovieView } from "../movie-view/movie-view"; |
4 | 4 |
|
5 | 5 | export const MainView = () => { |
6 | | - const [movies, setMovies] = useState([ |
7 | | - { |
8 | | - id: 1, |
9 | | - title: "Fight Club", |
10 | | - description: "An insomniac office worker and a devil-may-care soap maker form an underground fight club that evolves...", |
11 | | - genre: { name: "Drama" }, |
12 | | - director: { name: "David Fincher" }, |
13 | | - image: |
14 | | - "https://upload.wikimedia.org/wikipedia/en/f/fc/Fight_Club_poster.jpg", |
15 | | - }, |
16 | | - { |
17 | | - id: 2, |
18 | | - title: "Schindler's List", |
19 | | - description: "In German-occupied Poland during World War II, industrialist Oskar Schindler gradually becomes concerned...", |
20 | | - genre: { name: "Biography" }, |
21 | | - director: { name: "Steven Spielberg" }, |
22 | | - image: |
23 | | - "https://upload.wikimedia.org/wikipedia/en/3/38/Schindler%27s_List_movie.jpg", |
24 | | - }, |
25 | | - { |
26 | | - id: 3, |
27 | | - title: "The Shawshank Redemption", |
28 | | - description: "Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common...", |
29 | | - genre: { name: "Drama" }, |
30 | | - director: { name: "Frank Darabont" }, |
31 | | - image: |
32 | | - "https://upload.wikimedia.org/wikipedia/en/8/81/ShawshankRedemptionMoviePoster.jpg", |
33 | | - }, |
34 | | - { |
35 | | - id: 4, |
36 | | - title: "The Dark Knight", |
37 | | - description: "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, Batman must accept...", |
38 | | - genre: { name: "Action" }, |
39 | | - director: { name: "Christopher Nolan" }, |
40 | | - image: |
41 | | - "https://upload.wikimedia.org/wikipedia/en/1/1c/The_Dark_Knight_%282008_film%29.jpg", |
42 | | - }, |
43 | | - { |
44 | | - id: 5, |
45 | | - title: "The Matrix", |
46 | | - description: "A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war...", |
47 | | - genre: { name: "Sci-Fi" }, |
48 | | - director: { name: "Lana Wachowski" }, |
49 | | - image: |
50 | | - "https://upload.wikimedia.org/wikipedia/en/c/c1/The_Matrix_Poster.jpg", |
51 | | - } |
52 | | - ]); |
53 | | - |
| 6 | + const [movies, setMovies] = useState([]); |
54 | 7 | const [selectedMovie, setSelectedMovie] = useState(null); |
55 | 8 |
|
| 9 | + useEffect(() => { |
| 10 | + fetch("https://cf-2-movie-api.onrender.com/movies") |
| 11 | + .then((response) => response.json()) |
| 12 | + .then(({ data }) => { |
| 13 | + // Please review the response format of the API here |
| 14 | + // https://cf-2-movie-api.onrender.com/docs/#/Movie/get_movies |
| 15 | + const moviesFromApi = data.map(movie => { |
| 16 | + return { |
| 17 | + id: movie._id, |
| 18 | + Title: movie.Title, |
| 19 | + Description: movie.Description, |
| 20 | + ImagePath: movie.ImagePath, |
| 21 | + Featured: movie.Featured, |
| 22 | + ReleaseYear: movie.ReleaseYear, |
| 23 | + MPA: movie.MPA, |
| 24 | + IMDb: movie.IMDb, |
| 25 | + Genre: movie.Genre, |
| 26 | + Director: movie.Director, |
| 27 | + Actors: movie.Actors, |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + setMovies(moviesFromApi); |
| 32 | + }); |
| 33 | + }, []); |
| 34 | + |
56 | 35 | if (selectedMovie) { |
57 | | - return <MovieView movie={selectedMovie} onBackClick={() => setSelectedMovie(null)} /> |
| 36 | + // Bonus Task 2: Similar Movies |
| 37 | + const similarMovies = movies.filter(movie => { |
| 38 | + const isSimilarMovie = movie.Genre.Name === selectedMovie.Genre.Name; |
| 39 | + const isNotTheSelectedMovie = movie.id !== selectedMovie.id; |
| 40 | + return isNotTheSelectedMovie && isSimilarMovie; |
| 41 | + }); |
| 42 | + |
| 43 | + return ( |
| 44 | + <> |
| 45 | + <MovieView movie={selectedMovie} onBackClick={() => setSelectedMovie(null)} /> |
| 46 | + <hr /> |
| 47 | + <h2>Similar Movies</h2> |
| 48 | + {similarMovies.map(movie => ( |
| 49 | + <MovieCard |
| 50 | + key={movie.id} |
| 51 | + movie={movie} |
| 52 | + onMovieClick={(newSelectedMovie) => { |
| 53 | + setSelectedMovie(newSelectedMovie); |
| 54 | + }} |
| 55 | + /> |
| 56 | + ))} |
| 57 | + </> |
| 58 | + ); |
58 | 59 | } |
59 | 60 |
|
60 | 61 | if (movies.length === 0) { |
|
0 commit comments