-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
192 lines (164 loc) · 6.71 KB
/
script.js
File metadata and controls
192 lines (164 loc) · 6.71 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
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
const key = '6774c6aa';
var searchInput = document.getElementById('Input');
var displaySearchList = document.getElementsByClassName('fav-container');
fetch('http://www.omdbapi.com/?i=tt3896198&apikey=6774c6aa')
.then(res => res.json())
.then(data => console.log(data));
// Upon keypress - function findMovies is initiated
searchInput.addEventListener('input', findMovies);
async function singleMovie() {
// Finding ID of the movie from the URL
var urlQueryParams = new URLSearchParams(window.location.search);
var id = urlQueryParams.get('id')
console.log(id);
const url = `https://www.omdbapi.com/?i=${id}&apikey=${key}`
const res = await fetch(`${url}`);
const data = await res.json();
console.log(data);
console.log(url);
// Making the output html by string interpolition
var output = `
<div class="movie-poster">
<img src=${data.Poster} alt="Movie Poster">
</div>
<div class="movie-details">
<div class="details-header">
<div class="dh-ls">
<h2>${data.Title}</h2>
</div>
<div class="dh-rs">
<i class="fa-solid fa-bookmark" onClick=addTofavorites('${id}') style="cursor: pointer;"></i>
</div>
</div>
<span class="italics-text"><i>${data.Year} • ${data.Country} • Rating - <span
style="font-size: 18px; font-weight: 600;">${data.imdbRating}</span>/10 </i></span>
<ul class="details-ul">
<li><strong>Actors: </strong>${data.Actors}</li>
<li><strong>Director: </strong>${data.Director}</li>
<li><strong>Writers: </strong>${data.Writer}</li>
</ul>
<ul class="details-ul">
<li><strong>Genre: </strong>${data.Genre}</li>
<li><strong>Release Date: </strong>${data.DVD}</li>
<li><strong>Box Office: </strong>${data.BoxOffice}</li>
<li><strong>Movie Runtime: </strong>${data.Runtime}</li>
</ul>
<p style="font-size: 14px; margin-top:10px;">${data.Plot}</p>
<p style="font-size: 15px; font-style: italic; color: #222; margin-top: 10px;">
<i class="fa-solid fa-award"></i>
  ${data.Awards}
</p>
</div>
`
// Appending the output
document.querySelector('.movie-container').innerHTML = output
}
async function addTofavorites(id) {
console.log("fav-item", id);
localStorage.setItem(Math.random().toString(36).slice(2, 7), id);// math.random for the unique key and value pair
alert('Movie Added to Favorites!');
}
//Removing the movie from the favorites list and also from the localstorage
async function removeFromfavorites(id) {
console.log(id);
for (i in localStorage) {
// If the ID passed as argument matches with value associated with key, then removing it
if (localStorage[i] == id) {
localStorage.removeItem(i)
break;
}
}
//Alerting the user and refreshing the page
alert('Movie Removed from Favorites');
window.location.replace('favorite.html');
}
//Displaying the movie list on the search page according to the user list
async function displayMovieList(movies) {
var output = '';
//Traversing over the movies list which is passed as an argument to our function
for (i of movies) {
var img = '';
if (i.Poster != 'N/A') {
img = i.Poster;
}
else {
img = 'img/blank-poster.jpg';
}
var id = i.imdbID;
//Appending the output through string interpolition
output += `
<div class="fav-item">
<div class="fav-poster">
<a href="movie.html?id=${id}"><img src=${img} alt="Favorites Poster"></a>
</div>
<div class="fav-details">
<div class="fav-details-box">
<div>
<p class="fav-movie-name"><a href="movie.html?id=${id}">${i.Title}</a></p>
<p class="fav-movie-rating"><a href="movie.html?id=${id}">${i.Year}</a></p>
</div>
<div>
<i class="fa-solid fa-bookmark" style="cursor:pointer;" onClick=addTofavorites('${id}')></i>
</div>
</div>
</div>
</div>
`
}
//Appending this to the movie-display class of our html page
document.querySelector('.fav-container').innerHTML = output;
console.log("here is movie list ..", movies);
}
//When the user is searching for the movie then a list of the related movie will be displayed and that list is fetched
async function findMovies() {
const url = `https://www.omdbapi.com/?s=${(searchInput.value).trim()}&page=1&apikey=${key}`
const res = await fetch(`${url}`);
const data = await res.json();
if (data.Search) {
//Calling the function to display list of the movies related to the user search
displayMovieList(data.Search)
}
}
//Favorites movies are loaded on to the fav page from localstorage
async function favoritesMovieLoader() {
var output = ''
//Traversing over all the movies in the localstorage
for (i in localStorage) {
var id = localStorage.getItem(i);
if (id != null) {
//Fetching the movie through id
const url = `https://www.omdbapi.com/?i=${id}&plot=full&apikey=${key}`
const res = await fetch(`${url}`);
const data = await res.json();
console.log(data);
var img = ''
if (data.Poster) {
img = data.Poster
}
else { img = data.Title }
var Id = data.imdbID;
//Adding all the movie html in the output using interpolition
output += `
<div class="fav-item">
<div class="fav-poster">
<a href="movie.html?id=${id}"><img src=${img} alt="Favorites Poster"></a>
</div>
<div class="fav-details">
<div class="fav-details-box">
<div>
<p class="fav-movie-name">${data.Title}</p>
<p class="fav-movie-rating">${data.Year} · <span
style="font-size: 15px; font-weight: 600;">${data.imdbRating}</span>/10</p>
</div>
<div style="color: maroon">
<i class="fa-solid fa-trash" style="cursor:pointer;" onClick=removeFromfavorites('${Id}')></i>
</div>
</div>
</div>
</div>
`;
}
}
//Appending the html to the movie-display class in favorites page
document.querySelector('.fav-container').innerHTML = output;
}