-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
138 lines (121 loc) · 3.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { generateAutoComplete } from './autocomplete.js';
import { fetchMovie } from './fetchMovie.js';
// DOM elements
const leftAutoComplete = document.querySelector('.left-autocomplete');
const rightAutoComplete = document.querySelector('.right-autocomplete');
const tutorial = document.querySelector('.info-section');
const leftMovieSummary = document.querySelector('.left-movie-summary');
const rightMovieSummary = document.querySelector('.right-movie-summary');
const autoCompletConfig = {
renderOption(movie) {
return `<img src=${movie.Poster === 'N/A'
? 'https://www.reelviews.net/resources/img/default_poster.jpg'
: movie.Poster
} alt=${movie.Title}/><p class="movie-name">${movie.Title}</p>`;
},
searchFunction(...args) {
return fetchMovie(...args)
},
onInputSelect(movie) {
return movie.Title
},
optionsLoop: "Search"
}
generateAutoComplete({
root: leftAutoComplete,
onOptionSelect(movie) {
onMovieSelect(movie, leftMovieSummary, 'left')
},
...autoCompletConfig
});
generateAutoComplete({
root: rightAutoComplete,
onOptionSelect(movie) {
onMovieSelect(movie, rightMovieSummary, 'right')
},
...autoCompletConfig
});
const renderMovieDetails = (movieData) => {
tutorial.style.display = "none";
const dollarValue = Number(movieData.BoxOffice.replace(/[^0-9.-]+/g, ""));
const metaScoreValue = Number(movieData.Metascore);
const imdbRating = Number(movieData.imdbRating);
// For now, for awards. We are going to count the numbers in the Award string.
// E.G: Nominated for 7 Oscars. 21 wins & 43 nominations total
// Would lead to 21+7+43 = 71 count in awards.
// const awardCount = movieData.Awards.split(" ").reduce((prev, curr) => {
// const parsedValue = parseInt(curr, 10);
// if (Number.isNaN(parsedValue)) {
// return prev;
// } else {
// return prev + parsedValue
// }
// }, 0);
const awardCount = movieData.Awards.match(/\d+/g).reduce((acc, curr) => { return Number(acc) + Number(curr) }, 0)
return `<article class="media mb-4">
<figure class="media-left">
<p class="image">
<img src=${movieData.Poster} alt=${movieData.Title} />
</p>
</figure>
<div class="media-content">
<div class="content">
<div>
<h1>${movieData.Title}</h1>
<h4>${movieData.Genre}</h4>
<p>${movieData.Plot}</p>
</div>
</div>
</div>
</article>
<div class="tile is-ancestor">
<div class="is-12 tile is-vertical is-parent">
<div class="tile is-child box" data-value=${awardCount}>
<p class="subtitle">Awards:</p>
<p class="title">${movieData.Awards}</p>
</div>
<div class="tile is-child box" data-value=${imdbRating}>
<p class="subtitle">IMDB Rating:</p>
<p class="title">
${movieData.imdbRating}/10
</p>
</div>
<div class="tile is-child box" data-value=${metaScoreValue}>
<p class="subtitle"> MetaCritic Score: </p>
<p class="title">
${movieData.Metascore}/100
</p>
</div>
<div class="tile is-child box" data-value=${dollarValue}>
<p class="subtitle"> Box Office Collection:</p>
<p class="title">${movieData.BoxOffice}</p>
</div>
</div>
</div>`
};
let rightData, leftData;
export const onMovieSelect = async (movie, summaryNode, rightOrLeft) => {
const movieData = await fetchMovie(movie.imdbID, "i");
summaryNode.innerHTML = renderMovieDetails(movieData)
if (rightOrLeft === 'left') {
leftData = movieData
} else {
rightData = movieData;
}
if (rightData && leftData) startCompare()
}
export const startCompare = () => {
const leftSideElements = leftMovieSummary.querySelectorAll('.box');
const rightSideElements = rightMovieSummary.querySelectorAll('.box');
leftSideElements.forEach((leftElement, idx) => {
let rightElement = rightSideElements[idx];
compareAndSet(leftElement, rightElement)
})
}
function compareAndSet(leftElement, rightElement) {
if (+leftElement.dataset.value > +rightElement.dataset.value) {
leftElement.classList.add('has-background-success')
} else {
rightElement.classList.add('has-background-success')
}
}