From 9dba4f5646c56845e7d955519fa608bb4910250d Mon Sep 17 00:00:00 2001
From: Loi Phan
+ {released} • {runtime}
+ {genre}
+ ⭐
+ {imdbRating} IMD rating
+ You rated this movie {watchedUserRating} ⭐
+ {plot}
+ Starring {actors} Directed by {director}
@@ -200,13 +201,132 @@ function Movie({ movie, onSelectMovie }) {
);
}
-function MovieDetails({ selectedId, onCloseMovie }) {
+function MovieDetails({ selectedId, onCloseMovie, onAddWatched, watched }) {
+ const [movie, setMovie] = useState({});
+ const [isLoading, setIsLoading] = useState(false);
+ const [userRating, setUserRating] = useState(0);
+
+ const isWatched = watched.map((movie) => movie.imdbID).includes(selectedId);
+ const watchedUserRating = watched.find(
+ (movie) => movie.imdbID === selectedId
+ )?.userRating;
+
+ const {
+ Title: title,
+ Year: year,
+ Poster: poster,
+ Runtime: runtime,
+ imdbRating,
+ Plot: plot,
+ Released: released,
+ Actors: actors,
+ Director: director,
+ Genre: genre,
+ } = movie;
+
+ const handleAdd = () => {
+ const newWatchedMovie = {
+ imdbID: selectedId,
+ title,
+ year,
+ poster,
+ imdbRating: Number(imdbRating),
+ runtime: Number(runtime.split(" ").at(0)),
+ userRating,
+ };
+
+ onAddWatched(newWatchedMovie);
+ onCloseMovie();
+ };
+
+ useEffect(() => {
+ if (!title) return;
+ document.title = `Movie | ${title}`;
+
+ return () => {
+ document.title = "usePopcorn";
+ };
+ }, [title]);
+
+ useEffect(() => {
+ async function getMovieDetails() {
+ setIsLoading(true);
+ const res = await fetch(BASE_URL + `?apikey=${KEY}&i=${selectedId}`);
+
+ const data = await res.json();
+ setMovie(data);
+ setIsLoading(false);
+ }
+
+ getMovieDetails();
+ }, [selectedId]);
+
+ useEffect(() => {
+ const callback = (e) => {
+ if (e.code === "Escape") {
+ onCloseMovie();
+ }
+ };
+
+ document.addEventListener("keydown", callback);
+
+ return () => {
+ document.removeEventListener("keydown", callback);
+ };
+ }, [onCloseMovie]);
+
return (
+
{title}
+
⭐️ - {avgImdbRating} + {avgImdbRating.toFixed(2)}
🌟 - {avgUserRating} + {avgUserRating.toFixed(2)}
⏳ - {avgRuntime} min + {avgRuntime.toFixed(2)} min
); } -function WatchMovieList({ watched }) { +function WatchMovieList({ watched, onDeleteWatched }) { return (⭐️ @@ -269,6 +393,12 @@ function WatchedMovie({ movie }) { ⏳ {movie.runtime} min
+