-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
155 lines (136 loc) · 4.94 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const autoCompleteConfig = {
renderOption(movie) {
const imgSrc = movie.Poster === 'N/A' ? '' : movie.Poster;
return `
<img src="${imgSrc}" />
${movie.Title} (${movie.Year})
`;
},
inputValue(movie) {
return movie.Title;
},
async fetchData(searchTerm) {
const response = await axios.get('http://www.omdbapi.com/', {
params: {
apikey: "d9835cc5",
s: searchTerm
}
});
if (response.data.Error) {
return [];
}
return response.data.Search;
}
};
createAutoComplete ({
...autoCompleteConfig,
root: document.querySelector('#left-autocomplete'),
onOptionSelect(movie) {
document.querySelector('.tutorial').classList.add('is-hidden');
onMovieSelect(movie, document.querySelector('#left-summary'), 'left');
}
});
createAutoComplete ({
...autoCompleteConfig,
root: document.querySelector('#right-autocomplete'),
onOptionSelect(movie) {
document.querySelector('.tutorial').classList.add('is-hidden');
onMovieSelect(movie, document.querySelector('#right-summary'), 'right');
}
});
let leftMovie;
let rightMovie;
const onMovieSelect = async (movie, summaryElement, side) => {
const response = await axios.get('http://www.omdbapi.com/', {
params: {
apikey: "d9835cc5",
i: movie.imdbID
}
});
summaryElement.innerHTML = movieTemplate(response.data);
if (side === 'left') {
leftMovie = response.data;
} else {
rightMovie = response.data;
}
if (leftMovie && rightMovie) {
runComparison();
}
};
const runComparison = () => {
const leftSideStats = document.querySelectorAll('#left-summary .notification');
const rightSideStats = document.querySelectorAll('#right-summary .notification');
leftSideStats.forEach((leftStat, index) => {
const rightStat = rightSideStats[index];
const leftSideValue = leftStat.dataset.value;
const rightSideValue = rightStat.dataset.value;
if (rightSideValue > leftSideValue) {
leftStat.classList.remove('is-primary');
leftStat.classList.add('is-warning');
} else {
rightStat.classList.remove('is-active');
rightStat.classList.add('is-warning');
}
});
};
const movieTemplate = movieDetail => {
const dollars = parseInt(movieDetail.BoxOffice.replace(/\$/g, '').replace(/,/g, '')); //parseint converte string em numeros inteiros
const metascore = parseInt(movieDetail.Metascore);
const imdbScore = parseFloat(movieDetail.imdbRating); //parsefloat converte string em números quebrados (com decimais)
const imdbVotes = parseInt(movieDetail.imdbVotes.replace(/,/g, ''));
// metodo mais recente e eficiente usando reduce
const awards = movieDetail.Awards.split(' ').reduce((prev, word) => {
const value = parseInt(word);
if (isNaN(value)) {
return prev;
} else {
return prev + value;
}
}, 0);
// metodo mais antigo usando forEach
//let count = 0;
//const awards = movieDetail.Awards.split(' ').forEach((word) => { //word armazena as strings durante o loop
// const value = parseInt(word);
// if(isNaN(value)) {
// return;
// } else {
// count = count + value;
// }
//}); //extrai os numeros de uma string e soma eles, salvando em "count"
return `
<article class="media">
<figure class="media-left">
<p class="image">
<img src="${movieDetail.Poster}" />
</p>
</figure>
<div class="media-content">
<div class="content">
<h1>${movieDetail.Title}</h1>
<h4>${movieDetail.Genre}</h4>
<p>${movieDetail.Plot}</p>
</div>
</div>
</article>
<article data-value=${awards} class="notification is-primary">
<p class="title">${movieDetail.Awards}</p>
<p class="subtitle">Awards</p>
</article>
<article data-value=${dollars} class="notification is-primary">
<p class="title">${movieDetail.BoxOffice}</p>
<p class="subtitle">Box Office</p>
</article>
<article data-value=${metascore} class="notification is-primary">
<p class="title">${movieDetail.Metascore}</p>
<p class="subtitle">Metascore</p>
</article>
<article data-value=${imdbScore} class="notification is-primary">
<p class="title">${movieDetail.imdbRating}</p>
<p class="subtitle">IMDB Rating</p>
</article>
<article data-value=${imdbVotes} class="notification is-primary">
<p class="title">${movieDetail.imdbVotes}</p>
<p class="subtitle">IMDB Votes</p>
</article>
`;
};