I have a tricky question in React. I was able to make an API call to get the movies but now I need to make an API call with just the ID of one movie to get the rest of the information.
I'm tempted to make an API call inside the map() function as it will return to me unique information. If I try to make a separate call on its own with the fetchMovieInfo, it will update all the movies with the same information.
I feel the solution is to make another API inside the map() function.
You could see in the comment I was trying to call an API based on a button, but as I said, it updated all the cards with the same information.
Any help is greatly appreciated as I'm almost there...
import { useState } from "react";
const Home = () => {
const key = 'eee0805f';
const baseUrl = 'https://www.omdbapi.com/?apikey=' + key;
// This is the search input
const [message, setMessage] = useState('');
const [searchTerm , setSearchTerm] = useState('');
const [movies, setMovies] = useState(null);
const [movieInfo, setMovieInfo] = useState(null);
const handleChange = event => {
setMessage(event.target.value);
};
const fetchData = async (url) => {
try {
const response = await fetch(url);
const json = await response.json();
const newMovies = json.Search;
setMovies(newMovies);
} catch (error) {
console.log("error", error);
}
};
const fetchMovieInfo = async (url) => {
try {
const response = await fetch(url);
const json = await response.json();
const newInfo = json;
setMovieInfo(newInfo);
} catch (error) {
console.log("error", error);
}
};
const searchMovie = () => {
let url = baseUrl + '&s=' + encodeURIComponent(message);
console.log(url);
fetchData(url);
setSearchTerm(message);
setMessage("");
}
const moreInfo = (e) => {
let url = baseUrl + '&i=' + e.target.getAttribute('data-key');
console.log(url);
fetchMovieInfo(url);
}
// console.log(movies);
console.log(movieInfo);
return (
<div className="home">
<div className="searchContainer">
<input type="text" id="message" name="message" onChange={handleChange} value={message} placeholder="Please Enter Movie" />
<span style={{
color: 'white',
backgroundColor: '#f1356d',
borderRadius: '8px' ,
padding: '2px 8px'
}} onClick={searchMovie}>Search</span>
</div>
<h3>Title Searched: {searchTerm}</h3>
{movies && <div>
{movies.map(movie => (
<div className="movie-preview" key={movie.imdbID} >
<div className="movie-content">
<div className="movie-premier-image-content">
<img className="movie-preview-image" src={movie.Poster} />
</div>
<div>
<h2 className="movie-preview-headline">{ movie.Title }</h2>
<p>{movie.Year}</p>
{/* The problem here is that the information is all the same. */}
{movieInfo && <div>
<p>Director(s): {movieInfo.Director}</p>
<p>Genre: { movieInfo.Genre }</p>
<p>Date released: {movieInfo.Released}</p>
</div>}
</div>
</div>
{/* <div style={{
color: 'white',
backgroundColor: '#f1356d',
borderRadius: '8px' ,
padding: '2px 8px',
height: '24px',
width: '75px'
}} onClick={moreInfo} data-key={movie.imdbID}>More Info</div> */}
</div>
))}
</div>}
</div>
);
}
export default Home;