Skip to main content

Show Details Page

Just like we did with movies, we need to create a page to show the details of a TV show. This will be on the tv-details.html page. We will create a function called displayShowDetails() to get the show id from the url and then fetch the show data from the API.

// Display Show Details
async function displayShowDetails() {
const showId = window.location.search.split('=')[1];

const show = await fetchAPIData(`tv/${showId}`);

// Overlay for background image
displayBackgroundImage('tv', show.backdrop_path);

const div = document.createElement('div');

div.innerHTML = `
<div class="details-top">
<div>
${
show.poster_path
? `<img
src="https://image.tmdb.org/t/p/w500${show.poster_path}"
class="card-img-top"
alt="${show.name}"
/>`
: `<img
src="../images/no-image.jpg"
class="card-img-top"
alt="${show.name}"
/>`
}
</div>
<div>
<h2>${show.name}</h2>
<p>
<i class="fas fa-star text-primary"></i>
${show.vote_average.toFixed(1)} / 10
</p>
<p class="text-muted">Last Air Date: ${show.last_air_date}</p>
<p>
${show.overview}
</p>
<h5>Genres</h5>
<ul class="list-group">
${show.genres.map((genre) => `<li>${genre.name}</li>`).join('')}
</ul>
<a href="${
show.homepage
}" target="_blank" class="btn">Visit show Homepage</a>
</div>
</div>
<div class="details-bottom">
<h2>Show Info</h2>
<ul>
<li><span class="text-secondary">Number of Episodes:</span> ${
show.number_of_episodes
}</li>
<li><span class="text-secondary">Last Episode To Air:</span> ${
show.last_episode_to_air.name
}</li>
<li><span class="text-secondary">Status:</span> ${show.status}</li>
</ul>
<h4>Production Companies</h4>
<div class="list-group">
${show.production_companies
.map((company) => `<span>${company.name}</span>`)
.join(', ')}
</div>
</div>
`;

document.querySelector('#show-details').appendChild(div);
}

Call the function in the switch statement:

// Init App
function init() {
switch (global.currentPage) {
// ...
case '/tv-details.html':
displayShowDetails();
break;
// ...
}
}