|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <title>TV show search</title> |
| 5 | + <style> |
| 6 | + #actors { |
| 7 | + display: flex; |
| 8 | + } |
| 9 | + </style> |
| 10 | + </head> |
| 11 | + <body> |
| 12 | + <input id="query" type="text" /> |
| 13 | + <button id="search">Search Show</button> |
| 14 | + <div id="poster"></div> |
| 15 | + <div id="actors"></div> |
| 16 | + |
| 17 | + <script> |
| 18 | + document.getElementById('search').addEventListener('click', () => { |
| 19 | + const inputText = document.getElementById('query').value; |
| 20 | + apiShowSearch(inputText); |
| 21 | + }); |
| 22 | + |
| 23 | + function apiShowSearch(searchQuery) { |
| 24 | + const xmlReq = new XMLHttpRequest(); |
| 25 | + |
| 26 | + xmlReq.addEventListener('load', event => { |
| 27 | + const response = JSON.parse(event.currentTarget.response); |
| 28 | + displayShowPoster(response); |
| 29 | + getActors(response[0].show.id); |
| 30 | + }); |
| 31 | + |
| 32 | + xmlReq.open('GET', `http://api.tvmaze.com/search/shows?q=${searchQuery}`, true); |
| 33 | + xmlReq.send(); |
| 34 | + } |
| 35 | + |
| 36 | + function displayShowPoster(showResultsArr) { |
| 37 | + const topResult = showResultsArr[0].show; |
| 38 | + const posterDiv = document.getElementById('poster'); |
| 39 | + posterDiv.innerHTML = ''; |
| 40 | + |
| 41 | + const imageEl = document.createElement('img'); |
| 42 | + imageEl.src = topResult.image.original; |
| 43 | + imageEl.width = '200'; |
| 44 | + posterDiv.appendChild(imageEl); |
| 45 | + } |
| 46 | + |
| 47 | + function getActors(showId) { |
| 48 | + const xmlReq = new XMLHttpRequest(); |
| 49 | + |
| 50 | + xmlReq.addEventListener('load', event => { |
| 51 | + const response = JSON.parse(event.currentTarget.response); |
| 52 | + displayActorHeadshots(response); |
| 53 | + }); |
| 54 | + |
| 55 | + xmlReq.open('GET', `http://api.tvmaze.com/shows/${showId}/cast`, true); |
| 56 | + xmlReq.send(); |
| 57 | + } |
| 58 | + |
| 59 | + function displayActorHeadshots(castData) { |
| 60 | + const actorImagesEl = document.getElementById('actors'); |
| 61 | + actorImagesEl.innerHTML = ''; |
| 62 | + |
| 63 | + for (let castMember of castData) { |
| 64 | + const imageEl = document.createElement('img'); |
| 65 | + imageEl.src = castMember.person.image.original; |
| 66 | + imageEl.width = '100'; |
| 67 | + actorImagesEl.appendChild(imageEl); |
| 68 | + } |
| 69 | + } |
| 70 | + </script> |
| 71 | + </body> |
| 72 | +</html> |
0 commit comments