From db56b620301dbb2c0bcf7d807d9f77e093b5b5cb Mon Sep 17 00:00:00 2001 From: Loi Phan Date: Wed, 11 Oct 2023 17:35:56 +0700 Subject: [PATCH] Update sample code --- sample/use-popcorn/src/App.jsx | 153 ++++++++++++++++---------- sample/use-popcorn/src/StarRating.jsx | 11 ++ sample/use-popcorn/src/index.css | 12 +- sample/use-popcorn/src/main.jsx | 22 +--- 4 files changed, 116 insertions(+), 82 deletions(-) diff --git a/sample/use-popcorn/src/App.jsx b/sample/use-popcorn/src/App.jsx index 05c64de..4143daa 100644 --- a/sample/use-popcorn/src/App.jsx +++ b/sample/use-popcorn/src/App.jsx @@ -1,80 +1,103 @@ +import { useEffect } from "react"; 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); +const KEY = "68ff4a55"; +const BASE_URL = "http://www.omdbapi.com/"; + export default function App() { - const [movies, setMovies] = useState(tempMovieData); - const [watched, setWatched] = useState(tempWatchedData); + const [query, setQuery] = useState(""); + const [movies, setMovies] = useState([]); + const [watched, setWatched] = useState([]); + const [isLoading, setIsLoading] = useState(false); + const [error, setError] = useState(""); + const [selectedId, setSelectedId] = useState(null); + + const handleSelectMovie = (id) => { + setSelectedId((selectedId) => (id === selectedId ? null : id)); + }; + + const handleCloseMovie = () => { + setSelectedId(null); + }; + + useEffect(() => { + const fetchMovies = async () => { + try { + setIsLoading(true); + setError(""); + + const res = await fetch(BASE_URL + `?apikey=${KEY}&s=${query}`); + + if (!res.ok) + throw new Error("Something went wrong with fetching movies"); + + const data = await res.json(); + + if (data.Response === "False") throw new Error("Movie not found!"); + + setMovies(data.Search); + } catch (err) { + setError(err.message); + } finally { + setIsLoading(false); + } + }; + + if (query.length < 3) { + setMovies([]); + setError(""); + return; + } + + fetchMovies(); + }, [query]); return ( <> - +
- + {/* {isLoading ? : } */} + {isLoading && } + {!isLoading && !error && ( + + )} + {error && } - - + {selectedId ? ( + + ) : ( + <> + + + + )}
); } +function Loader() { + return

Loading...

; +} + +function ErrorMessage({ message }) { + return

{message}

; +} + function NavBar({ children }) { return (