-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
215 lines (192 loc) · 7.17 KB
/
script.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
const btnSearch = document.querySelector(".btn-search")
const movieContainerEl = document.querySelector('.container-movie')
const checkLocalStorage = JSON.parse(localStorage.getItem("localWatchlistArray"))
let movieArray = []
let watchlistArray = []
let totalMoviePages = ""
if (checkLocalStorage) {
watchlistArray = checkLocalStorage
}
// Movie Page
if (getCurruntPage() === "movie") {
document.querySelector('.my-watchlist').addEventListener('click', () => {
window.location.href = "watchlist.html"
})
btnSearch.addEventListener('click', () => {
movieArray = []
const searchText = document.querySelector('.search-text').value.trimEnd()
if (searchText == "") {
setEmptyContainer("emptySearch")
}
else {
getMovieId(searchText, 1)
}
})
document.addEventListener('click', (e) => {
if (e.target.dataset.imdb) {
const tagetElementId = e.target.id
document.getElementById(tagetElementId)
{
document.getElementById(`${tagetElementId}`).classList.remove('fa-circle-plus')
document.getElementById(`${tagetElementId}`).classList.add('fa-circle-check')
}
updateWatchlistArray(e.target.dataset.imdb)
}
else if (e.target.dataset.page) {
movieArray = []
const searchText = document.querySelector('.search-text').value.trimEnd()
getMovieId(searchText, parseInt(e.target.id))
}
})
function updateWatchlistArray(movieId) {
const checkArray = watchlistArray.find(item => item.imdbID === movieId)
if (checkArray) {
window.alert("Already in Watchlist")
}
else {
let watchlistObj = movieArray.filter(item => item.imdbID === movieId)[0]
watchlistArray.push(watchlistObj)
updateLocalStorage()
}
}
function getMovieId(searchText, pageNo) {
loadaing()
fetch(`https://www.omdbapi.com/?apikey=367e161c&s=${searchText}&page=${pageNo}`)
.then(res => res.json())
.then(data => {
totalMoviePages = Math.ceil(data.totalResults / 10)
if (data.Response === "True") {
const moviesIdArray = data.Search.map(item => item.imdbID)
fetchMovies(moviesIdArray)
}
else {
setEmptyContainer("movieContainer")
}
})
}
async function fetchMovies(data) {
for (let item of data) {
item = item.replace('"', '')
await fetch(`https://www.omdbapi.com/?apikey=367e161c&i=${item}`)
.then(res => res.json())
.then(data => movieArray.push(data))
}
render(movieArray)
genratePageNumber(totalMoviePages)
}
}
// Watchlist Page
if (getCurruntPage() === "watchlist") {
document.querySelector(".container-movie").style.padding = "20px 20px"
document.querySelector('.go-to-search').addEventListener('click', () => {
window.location.href = "index.html"
})
render(watchlistArray)
if (watchlistArray.length === 0) {
setEmptyContainer("watchlistContainer")
}
document.addEventListener("click", (e) => {
if (e.target.dataset.imdb) {
removeArrayObject(e.target.dataset.imdb)
render(watchlistArray)
}
if (watchlistArray.length === 0) {
setEmptyContainer("watchlistContainer")
}
})
}
// Utils
function loadaing() {
movieContainerEl.innerHTML = `
<div class="logo-container">
<img src="assert/load.svg" class="loading">
</div>
`
}
function getCurruntPage() {
return document.body.classList.contains('page-movie') ? "movie" : "watchlist"
}
function genratePageNumber(numOfPages) {
let pageNumHtml = Array(numOfPages).fill(0).map((item, index) => item = index + 1)
pageNumHtml = pageNumHtml.map(item => item =
`<span id="${item}" data-page=${item} class="number">${item}</span>`).join("")
movieContainerEl.innerHTML +=
`<div class="page-number">
${pageNumHtml}
</div>`
}
function checkExistingElement(item) {
return checkLocalStorage.filter(oldItem => oldItem.imdbID === item).length > 0 ? true : false
}
function removeArrayObject(itemId) {
watchlistArray = watchlistArray.filter(item => item.imdbID != itemId)
updateLocalStorage()
}
function updateLocalStorage() {
localStorage.setItem("localWatchlistArray", JSON.stringify(watchlistArray))
}
function setEmptyContainer(message) {
if (message === "watchlistContainer") {
movieContainerEl.innerHTML = `
<div class="logo-container">
<p>Your Watchlist is litte empty </p>
<p class="font-dark">
<a href="index.html">
<i class="fa-solid fa-circle-plus"></i>
</a>
Let's add some movies
</p>
</div>
`
}
else if (message === "movieContainer") {
movieContainerEl.innerHTML = `
<div class="logo-container">
<p>
Movie Not Found Please Try another Search
</p>
</div>
`
}
else if (message === "emptySearch") {
movieContainerEl.innerHTML = `
<div class="logo-container">
<p>
Try Putting some text into search box and search again
</p>
</div>`
}
}
function render(data) {
let html = ""
let icon = `fa-circle-plus`
data.forEach(item => {
if (checkLocalStorage && getCurruntPage() == "movie") {
icon = checkExistingElement(item.imdbID) ? `fa-circle-check` : `fa-circle-plus`
}
else if(getCurruntPage()=="watchlist"){
icon = "fa-circle-minus"
}
html += `
<div class="movie-card">
<div class="movie-image" style="background-image:url('${item.Poster}')"></div>
<div class="movie-details">
<h2 class="movie-name"> ${item.Title}
<span class="movie-rating">⭐${item.imdbRating}</span>
</h2>
<p class="movie-info">
<span class="movie-duration">${item.Runtime}</span>
<span class="movie-genres">${item.Genre}</span>
<span class="watchlist" >
<i class="fa-solid ${icon}" data-imdb="${item.imdbID}" id=${item.imdbID}></i> watchlist
</span>
</p>
<p class="movie-descreption">
${item.Plot}
</p>
</div>
</div>
`
})
movieContainerEl.innerHTML = html
}