-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfav.js
More file actions
144 lines (119 loc) · 4.85 KB
/
Copy pathfav.js
File metadata and controls
144 lines (119 loc) · 4.85 KB
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
let favMovies = JSON.parse(localStorage.getItem('favourites'));
const url = 'https://www.omdbapi.com/?apikey=f26ec8b1 ';
function fetchMovies(){
let body = document.querySelector('body');
let elem = document.createElement('div');
elem.id = 'loading-wrapper';
let p = document.createElement('p');
p.innerHTML = 'Loading...';
elem.appendChild(p);
body.append(elem);
favMovies = JSON.parse(localStorage.getItem('favourites'));
if(favMovies.length == 0){
let elem = document.querySelector('#loading-wrapper p');
elem.innerHTML = 'No movies are added to favourites yet, click here to go back.';
elem.addEventListener('click', () => {
window.close();
})
}else{
if(document.getElementById('loading-wrapper')){
setTimeout(() => {
document.getElementById('loading-wrapper').remove();
}, 1000)
}
document.querySelector('#navigation').addEventListener('click', () => {
window.close();
})
let movieContainer = document.getElementById('movies-container');
movieContainer.innerHTML = '';
favMovies.forEach(movieID => {
getDetails(movieID);
})
}
}
async function getDetails(movieID){
let res = await fetch(url+`&i=${movieID}`, {method: 'GET'});
let data = await res.json();
if(!data.Error){
let movieContainer = document.getElementById('movies-container');
let card = document.createElement("div");
card.classList.add('card', movieID);
movieContainer.appendChild(card);
let poster = document.createElement('div');
poster.classList.add('poster');
card.appendChild(poster);
let favouriteIcon = document.createElement('i');
favouriteIcon.classList.add('fav', 'fa-solid', 'fa-heart');
movieContainer.appendChild(favouriteIcon);
let posterImage = document.createElement('img');
posterImage.src = data.Poster;
posterImage.alt = 'Movie Poster';
poster.appendChild(posterImage);
let details = document.createElement('div');
details.classList.add('details');
card.appendChild(details);
let title = document.createElement('h1');
title.classList.add('title');
title.innerHTML = data.Title;
details.appendChild(title);
let director = document.createElement('h3');
director.classList.add('director');
director.innerHTML = data.Director;
details.appendChild(director);
let ratingDiv = document.createElement('div');
ratingDiv.classList.add('rating');
details.appendChild(ratingDiv);
const rating = data.imdbRating/2;
const wholeRating = Math.floor(rating);
let count = 0;
for(let i=0; i<wholeRating; i++){
let star = document.createElement('i');
star.classList.add('fa-solid', 'fa-star');
ratingDiv.appendChild(star);
count++;
}
if(rating > wholeRating){
let halfStar = document.createElement('i');
halfStar.classList.add('fa-solid', 'fa-star-half-stroke');
ratingDiv.appendChild(halfStar);
count++;
}
while(count < 5){
let emptyStar = document.createElement('i');
emptyStar.classList.add('fa-regular', 'fa-star');
ratingDiv.appendChild(emptyStar);
count++;
}
let ratingSpan = document.createElement('span');
ratingSpan.innerHTML = `${rating.toFixed(1)}/5`;
ratingDiv.appendChild(ratingSpan);
let tagsDiv = document.createElement('div');
tagsDiv.classList.add('tags');
details.appendChild(tagsDiv);
let genres = data.Genre.split(', ');
genres.forEach(elem => {
let genreSpan = document.createElement('span');
genreSpan.innerHTML = elem;
tagsDiv.appendChild(genreSpan);
});
let reviewDiv = document.createElement('div');
reviewDiv.classList.add('review');
reviewDiv.innerHTML = data.Plot.substring(0, 200) + '...';
details.appendChild(reviewDiv);
card.addEventListener('click', () => {
localStorage.setItem('movieDetails', data.imdbID);
const win = window.open('./movie.html', '_blank');
win.focus();
})
favouriteIcon.addEventListener('click', () => {
removeFav(movieID);
fetchMovies();
});
}
}
function removeFav(movieID){
const index = favMovies.indexOf(movieID);
favMovies.splice(index, 1);
localStorage.setItem('favourites', JSON.stringify(favMovies));
}
fetchMovies();