I'm building something in React that makes an fetch call to a Movie API and displays the list of movies for the user.
For some reason this specific fetch call provides really basic results about each movie in the list. But there's a different fetch that provides a lot of details on a specific movie.
Is there a way to make the initial API call (for the list), at the same time as making the other calls (details for each movie), so the user sees more details from the search?
EDIT:
handleSubmitMovie() { //This returns the list of 10 movies.
if (this.state.inputTitle) { //If the user has input something.
fetch(
url + `/` +
// searchParam + this.state.inputTitle +
`?s=${this.state.inputTitle}` +
`&type=${this.state.selectType}` +
`&page=${this.state.currPage}` +
`&apikey=` + apiKey
)
.then(res => {
if(res.ok) { //If API call is successful, return JSON file.
return res.json();
} else { //Else throw an Error.
throw Error(`Request rejected with status ${res.status}`);
}
})
.then(data => { //JSON file is represented by data.
if (data.Response === "True") { //If matching movie(s) were found.
for (let x=0; x < data.Search.length; x++) { //Runs for each record returned.
this.fullMovieSummary(data.Search[x].imdbID); //Calls fullMovieSummary with current record's imdbID.
}
this.setState({
moviesList: data
})
} else { //Else no matching movie(s) were found.
this.setState({
moviesList: '',
movieData: ''
})
}
})
.catch(console.error);
} else { //Else the user has input nothing.
this.setState({
moviesList: '',
movieData: ''
})
}
}
fullMovieSummary(currMovieID) { //This provides full details on a single movie.
fetch(
url + `/` +
`?i=${currMovieID}` +
`&apikey=` + apiKey
)
.then(res => {
if(res.ok) { //If API call is successful, return JSON file.
return res.json();
} else { //Else throw an Error.
throw Error(`Request rejected with status ${res.status}`);
}
})
.then(data => { //JSON file is represented by data.
if (data.Response === "True") { //If matching movie(s) were found.
this.setState(
{
movieData:[...this.state.movieData, data]
}
)
} else { //Else no matching movie(s) were found.
this.setState({
movieData: ''
})
}
})
.catch(console.error);
}