Skip to content

Instantly share code, notes, and snippets.

@linikerunk
Last active October 26, 2021 23:03
Show Gist options
  • Select an option

  • Save linikerunk/b62c79563114c019182fafc1d97fcbb2 to your computer and use it in GitHub Desktop.

Select an option

Save linikerunk/b62c79563114c019182fafc1d97fcbb2 to your computer and use it in GitHub Desktop.
import React, { useEffect, useState } from 'react';
import Movie from './components/Movie';
const FEATURED_API = 'https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=76f5949e22b36c32ba5161bd79bfd0d8&language=pt-BR';
const SEARCH_API = 'https://api.themoviedb.org/3/search/movie?&api_key=76f5949e22b36c32ba5161bd79bfd0d8&language=pt-BR&query=';
function App() {
const [movies, setMovies] = useState([]);
const [searchTerm, setSearchTerm] = useState("");
useEffect(() => {
getMovies(FEATURED_API);
}, []);
const getMovies = (API) => {
fetch(API)
.then(res => res.json())
.then(data => {
setMovies(data.results);
});
}
const handleOnSubmit = (e) => {
e.preventDefault();
if (searchTerm && searchTerm !== '') {
getMovies(SEARCH_API + searchTerm)
setSearchTerm('');
}
};
const handleOnChange = (e) => {
setSearchTerm(e.target.value);
}
return (
<>
<header>
<form onSubmit={handleOnSubmit}>
<input
className="search"
type="text"
placeholder="Pesquisar"
value={searchTerm}
onChange={handleOnChange}
/>
</form>
</header>
<div className="movie-container">
{ movies.length > 0 &&
movies.map((movie) => <Movie
key={movie.id}
{...movie}
/>)
}
</div>
</>)
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment