-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (136 loc) · 4.24 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
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: 'ff52cee5',
//i: 'tt1285016'
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, summarySide, side) => {
const response = await axios.get('http://www.omdbapi.com/', {
params: {
apikey: 'ff52cee5',
i: movie.imdbID
}
});
summarySide.innerHTML = movieTempelate(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 = parseFloat(leftStat.dataset.value);
const rightSideValue = parseFloat(rightStat.dataset.value);
console.log(leftSideValue, rightSideValue);
if (rightSideValue > leftSideValue) {
leftStat.classList.remove('is-primary');
leftStat.classList.add('is-warning');
rightStat.classList.remove('is-warning');
rightStat.classList.add('is-primary');
} else if (rightSideValue === leftSideValue) {
leftStat.classList.remove('is-primary');
rightStat.classList.remove('is-primary');
leftStat.classList.remove('is-warning');
rightStat.classList.remove('is-warning');
} else {
rightStat.classList.remove('is-primary');
rightStat.classList.add('is-warning');
leftStat.classList.remove('is-warning');
leftStat.classList.add('is-primary');
}
});
};
const movieTempelate = (movieDetail) => {
const rottenTom = parseInt(movieDetail.Ratings[1].Value.replace(/\%/g, ''));
const metaScore = parseInt(movieDetail.Metascore);
const imdbRat = parseFloat(movieDetail.imdbRating);
const votes = parseFloat(movieDetail.imdbVotes.replace(/,/g, ''));
const awards = movieDetail.Awards.split(' ').reduce((prev, word) => {
const value = parseInt(word);
if (isNaN(value)) {
return prev;
} else {
return prev + value;
}
}, 0);
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="${rottenTom}" class="notification is-primary">
<p class="title"> ${movieDetail.Ratings[1].Value}</p>
<p class="subtitle">Rotten Tomatoes</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="${imdbRat}" class="notification is-primary">
<p class="title"> ${movieDetail.imdbRating}</p>
<p class="subtitle">IMDB Rating</p>
</article>
<article data-value="${votes}" class="notification is-primary">
<p class="title"> ${movieDetail.imdbVotes}</p>
<p class="subtitle">IMDB Votes</p>
</article>
`;
};