|
| 1 | +const autoCompleteConfig = { |
| 2 | + renderOption(movie) { |
| 3 | + const imgSrc = movie.Poster === 'N/A' ? '' : movie.Poster; |
| 4 | + return ` |
| 5 | + <img src="${imgSrc}" /> |
| 6 | + ${movie.Title} (${movie.Year}) |
| 7 | + `; |
| 8 | + }, |
| 9 | + inputValue(movie) { |
| 10 | + return movie.Title; |
| 11 | + }, |
| 12 | + async fetchData(searchTerm) { |
| 13 | + const response = await axios.get('http://www.omdbapi.com/', { |
| 14 | + params: { |
| 15 | + apikey: apikey, |
| 16 | + s: searchTerm, |
| 17 | + }, |
| 18 | + }) |
| 19 | + |
| 20 | + if (response.data.Error) { |
| 21 | + return []; |
| 22 | + } |
| 23 | + |
| 24 | + return response.data.Search; |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +createAutoComplete({ |
| 29 | + ...autoCompleteConfig, |
| 30 | + root: document.querySelector('#left-autocomplete'), |
| 31 | + onOptionSelect(movie) { |
| 32 | + document.querySelector('.tutorial').classList.add('is-hidden'); |
| 33 | + onMovieSelect(movie, document.querySelector('#left-summary'), 'left'); |
| 34 | + } |
| 35 | +}); |
| 36 | +createAutoComplete({ |
| 37 | + ...autoCompleteConfig, |
| 38 | + root: document.querySelector('#right-autocomplete'), |
| 39 | + onOptionSelect(movie) { |
| 40 | + document.querySelector('.tutorial').classList.add('is-hidden'); |
| 41 | + onMovieSelect(movie, document.querySelector('#right-summary'), 'right'); |
| 42 | + } |
| 43 | +}); |
| 44 | + |
| 45 | +let leftMovie; |
| 46 | +let rightMovie; |
| 47 | +const onMovieSelect = async (movie, summaryElement, side) => { |
| 48 | + const response = await axios.get('http://www.omdbapi.com/', { |
| 49 | + params: { |
| 50 | + apikey: apikey, |
| 51 | + i: movie.imdbID, |
| 52 | + }, |
| 53 | + }) |
| 54 | + |
| 55 | + summaryElement.innerHTML = movieTemplate(response.data); |
| 56 | + |
| 57 | + if (side === 'left') { |
| 58 | + leftMovie = response.data; |
| 59 | + } else { |
| 60 | + rightMovie = response.data; |
| 61 | + } |
| 62 | + |
| 63 | + if (leftMovie && rightMovie) { |
| 64 | + runComparison(); |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +const runComparison = () => { |
| 69 | + const leftSideStats = document.querySelectorAll( |
| 70 | + '#left-summary .notification' |
| 71 | + ); |
| 72 | + const rightSideStats = document.querySelectorAll( |
| 73 | + '#right-summary .notification' |
| 74 | + ); |
| 75 | + |
| 76 | + leftSideStats.forEach((leftStat, index) => { |
| 77 | + const rightStat = rightSideStats[index]; |
| 78 | + |
| 79 | + const leftSideValue = leftStat.dataset.value; |
| 80 | + const rightSideValue = rightStat.dataset.value; |
| 81 | + |
| 82 | + if (rightSideValue > leftSideValue) { |
| 83 | + leftStat.classList.remove('is-primary'); |
| 84 | + leftStat.classList.add('is-warning'); |
| 85 | + } else { |
| 86 | + rightStat.classList.remove('is-primary'); |
| 87 | + rightStat.classList.add('is-warning'); |
| 88 | + } |
| 89 | + }); |
| 90 | +}; |
| 91 | + |
| 92 | +const movieTemplate = movieDetail => { |
| 93 | + const dollars = parseInt( |
| 94 | + movieDetail.BoxOffice.replace(/\$/g, '').replace(/,/g, '') |
| 95 | + ); |
| 96 | + const metascore = parseInt(movieDetail.Metascore); |
| 97 | + const imdbRating = parseFloat(movieDetail.imdbRating); |
| 98 | + const imdbVotes = parseInt(movieDetail.imdbVotes.replace(/,/g, '')); |
| 99 | + const awards = movieDetail.Awards.split(' ').reduce((prev, word) => { |
| 100 | + const value = parseInt(word); |
| 101 | + |
| 102 | + if (isNaN(value)) { |
| 103 | + return prev; |
| 104 | + } else { |
| 105 | + return prev + value; |
| 106 | + } |
| 107 | + }, 0); |
| 108 | + |
| 109 | + return ` |
| 110 | + <article class="media"> |
| 111 | + <figure class="media-left"> |
| 112 | + <p class="image"> |
| 113 | + <img src="${movieDetail.Poster}" /> |
| 114 | + </p> |
| 115 | + </figure> |
| 116 | + <div class="media-content"> |
| 117 | + <div class="content"> |
| 118 | + <h1>${movieDetail.Title}</h1> |
| 119 | + <h4>${movieDetail.Genre}</h4> |
| 120 | + <p>${movieDetail.Plot}</p> |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + </article> |
| 124 | +
|
| 125 | + <article data-value=${awards} class="notification is-primary"> |
| 126 | + <p class="title">${movieDetail.Awards}</p> |
| 127 | + <p class="subtitle">Awards</p> |
| 128 | + </article> |
| 129 | + <article data-value=${dollars} class="notification is-primary"> |
| 130 | + <p class="title">${movieDetail.BoxOffice}</p> |
| 131 | + <p class="subtitle">Box Office</p> |
| 132 | + </article> |
| 133 | + <article data-value=${metascore} class="notification is-primary"> |
| 134 | + <p class="title">${movieDetail.Metascore}</p> |
| 135 | + <p class="subtitle">Metascore</p> |
| 136 | + </article> |
| 137 | + <article data-value=${imdbRating} class="notification is-primary"> |
| 138 | + <p class="title">${movieDetail.imdbRating}</p> |
| 139 | + <p class="subtitle">IMDB Rating</p> |
| 140 | + </article> |
| 141 | + <article data-value=${imdbVotes} class="notification is-primary"> |
| 142 | + <p class="title">${movieDetail.imdbVotes}</p> |
| 143 | + <p class="subtitle">IMDB Votes</p> |
| 144 | + </article> |
| 145 | + `; |
| 146 | +}; |
0 commit comments