-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
191 lines (153 loc) · 5.76 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
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
const APIKey = "93ac677313f294316aab34b8d4ec8917";
let activeImgNo = 0;
let slideMovieNames = [];
let slideMovieOverview = [];
const searchBox = document.querySelector(".searchBox__input");
const searchBtn = document.querySelector(".searchBox__btn");
const resultImg = document.querySelector(".movies");
const imgLeft = document.querySelector(".frontImages__btn--left");
const imgRight = document.querySelector(".frontImages__btn--right");
const placeSlideImages = (data) => {
const slideContainer = document.querySelector(".frontImages");
const backgroundSlide = document.querySelector(".slide");
data.forEach(movie => {
let frontImgUrl = `https://image.tmdb.org/t/p/w342${movie.poster_path}`;
let backImgUrl = `https://image.tmdb.org/t/p/w1280${movie.backdrop_path}`;
slideMovieNames.push(movie.title);
slideMovieOverview.push(movie.overview);
// console.log(movie);
// sessionStorage.setItem("key", "value");
const newImg = document.createElement('img');
newImg.classList.add("frontImages__img");
newImg.src = `${frontImgUrl}`;
slideContainer.appendChild(newImg);
const newBackImg = document.createElement('img');
newBackImg.classList.add("slide__backImg");
newBackImg.src = `${backImgUrl}`;
backgroundSlide.appendChild(newBackImg);
// console.dir(newImg);
});
}
const setSlides = () => {
const slides = document.querySelectorAll(".frontImages__img");
const backImg = document.querySelectorAll(".slide__backImg");
const title = document.querySelector(".info__title");
const overview = document.querySelector(".info__overview");
slides[activeImgNo].classList.add("active");
backImg[activeImgNo].classList.add('active');
title.innerHTML = slideMovieNames[activeImgNo];
overview.innerHTML = slideMovieOverview[activeImgNo];
// console.dir(slides);
}
const getSlideImages = async () => {
let APIUrl = `https://api.themoviedb.org/3/discover/movie?api_key=${APIKey}&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1`;
try {
const res = await axios.get(APIUrl);
// console.log(res.data.results);
placeSlideImages(res.data.results);
setSlides();
}
catch (err) {
console.log(err);
}
}
getSlideImages();
const moveSlideToRight = () => {
const slides = document.querySelectorAll(".frontImages__img");
const backImg = document.querySelectorAll(".slide__backImg");
slides[activeImgNo].classList.remove("active");
backImg[activeImgNo].classList.remove("active");
activeImgNo++;
if (activeImgNo > slides.length - 1) {
activeImgNo = 0;
}
setSlides();
}
const moveSlideToLeft = () => {
const slides = document.querySelectorAll(".frontImages__img");
const backImg = document.querySelectorAll(".slide__backImg");
slides[activeImgNo].classList.remove("active");
backImg[activeImgNo].classList.remove("active");
activeImgNo--;
if (activeImgNo < 0) {
activeImgNo = slides.length - 1;
}
setSlides();
}
const autoChangeSlide = () => {
setInterval(moveSlideToRight, 3000);
}
autoChangeSlide();
const setTrending = (movies, shows) => {
const moviesContainer = document.querySelector(".movies__container");
const showsContainer = document.querySelector(".series__container");
for (let i = 0; i < 10; i++) {
let movImgs = `https://image.tmdb.org/t/p/w342${movies[i].poster_path}`;
let showImgs = `https://image.tmdb.org/t/p/w342${shows[i].poster_path}`;
const newMovAnchor = document.createElement('a');
const newMovDiv = document.createElement('div');
const newMovImg = document.createElement('img');
newMovImg.src = movImgs;
newMovAnchor.href = "./pages/details.html";
newMovAnchor.appendChild(newMovImg);
newMovDiv.classList.add("movies__image");
newMovDiv.id = movies[i].id;
newMovDiv.appendChild(newMovAnchor);
moviesContainer.appendChild(newMovDiv);
const newShowAnchor = document.createElement('a');
const newShowDiv = document.createElement('div');
const newShowImg = document.createElement('img');
newShowImg.src = showImgs;
newShowAnchor.href = "./pages/details.html";
newShowAnchor.appendChild(newShowImg);
newShowDiv.classList.add("series__image");
newShowDiv.id = shows[i].id;
newShowDiv.appendChild(newShowAnchor);
showsContainer.appendChild(newShowDiv);
}
const movImgs = document.querySelectorAll(".movies__container");
const showImgs = document.querySelectorAll(".series__container");
getMovDetails(movImgs);
getShowDetails(showImgs);
}
const getHomeMoviesShows = async () => {
let movieApiUrl = `https://api.themoviedb.org/3/trending/movie/week?api_key=${APIKey}`;
let showsApiUrl = `https://api.themoviedb.org/3/trending/tv/week?api_key=${APIKey}`;
try {
const moviesRes = await axios.get(movieApiUrl);
const showsRes = await axios.get(showsApiUrl);
// console.log(moviesRes.data.results);
// console.log(showsRes.data.results);
setTrending(moviesRes.data.results, showsRes.data.results);
} catch (err) {
console.log(err);
}
}
getHomeMoviesShows();
imgLeft.addEventListener("click", () => {
moveSlideToLeft();
})
imgRight.addEventListener("click", () => {
moveSlideToRight();
})
const getMovDetails = (imgs) => {
imgs.forEach((img) => {
img.addEventListener("click", (evt) => {
// evt.preventDefault();
sessionStorage.setItem("movId", evt.target.parentElement.parentElement.id);
sessionStorage.removeItem("showId");
})
});
}
const getShowDetails = (imgs) => {
imgs.forEach(img => {
img.addEventListener("click", (evt) => {
// evt.preventDefault();
sessionStorage.setItem("showId", evt.target.parentElement.parentElement.id);
sessionStorage.removeItem("movId");
})
});
}
searchBtn.addEventListener("click", (evt) => {
sessionStorage.setItem("movieName", searchBox.value);
})