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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
| import { useState } from "react";
const tempMovieData = [ { imdbID: "tt1375666", Title: "Inception", Year: "2010", Poster: "https://m.media-amazon.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg", }, { imdbID: "tt0133093", Title: "The Matrix", Year: "1999", Poster: "https://m.media-amazon.com/images/M/MV5BNzQzOTk3OTAtNDQ0Zi00ZTVkLWI0MTEtMDllZjNkYzNjNTc4L2ltYWdlXkEyXkFqcGdeQXVyNjU0OTQ0OTY@._V1_SX300.jpg", }, { imdbID: "tt6751668", Title: "Parasite", Year: "2019", Poster: "https://m.media-amazon.com/images/M/MV5BYWZjMjk3ZTItODQ2ZC00NTY5LWE0ZDYtZTI3MjcwN2Q5NTVkXkEyXkFqcGdeQXVyODk4OTc3MTY@._V1_SX300.jpg", }, ];
const tempWatchedData = [ { imdbID: "tt1375666", Title: "Inception", Year: "2010", Poster: "https://m.media-amazon.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg", runtime: 148, imdbRating: 8.8, userRating: 10, }, { imdbID: "tt0088763", Title: "Back to the Future", Year: "1985", Poster: "https://m.media-amazon.com/images/M/MV5BZmU0M2Y1OGUtZjIxNi00ZjBkLTg1MjgtOWIyNThiZWIwYjRiXkEyXkFqcGdeQXVyMTQxNzMzNDI@._V1_SX300.jpg", runtime: 116, imdbRating: 8.5, userRating: 9, }, ];
const average = (arr) => arr.reduce((acc, cur, i, arr) => acc + cur / arr.length, 0);
export default function App() { const [movies, setMovies] = useState(tempMovieData); const [watched, setWatched] = useState(tempWatchedData); return ( <> <NavBar> <Search /> <NumResults movies={movies} /> </NavBar>
<Main> <Box> <MovieList movies={movies} /> </Box> <Box> <WatchedSummary watched={watched} /> <WatchedMovieList watched={watched} /> </Box> </Main> </> ); }
function NavBar({ children }) { return ( <nav className="nav-bar"> {" "} <Logo /> {children} </nav> ); }
function Logo() { return ( <div className="logo"> <span role="img">🍿</span> <h1>usePopcorn</h1> </div> ); }
function Search() { const [query, setQuery] = useState(""); return ( <input className="search" type="text" placeholder="Search movies..." value={query} onChange={(e) => setQuery(e.target.value)} /> ); }
function NumResults({ movies }) { return ( <p className="num-results"> Found <strong>{movies.length}</strong> results </p> ); }
function Main({ children }) { return <main className="main">{children}</main>; }
function Box({ children }) { const [isOpen, setIsOpen] = useState(true); return ( <div className="box"> <button className="btn-toggle" onClick={() => setIsOpen((open) => !open)}> {isOpen ? "–" : "+"} </button> {isOpen && children} </div> ); }
function MovieList({ movies }) { return ( <ul className="list"> {movies?.map((movie) => ( <Movie movie={movie} key={movie.imdbID} /> ))} </ul> ); }
function Movie({ movie }) { return ( <li key={movie.imdbID}> <img src={movie.Poster} alt={`${movie.Title} poster`} /> <h3>{movie.Title}</h3> <div> <p> <span>🗓</span> <span>{movie.Year}</span> </p> </div> </li> ); }
function WatchedSummary({ watched }) { const avgImdbRating = average(watched.map((movie) => movie.imdbRating)); const avgUserRating = average(watched.map((movie) => movie.userRating)); const avgRuntime = average(watched.map((movie) => movie.runtime)); return ( <div className="summary"> <h2>Movies you watched</h2> <div> <p> <span>#️⃣</span> <span>{watched.length} movies</span> </p> <p> <span>⭐️</span> <span>{avgImdbRating}</span> </p> <p> <span>🌟</span> <span>{avgUserRating}</span> </p> <p> <span>⏳</span> <span>{avgRuntime} min</span> </p> </div> </div> ); }
function WatchedMovieList({ watched }) { return ( <ul className="list"> {watched.map((movie) => ( <WatchedMovie movie={movie} /> ))} </ul> ); }
function WatchedMovie({ movie }) { return ( <li key={movie.imdbID}> <img src={movie.Poster} alt={`${movie.Title} poster`} /> <h3>{movie.Title}</h3> <div> <p> <span>⭐️</span> <span>{movie.imdbRating}</span> </p> <p> <span>🌟</span> <span>{movie.userRating}</span> </p> <p> <span>⏳</span> <span>{movie.runtime} min</span> </p> </div> </li> ); }
|