forked from halitbatur/movie-project
-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
299 lines (240 loc) · 11.3 KB
/
script.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
'use strict';
const TMDB_BASE_URL = "https://api.themoviedb.org/3";
const PROFILE_BASE_URL = "http://image.tmdb.org/t/p/w185";
const BACKDROP_BASE_URL = "http://image.tmdb.org/t/p/w780";
const CONTAINER = document.querySelector(".containerr");
// Don't touch this function please
const autorun = async() => {
const movies = await fetchMovies();
renderMovies(movies.results);
};
// Don't touch this function please
const constructUrl = (path) => {
return `${TMDB_BASE_URL}/${path}?api_key=${atob(
"NTQyMDAzOTE4NzY5ZGY1MDA4M2ExM2M0MTViYmM2MDI="
)}`;
};
// You may need to add to this function, definitely don't delete it.
const movieDetails = async(movie) => {
const movieRes = await fetchMovie(movie.id);
const movieCredits = await creditDetails(movie.id);
const relatedMoviesDiv = await relatedMovies(movie.id);
// console.log(movie.id);
const renderCreditHtml = renderCredits(movieCredits.cast);
const relatedMoviesHTML = renderRelatedMovies(relatedMoviesDiv.results);
renderMovie(movieRes, relatedMoviesHTML, renderCreditHtml);
// renderRelatedMovies(relatedMoviesDiv.results);
};
// This function is to fetch movies. You may need to add it or change some part in it in order to apply some of the features.
const fetchMovies = async() => {
const url = constructUrl(`movie/now_playing`);
const res = await fetch(url);
// console.log(res.json());
return res.json();
};
// Don't touch this function please. This function is to fetch one movie.
const fetchMovie = async(movieId) => {
const url = constructUrl(`movie/${movieId}`);
const res = await fetch(url);
return res.json();
};
// You'll need to play with this function in order to add features and enhance the style.
const renderMovies = (movies) => {
const lastCA = document.querySelector("#lastCard");
movies.map((movie) => {
const movieDiv = document.getElementById("nowPlaying");
const divCard = document.createElement("div");
divCard.className = "container h-96 bg-gray-300 bg-no-repeat bg-cover bg-center";
divCard.style.backgroundImage = `url(${BACKDROP_BASE_URL+movie.backdrop_path})`;
divCard.innerHTML = `
<div class="triangle-right z-50 absolute"></div>
<div id="triangle-topright" class="ml-1.5 z-50 absolute">
</div>
<div class="overlay">
<div class="movieheader-text mr-40 text-xl">${movie.title}</div>
<p class="moviegener-style text-lg text-white pt-64 ml-5 ">${movieGenre(movie)}</p>
<div class="star-icon flex pt-5 pb-5 ml-5 text-white text-sm">${getRating(movie)}</div>
</div>
`;
movieDiv.appendChild(divCard);
divCard.addEventListener("click", () => {
movieDetails(movie);
lastCA.remove();
});
CONTAINER.appendChild(movieDiv);
});
};
const creditDetails = async(movieId) => {
const url = constructUrl(`movie/${movieId}/credits`);
const res = await fetch(url);
return res.json();
}
const relatedMovies = async(movie) => {
const url = constructUrl(`movie/${movie}/similar`);
const res = await fetch(url);
return res.json();
}
function getRating(movie) {
let rating = "";
for (let i = 0; i < movie.vote_average; i++) {
rating += `<i class="fas fa-star"></i>`;
}
return rating;
}
function movieGenre(movie) {
let genre = "";
for (let i = 0; i < movie.genre_ids.length; i++) {
if (movie.genre_ids[i] === 28) {
genre += "Action ";
} else if (movie.genre_ids[i] === 12) {
genre += "Adventure ";
} else if (movie.genre_ids[i] === 16) {
genre += "Animation ";
} else if (movie.genre_ids[i] === 35) {
genre += "Comedy ";
} else if (movie.genre_ids[i] === 80) {
genre += "Crime ";
} else if (movie.genre_ids[i] === 99) {
genre += "Documentary ";
} else if (movie.genre_ids[i] === 18) {
genre += "Drama ";
} else if (movie.genre_ids[i] === 10751) {
genre += "Family ";
} else if (movie.genre_ids[i] === 14) {
genre += "Fantasy ";
} else if (movie.genre_ids[i] === 36) {
genre += "History ";
} else if (movie.genre_ids[i] === 27) {
genre += "Horror ";
} else if (movie.genre_ids[i] === 10402) {
genre += "Music ";
} else if (movie.genre_ids[i] === 9648) {
genre += "Mystery ";
} else if (movie.genre_ids[i] === 10749) {
genre += "Romance ";
} else if (movie.genre_ids[i] === 878) {
genre += "Science Fiction ";
} else if (movie.genre_ids[i] === 10770) {
genre += "TV Movie ";
} else if (movie.genre_ids[i] === 53) {
genre += "Thriller ";
} else if (movie.genre_ids[i] === 10752) {
genre += "War ";
} else if (movie.genre_ids[i] === 37) {
genre += "Western ";
}
}
return genre;
}
// You'll need to play with this function in order to add features and enhance the style.
const renderMovie = (movie, relatedMoviesHTML, renderCreditHtml) => {
// console.log(movie);
CONTAINER.innerHTML = `
<div class="bg-black w-full absolute z-50 ">
<div class="flex mt-16 ml-10" >
<div class="mainmovie-div bg-gray-300 bg-no-repeat bg-cover bg-center " style="background-Image: url(${BACKDROP_BASE_URL+movie.backdrop_path})" >
<div class="movie-triangle1 z-50 absolute"></div>
<div class="movie-triangle2 z-50 absolute"></div>
</div>
<div class="red-circle w-9 h-9 bg-red-900 ml-36 mt-3 rounded-full "></div>
<div class="line-1 rounded absolute "></div>
<div class="mainmovie-div flex flex-col gap-24 bg-black ml-24 " >
<div class="">
<p class="moviesparaghrap mt-3 text-center text-5xl text-red-900 ">${movie.title}</p>
</div>
<div class="">
<p class="moviesparaghrap2 font-bold text-center text-lg text-white ">${movie.overview}</p>
</div>
<div class="flex">
<p class="moviesparaghrap text-center text-4xl mt-10 text-red-900 ">Ratings: ${getRating(movie)}</p>
<div class="star-icon flex mt-10 ml-5 text-white text-4xl"></div>
</div>
</div>
</div>
${renderCreditHtml}
${relatedMoviesHTML}
`;
};
//we need to call this function after the render movie is finished add this to the same div
const renderCredits = (credits) => {
return `
<div class="mt-32">
<p class="moviesparaghrap text-4xl text-white ml-10">Movie Team</p>
<div class="grid grid-cols-3 gap-10 ml-10 mr-10 mt-12">
<div class="container h-96 bg-gray-300 bg-no-repeat bg-cover bg-center" style="background-Image: url(${BACKDROP_BASE_URL+credits[0].profile_path})">
<div class="triangle-right z-50 absolute"></div>
<div id="triangle-topright" class="ml-1.5 z-50 absolute"></div>
<div class="movieoverlay">
<div class="movieheader-text text-4xl">
${credits[0].name}
</div>
<p class="moviegener-style text-lg text-white mt-64 ml-5 ">populararity : ${populararity(credits)}</p> <p class="moviegener-style text-lg text-white mt-64 ml-5 ">${credits[0].name}</p>
</div>
</div>
<div class="container h-96 bg-gray-300 bg-no-repeat bg-cover bg-center"style="background-Image: url(${BACKDROP_BASE_URL+credits[1].profile_path})">
<div id="triangle1-topright" class="ml-96 z-50 absolute"></div>
<div class="movieoverlay">
<div class="movieheader-text text-4xl">
${credits[1].name}
</div>
<p class="moviegener-style text-lg text-white mt-64 ml-5 ">populararity : ${populararity(credits)}</p>
</div>
</div>
<div class="container h-96 bg-gray-300 bg-no-repeat bg-cover bg-center" style="background-Image: url(${BACKDROP_BASE_URL+credits[2].profile_path})">
<div class="triangle-left z-50 absolute"></div>
<div class="movieoverlay">
<div class="movieheader-text text-4xl">
${credits[2].name}
</div>
<p class="moviegener-style text-lg text-white mt-64 ml-5 ">populararity : ${populararity(credits)}</p>
</div>
</div>
</div>
</div>
`
}
function populararity(credits) {
let populararity = 0;
for (let i = 0; i < credits.length; i++) {
populararity += credits[i].popularity;
if (i === credits.length - 1) {
populararity = populararity / credits.length;
populararity = String(populararity).slice(0, 4);
}
}
return populararity;
}
const renderRelatedMovies = (movie) => {
// create as js element and then append each card with event listener
return `
<div class="mt-32">
<p class="moviesparaghrap text-4xl text-white ml-10">Related Movies</p>
<div class="grid grid-cols-3 gap-10 ml-10 mr-10 mt-12">
<div class="container h-96 bg-gray-300 bg-no-repeat bg-cover bg-center"style="background-Image: url(${BACKDROP_BASE_URL+movie[0].backdrop_path})" onClick="movieDetails(movie[0])">
<div class="triangle-right z-50 absolute"></div>
<div id="triangle-topright" class="ml-1.5 z-50 absolute"></div>
<div class="movieoverlay">
<div class="movieheader-text text-4xl"></div>
<p class="moviegener-style text-lg text-white mt-64 ml-5 ">${movie[0].title}</p>
</div>
</div>
<div class="container h-96 bg-gray-300 bg-no-repeat bg-cover bg-center" style="background-Image: url(${BACKDROP_BASE_URL+movie[1].backdrop_path})">
<div id="triangle1-topright" class="ml-96 z-50 absolute"></div>
<div class="movieoverlay">
<div class="movieheader-text text-4xl"></div>
<p class="moviegener-style text-lg text-white mt-64 ml-5 ">${movie[1].title}</p>
</div>
</div>
<div class="container h-96 bg-gray-300 bg-no-repeat bg-cover bg-center" style="background-Image: url(${BACKDROP_BASE_URL+movie[2].backdrop_path})">
<div class="triangle-left z-50 absolute"></div>
<div class="movieoverlay">
<div class="movieheader-text text-4xl"></div>
<p class="moviegener-style text-lg text-white mt-64 ml-5 ">${movie[2].title}</p>
</div>
</div>
</div>
</div>
</div>
`
}
document.addEventListener("DOMContentLoaded", autorun);