-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdisplay-movies.js
146 lines (131 loc) · 6.32 KB
/
display-movies.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
document.addEventListener('DOMContentLoaded', function () {
const apiKey = 'b9777c51aea4a211a9c6f0e839934890';
const accessToken = 'eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJiOTc3N2M1MWFlYTRhMjExYTljNmYwZTgzOTkzNDg5MCIsIm5iZiI6MTcyMjEwMzM3Mi41OTY2NzksInN1YiI6IjY2OTIzYzIyNGVlNGFiYzcyNzVlODg0MSIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.i2AWKgJL01w3QB0-sop6hg0ImVmQbk6SVVnPlc-XBco';
const apiUrl = 'https://api.themoviedb.org/3';
// Fetch and display recently viewed movies
function displayRecentlyViewedMovies(buttons) {
const recentViewedMovies = JSON.parse(localStorage.getItem('recentlyViewedMovies')) || [];
const container = document.querySelector('#recent-viewed-grid');
if (!container) return;
container.innerHTML = '';
if (recentViewedMovies.length === 0) {
container.innerHTML = '<div class="no-movies-message">No Movies watched recently</div>';
return;
}
recentViewedMovies.forEach(movie => {
const imageUrl = movie.poster_path
? `https://image.tmdb.org/t/p/w500/${movie.poster_path}`
: 'https://via.placeholder.com/200x300';
const title = movie.title;
const overview = movie.overview ? movie.overview.substring(0, 150) + '...' : 'No overview available';
const rating = movie.vote_average;
const movieElement = document.createElement('div');
movieElement.classList.add('movie-item');
let clickAction = '';
if (buttons) {
movieElement.innerHTML = `
<div class="movie-card">
<div class="movie-card-inner">
<div class="movie-card-front">
<img src="${imageUrl}" alt="${title}">
</div>
<div class="movie-card-back">
<h3>${title}</h3>
<p>${overview}</p>
<p>Rating: ${rating}/10</p>
<a href="play.html?id=${movie.id}" class="button">Play Now</a>
</div>
</div>
</div>
`;
} else {
movieElement.innerHTML = `
<div class="movie-card" onclick="location.href='play.html?id=${movie.id}'">
<div class="movie-card-inner">
<div class="movie-card-front">
<img src="${imageUrl}" alt="${title}">
</div>
<div class="movie-card-back">
<h3>${title}</h3>
<p>${overview}</p>
<p>Rating: ${rating}/10</p>
</div>
</div>
</div>
`;
}
container.appendChild(movieElement);
});
}
// Fetch and display now playing movies
function fetchMovies() {
fetch(`${apiUrl}/movie/latest?api_key=${apiKey}`, {
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
})
.then(response => response.json())
.then(data => {
const recentMovies = data.results.slice(0, 10); // Limit to 10 movies
displayMovies(recentMovies, '#movie-grid');
})
.catch(error => {
console.error('Error fetching recent movies:', error);
});
}
// Function to display movies in a grid
function displayMovies(movies, containerSelector) {
const container = document.querySelector(containerSelector);
if (!container) return;
container.innerHTML = '';
const showDetailsButton = localStorage.getItem('viewDetails') === 'true';
movies.forEach(movie => {
const imageUrl = movie.poster_path
? `https://image.tmdb.org/t/p/w500/${movie.poster_path}`
: 'https://via.placeholder.com/200x300';
const title = movie.title;
const overview = movie.overview ? movie.overview.substring(0, 150) + '...' : 'No overview available';
const rating = movie.vote_average;
const movieElement = document.createElement('div');
movieElement.classList.add('movie-item');
if (showDetailsButton) {
movieElement.innerHTML = `
<div class="movie-card">
<div class="movie-card-inner">
<div class="movie-card-front">
<img src="${imageUrl}" alt="${title}">
</div>
<div class="movie-card-back">
<h3>${title}</h3>
<p>${overview}</p>
<p>Rating: ${rating}/10</p>
<a href="movie.html?id=${movie.id}" class="button">View Details</a>
</div>
</div>
</div>
`;
} else {
movieElement.innerHTML = `
<div class="movie-card" onclick="location.href='movie.html?id=${movie.id}'">
<div class="movie-card-inner">
<div class="movie-card-front">
<img src="${imageUrl}" alt="${title}">
</div>
<div class="movie-card-back">
<h3>${title}</h3>
<p>${overview}</p>
<p>Rating: ${rating}/10</p>
</div>
</div>
</div>
`;
}
container.appendChild(movieElement);
});
}
// Call functions
const buttons = localStorage.getItem('buttonVisibility') === 'true'; // Check button visibility setting
displayRecentlyViewedMovies(buttons);
fetchMovies();
});