-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
152 lines (122 loc) · 3.68 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
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('https://www.omdbapi.com/', {
params: {
apikey: 'd9835cc5',
s: searchTerm
}
});
if (response.data.Error) {
return [];
}
return response.data.Search;
}
}; //EOF autoCompleteConfig
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('https://www.omdbapi.com/', {
params: {
apikey: 'd9835cc5',
t: movie.Title
}
});
summaryElement.innerHTML = movieTemplate(response.data);
if (side === 'left') {
leftMovie = response.data;
} else {
rightMovie = response.data;
}
if (leftMovie && rightMovie) {
let leftActors = leftMovie.Actors;
let rightActors = rightMovie.Actors;
//splits data and adds to array, be careful to whitespaces
let lefty = leftActors.split(', ');
let righty = rightActors.split(', ');
runComparison(lefty, righty);
}
}; //EOF onMovieSelect
const runComparison = (lefty, righty) => {
//searches the same actors
var intersections = lefty.filter(e => righty.indexOf(e) !== -1);
//add same actors
document.querySelector('#similar-cast').innerHTML =
`
<article class="notification is-info">
<p class="title">${ intersections.length ? intersections : 'Not have smilar cast' }</p>
<p class="subtitle">Smilar Cast</p>
</article>
`
const leftSideStats = document.querySelectorAll(
'#left-summary .notification'
);
const rightSideStats = document.querySelectorAll(
'#right-summary .notification'
);
//for higher IMDB point changes color
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-primary');
rightStat.classList.add('is-warning');
}
});
};
const movieTemplate = movieDetail => {
const imdbRating = parseFloat(movieDetail.imdbRating);
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=${imdbRating} class="notification is-primary">
<p class="title">${movieDetail.imdbRating}</p>
<p class="subtitle">IMDB Rating</p>
</article>
<article class="notification is-primary">
<p class="title">${movieDetail.Actors}</p>
<p class="subtitle">Cast</p>
</article>
`;
};