-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
161 lines (132 loc) · 4.73 KB
/
main.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
// show menu
const nav = document.getElementById("nav"),
headerMenu = document.getElementById("header-menu"),
navClose = document.getElementById("nav_close");
// show menu
if (headerMenu) {
headerMenu.addEventListener("click", () => {
nav.classList.add("show-menu");
});
}
// Menu hidden
if (navClose) {
navClose.addEventListener("click", () => {
nav.classList.remove("show-menu");
});
}
// scroll page
window.addEventListener("scroll", function () {
var header = document.querySelector(".header");
if (window.scrollY > 50) {
header.classList.add("scrolled");
} else {
header.classList.remove("scrolled");
}
});
/* --------------- movies Container part -------------------- */
fetchData();
document.querySelector(`.search`).addEventListener(`keypress`, function (e) {
if (e.key == `Enter`) {
const index = e.target.value.trim();
if (index) {
fetchSearch(index);
}
}
});
async function fetchData() {
try {
const response = await fetch(
`https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=3fd2be6f0c70a2a598f084ddfb75487c&page=1'`
);
if (!response.ok) {
throw new Error(`Could not fetch resource`);
}
const data = await response.json();
displayMovies(data.results);
} catch (error) {
console.error(error);
}
}
async function fetchSearch(index) {
try {
const response = await fetch(
`https://api.themoviedb.org/3/search/movie?api_key=3fd2be6f0c70a2a598f084ddfb75487c&query=${encodeURIComponent(
index
)}`
);
if (!response.ok) {
throw new Error(`Could not fetch resource`);
}
const data = await response.json();
displayMovies(data.results);
} catch (error) {
console.error(error);
}
}
function displayMovies(film) {
const container = document.getElementById(`moviesContainer`);
container.innerHTML = "";
const favorites = JSON.parse(localStorage.getItem('favorites')) || [];
for (let i = 0; i < film.length; i++) {
const content = document.createElement(`div`);
content.classList.add(`content`);
const image = document.createElement(`img`);
image.src = film[i].poster_path
? `https://image.tmdb.org/t/p/w1280${film[i].poster_path}`
: `https://dummyimage.com/300x450/ddd/555.png&text=No+Image`; // default image
image.classList.add(`poster`);
content.appendChild(image);
const bottomPart = document.createElement(`div`);
bottomPart.classList.add(`bottomPart`);
content.appendChild(bottomPart);
const title = document.createElement(`p`);
title.textContent = film[i].title;
title.classList.add("title");
bottomPart.appendChild(title);
document.querySelectorAll('.title').forEach(title => {
if (title.textContent.length > 10) {
title.style.fontSize = '16px';
}
if (title.textContent.length > 20) {
title.style.fontSize = '12px';
}
});
const rate = document.createElement(`span`);
rate.textContent = film[i].vote_average;
rate.classList.add(`rate`);
bottomPart.appendChild(rate);
// Add heart icon for adding to favorites
const heartIcon = document.createElement("span");
heartIcon.classList.add("heart-icon", "ri-heart-line");
heartIcon.dataset.movieId = film[i].id;
if (favorites.includes(film[i].id)) {
heartIcon.classList.add("ri-heart-fill");
heartIcon.classList.remove("ri-heart-line");
}
heartIcon.addEventListener("click", function () {
this.classList.toggle("ri-heart-fill");
this.classList.toggle("ri-heart-line");
const movieId = parseInt(this.dataset.movieId);
let favorites = JSON.parse(localStorage.getItem('favorites')) || [];
if (this.classList.contains("ri-heart-fill")) {
favorites.push(movieId);
} else {
favorites = favorites.filter(id => id !== movieId);
}
localStorage.setItem('favorites', JSON.stringify(favorites));
});
bottomPart.appendChild(heartIcon);
// const heartIcon = document.createElement("span");
// heartIcon.classList.add("heart-icon", "ri-heart-line");
// heartIcon.dataset.movieId = film[i].id;
// Create "View Details" Button
const viewDetailsBtn = document.createElement(`button`);
viewDetailsBtn.textContent = "View Details";
viewDetailsBtn.classList.add("viewDetails");
viewDetailsBtn.addEventListener("click", () => {
window.location.href = `detiels.html?movieId=${film[i].id}`;
});
bottomPart.appendChild(viewDetailsBtn);
container.appendChild(content);
}
}