-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
120 lines (101 loc) · 3.65 KB
/
index.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
const apiKey = "e631858d"
let allMoviesArray = []
let watchlistArray = []
const searchInputEl = document.getElementById("search-input")
const formEl = document.getElementById("form")
const moviesContainerEl = document.getElementById("movies-container")
const addedMsgEl = document.getElementById('added-msg')
formEl.addEventListener("submit", async function(e) {
e.preventDefault()
let moviesArray = []
//Getting the result of searched film
const response = await fetch(`https://www.omdbapi.com/?apikey=${apiKey}&s=${searchInputEl.value}`)
const data = await response.json()
moviesArray = data.Search
if (data.Response === "True") {
getMoviesInfo(moviesArray)
}else {
moviesContainerEl.innerHTML = `
<div class="not-found-container">
<p class="not-found">Unable To Find What You’re Looking For<br>Please Try Another Search.
</div>
`
}
searchInputEl.value = ""
});
// Lister for add movie in localStorage
moviesContainerEl.addEventListener("click", function(e) {
if (e.target.dataset.addMovie) {
let found=false
watchlistArray.forEach(movie => {
if(e.target.dataset.addMovie === movie.imdbID) {
found=true
}
});
if(found === false) {
const foundFilm = allMoviesArray.find((movie) => movie.imdbID === e.target.dataset.addMovie)
watchlistArray.push(foundFilm)
localStorage.setItem('id', JSON.stringify(watchlistArray))
addedMsgEl.classList.toggle('added')
addedMsgEl.innerText = 'Film Added To Your Watchlist'
setTimeout(() => {
addedMsgEl.classList.toggle("added")
}, 3000)
}else {
addedMsgEl.classList.toggle("added")
addedMsgEl.innerText = 'Film Already In Watchlist'
setTimeout(() => {
addedMsgEl.classList.toggle("added")
}, 3000)
}
}
})
async function getMoviesInfo(arr) {
let moviesInfoArray = []
// fetching the movies with full info, pushing all the info in the array and calling
// render function passing this array
arr.map(async (movie) => {
const response = await fetch(`https://www.omdbapi.com/?apikey=${apiKey}&i=${movie.imdbID}`)
const data = await response.json()
moviesInfoArray.push(data)
renderHtml(moviesInfoArray)
});
console.log(moviesInfoArray)
allMoviesArray = moviesInfoArray
}
function renderHtml(arr) {
let html = ""
arr.map(movie => {
const {
Poster,
Title,
Ratings,
Runtime,
Genre,
Plot,
imdbID
} = movie
html += `
<div class="movie">
<img src="${Poster}" class="poster" />
<div class="movie-details-container">
<div class="movie-title">
<h2>${Title}</h2>
<p class="ratings">⭐ ${Ratings[0].Value}</p>
</div>
<div class="info-container">
<div id="info" class="info">
<p>${Runtime}</p>
<p>${Genre}</p>
<p class="watchlist fa-solid fa-circle-plus" data-add-movie="${imdbID}"> Watchlist</p>
</div>
<div class="plot">
<p>${Plot}</p>
</div>
</div>
</div>
</div>
`
}).join('')
moviesContainerEl.innerHTML = html
}