-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
153 lines (135 loc) · 4.35 KB
/
app.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
/*
API params
s --> for index search (list) --> pass movie name
i --> for show search (lookup) --> pass movie id
*/
const configAutoComplete = {
renderOption(movie) {
// if poster of the movie is unavailable i.e "N/A",
// setting it equal to an empty string to avoid errors
const imgSrc = movie.Poster === "N/A" ? "" : movie.Poster;
return `
<img src="${imgSrc}" />
${movie.Title} (${movie.Year})
`;
},
inputValue(movie) {
return movie.Title;
},
// helper function: for index search or to get partial data of movies
async fetchData(searchQuery) {
const response = await axios.get("//www.omdbapi.com/", {
params: {
apikey: "96f77f16",
s: searchQuery,
},
});
// returning an empty array if the response consists of any error
if (response.data.Error) {
return [];
}
// returning the array of the movies object
return response.data.Search;
},
};
createAutoComplete({
...configAutoComplete,
root: document.querySelector("#left-autocomplete"),
onOptionSelect(movie) {
document.querySelector(".tutorial").classList.add("is-hidden");
onMovieSelect(movie, document.querySelector("#left-summary"), "left");
},
});
createAutoComplete({
...configAutoComplete,
root: document.querySelector("#right-autocomplete"),
onOptionSelect(movie) {
document.querySelector(".tutorial").classList.add("is-hidden");
onMovieSelect(movie, document.querySelector("#right-summary"), "right");
},
});
let leftMovie, rightMovie;
const onMovieSelect = async (movie, summaryTarget, side) => {
const response = await axios.get("//www.omdbapi.com/", {
params: {
apikey: "96f77f16",
i: movie.imdbID,
},
});
summaryTarget.innerHTML = movieTemplate(response.data);
if (side === "left") leftMovie = response.data;
else rightMovie = response.data;
if (leftMovie && rightMovie) runComparision();
};
const runComparision = () => {
const leftSideStats = document.querySelectorAll(
"#left-summary .notification"
);
const rightSideStats = document.querySelectorAll(
"#right-summary .notification"
);
leftSideStats.forEach((leftStat, index) => {
const rightStat = rightSideStats[index];
const leftSideValue = parseInt(leftStat.dataset.value);
const rightSideValue = parseInt(rightStat.dataset.value);
if (rightSideValue > leftSideValue) {
leftStat.classList.remove("is-primary");
leftStat.classList.add("is-warning");
} else {
rightStat.classList.remove("is-primary");
rightStat.classList.add("is-warning");
}
});
};
const movieTemplate = (movieDetail) => {
const dollars = parseInt(
movieDetail.BoxOffice.replace(/\$/g, "").replace(/,/g, "")
);
const metascore = parseInt(movieDetail.Metascore);
const imdbRating = parseFloat(movieDetail.imdbRating);
const imdbVotes = parseInt(movieDetail.imdbVotes.replace(/,/g, ""));
const awards = movieDetail.Awards.split(" ").reduce((count, word) => {
const value = parseInt(word);
if (isNaN(value)) {
return count;
} else {
return count + 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="${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="${imdbRating}" 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>
`;
};