forked from Kumardinesh1908/Netflix-Clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
220 lines (184 loc) · 9.15 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
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
// Get references to HTML elements
const searchInput = document.getElementById('searchInput');
const searchResults = document.getElementById('searchResults');
const goToWatchlistBtn = document.getElementById('goToWatchlist');
// Event listener to navigate to WatchList page
goToWatchlistBtn.addEventListener('click', () => {
window.location.href = 'watchList/watchlist.html';
});
const scrollDistance = 900;
// Define a function to handle scrolling
function setupScroll(containerClass, previousButtonClass, nextButtonClass) {
const previousButtons = document.querySelectorAll(`.${previousButtonClass}`);
const nextButtons = document.querySelectorAll(`.${nextButtonClass}`);
const containers = document.querySelectorAll(`.${containerClass}`);
containers.forEach((container, index) => {
const previousButton = previousButtons[index];
const nextButton = nextButtons[index];
nextButton.addEventListener('click', () => {
container.scrollBy({
left: scrollDistance,
behavior: 'smooth',
});
});
previousButton.addEventListener('click', () => {
container.scrollBy({
left: -scrollDistance,
behavior: 'smooth',
});
});
});
}
// SetupScroll function called for each section
setupScroll('trending-container', 'trending-previous', 'trending-next');
setupScroll('netflix-container', 'netflix-previous', 'netflix-next');
setupScroll('netflixShows-container', 'netflixShows-previous', 'netflixShows-next');
setupScroll('top-container', 'top-previous', 'top-next');
setupScroll('horror-container', 'horror-previous', 'horror-next');
setupScroll('comedy-container', 'comedy-previous', 'comedy-next');
setupScroll('action-container', 'action-previous', 'action-next');
setupScroll('romantic-container', 'romantic-previous', 'romantic-next');
// TMDB API key
const api_Key = '4626200399b08f9d04b72348e3625f15';
// Function to fetch and display movies or TV shows
function fetchMedia(containerClass, endpoint, mediaType) {
const containers = document.querySelectorAll(`.${containerClass}`);
containers.forEach((container) => {
fetch(`https://api.themoviedb.org/3/${endpoint}&api_key=${api_Key}`)
.then(response => response.json())
.then(data => {
const fetchResults = data.results;
fetchResults.forEach(item => {
const itemElement = document.createElement('div');
const imageUrl = containerClass === 'netflix-container' ? item.poster_path : item.backdrop_path;
itemElement.innerHTML = ` <img src="https://image.tmdb.org/t/p/w500${imageUrl}" alt="${item.title || item.name}"> `;
container.appendChild(itemElement);
itemElement.addEventListener('click', () => {
const media_Type = item.media_type || mediaType
window.location.href = `movie_details/movie_details.html?media=${media_Type}&id=${item.id}`;
});
});
if (containerClass === 'netflix-container') {
const randomIndex = Math.floor(Math.random() * fetchResults.length);
const randomMovie = fetchResults[randomIndex];
const banner = document.getElementById('banner');
const play = document.getElementById('play-button');
const info = document.getElementById('more-info');
const title = document.getElementById('banner-title');
banner.src = `https://image.tmdb.org/t/p/original/${randomMovie.backdrop_path}`;
title.textContent = randomMovie.title || randomMovie.name;
function redirectToMovieDetails() {
const media_Type = randomMovie.media_type || mediaType;
window.location.href = `movie_details/movie_details.html?media=${media_Type}&id=${randomMovie.id}`;
}
play.addEventListener('click', redirectToMovieDetails);
info.addEventListener('click', redirectToMovieDetails);
}
})
.catch(error => {
console.error(error);
});
})
}
// Initial fetch of trending, Netflix, top rated, horror, comedy, action, and romantic on page load
fetchMedia('trending-container', 'trending/all/week?');
fetchMedia('netflix-container', 'discover/tv?with_networks=213', 'tv');
fetchMedia('netflixShows-container', 'discover/tv?', 'tv');
fetchMedia('top-container', 'movie/top_rated?', 'movie');
fetchMedia('horror-container', 'discover/movie?with_genres=27', 'movie');
fetchMedia('comedy-container', 'discover/movie?with_genres=35', 'movie');
fetchMedia('action-container', 'discover/movie?with_genres=28', 'movie');
fetchMedia('romantic-container', 'discover/movie?with_genres=10749', 'movie');
// Retrieve watchlist from local storage or create an empty array if it doesn't exist
const watchlist = JSON.parse(localStorage.getItem('watchlist')) || [];
// Function to handle search input changes
async function handleSearchInput() {
const query = searchInput.value;
if (query.length > 2) {
const results = await fetchSearchResults(query);
if (results.length !== 0) {
searchResults.style.visibility = "visible";
}
displaySearchResults(results);
} else {
searchResults.innerHTML = '';
searchResults.style.visibility = "hidden";
}
}
// Event listener for search input changes
searchInput.addEventListener('input', handleSearchInput);
// Event listener for Enter key press in search input
searchInput.addEventListener('keyup', async event => {
if (event.key === 'Enter') {
handleSearchInput();
}
});
// Function to fetch search results from TMDB API
async function fetchSearchResults(query) {
const response = await fetch(`https://api.themoviedb.org/3/search/multi?api_key=${api_Key}&query=${query}`);
const data = await response.json();
return data.results || [];
}
// Function to display search results
function displaySearchResults(results) {
searchResults.innerHTML = '';
results.map(movie => {
const shortenedTitle = movie.title || movie.name;
const date = movie.release_date || movie.first_air_date;
let buttonText = "Add to WatchList"; // Set default button text
// Check if the movie is already in WatchList
if (watchlist.find(watchlistItem => watchlistItem.id === movie.id)) {
buttonText = "Go to WatchList"; // Change button text
}
const movieItem = document.createElement('div');
// Create HTML structure for each movie
movieItem.innerHTML = `<div class = "search-item-thumbnail">
<img src ="https://image.tmdb.org/t/p/w500${movie.poster_path}">
</div>
<div class ="search-item-info">
<h3>${shortenedTitle}</h3>
<p>${movie.media_type} <span> ${date}</span></p>
</div>
<button class="watchListBtn" id="${movie.id}">${buttonText}</button>`;
const watchListBtn = movieItem.querySelector('.watchListBtn');
// Add event listener to WatchList button
watchListBtn.addEventListener('click', () => {
if (buttonText === "Add to WatchList") {
addToWatchList(movie);
} else {
window.location.href = 'watchList/watchlist.html'; // Navigate to the WatchList page
}
});
const thumbnail = movieItem.querySelector('.search-item-thumbnail');
const info = movieItem.querySelector('.search-item-info');
// Add event listener to navigate to movie details page
(thumbnail && info).addEventListener('click', () => {
window.location.href = `movie_details/movie_details.html?media=${movie.media_type}&id=${movie.id}`;
});
movieItem.setAttribute('class', 'movie-list');
// Append movie item to search results
searchResults.appendChild(movieItem);
});
}
// Function to add a movie to WatchList
function addToWatchList(movie) {
// Check if the movie is not already in the WatchList list
if (!watchlist.find(watchlistItem => watchlistItem.id === movie.id)) {
watchlist.push(movie);
localStorage.setItem('watchlist', JSON.stringify(watchlist)); // Store in Local Storage
const watchListBtn = document.querySelector(`[id="${movie.id}"]`);
if (watchListBtn) {
watchListBtn.textContent = "Go to WatchList";
watchListBtn.addEventListener('click', () => {
window.location.href = 'watchList/watchlist.html'; // Navigate to the WatchList page
});
}
}
}
// Event listener to close search results when clicking outside
document.addEventListener('click', event => {
if (!searchResults.contains(event.target)) {
searchResults.innerHTML = '';
searchResults.style.visibility = "hidden";
}
});