-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
143 lines (131 loc) · 3.9 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
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 res = await axios.get("https://www.omdbapi.com/", {
params: {
apikey: "f10e693e",
s: searchTerm
}
});
if (res.data.Error) {
return [];
}
return res.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, summaryEl, side) => {
const res = await axios.get("https://www.omdbapi.com/", {
params: {
apikey: "f10e693e",
t: movie.Title
}
});
summaryEl.innerHTML = movieTemplate(res.data);
if (side === "left") {
leftMovie = res.data;
} else {
rightMovie = res.data;
}
if (leftMovie && rightMovie) {
runComparsion();
}
};
const runComparsion = () => {
const leftSideStats = document.querySelectorAll(
"#left-summary .notification"
);
const rightSideStats = document.querySelectorAll(
"#right-summary .notification"
);
leftSideStats.forEach((leftStat, i) => {
const rightStat = rightSideStats[i];
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 boxOffice = parseInt(
movieDetail.BoxOffice.replace(/\$/g, "").replace(/,/g, "")
);
const metascore = parseInt(movieDetail.Metascore);
const imdbRating = parseFloat(movieDetail.imdbRating);
const imdbVotes = 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=${boxOffice} 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>
`;
};