-
Notifications
You must be signed in to change notification settings - Fork 1
/
favourite.js
141 lines (131 loc) · 4.04 KB
/
favourite.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
const apikey = "c9c05ce6"
var arr = JSON.parse(localStorage.getItem("data"));
localStorage.removeItem("data");
localStorage.setItem('data', '[]');
const movieList = document.getElementsByClassName('movie-list');
localStorage.setItem('data', JSON.stringify(arr));
//get unique items
const getUniqueMovies = (array) =>(
[...new Set(array)]
);
arr = getUniqueMovies(arr);
// take out ids from localStorage
takeIdOneByOne(arr);
function takeIdOneByOne(arr){
for(let i = 0; i < arr.length;i++){
if(arr[i] == null){
continue
}else{
saveArrayToLocalStorage(arr[i]);
movieDetails(arr[i]);
}
}
// reloadPage();
}
//save array to local storage
function saveArrayToLocalStorage(id){
if(localStorage.getItem('data') == null){
localStorage.setItem('data', '[]');
}
var localArray = JSON.parse(localStorage.getItem('data'));
localArray.push(id);
localStorage.setItem('data', JSON.stringify(localArray));
}
//create url
function movieDetails(movieId){
var url = "http://www.omdbapi.com/?i="+movieId+"&apikey="+apikey;
fetchDataFromApiforDetails(url);
url = ''
}
// fetch data form api
async function fetchDataFromApiforDetails(url){
try {
let data = await fetch(url);
const responseData = await data.json();
if(responseData.Title != undefined){
fevMoviePage(responseData);
}else{
throw new Error('There is no movie found in this name');
}
}
catch(err){
showNotification(err);
return;
}
}
//create fevourite page
function fevMoviePage(responseData){
const movieDiv = document.createElement('div');
movieDiv.className = "movie-area"
movieDiv.id = responseData.imdbID;
movieDiv.innerHTML = `
<div class="poster">
<img src="${responseData.Poster}">
</div>
<div class="details">
<p>${responseData.Title} (${responseData.Year})</p>
<p>Language:${responseData.Language}</p>
<button class="remove-fev" id=${responseData.imdbID}><i class="fa-solid fa-trash-can"></i>Remove Favorite</button>
</div>
`
movieList[0].appendChild(movieDiv);
}
// alart function
function showNotification(text){
alert(text);
}
//remove movie from fevorite
function removeFevoriteFromArray(id){
const data = localStorage.getItem('data');
if(data.length == 1){
localStorage.clear();
return;
}
const element = document.getElementById(id);
element.classList.add('hide-display');
updateLocalStorage(id);
}
//update the local storage
function updateLocalStorage(id){
localStorage.removeItem("data");
if(localStorage.getItem('data') == null){
localStorage.setItem('data', '[]');
}
arr = arr.filter(e => e !== id);
var localArray = JSON.parse(localStorage.getItem('data'));
for(let i = 0; i < arr.length; i++){
localArray.push(arr[i]);
}
localStorage.setItem('data', JSON.stringify(localArray));
}
//add eventListener
function handledClickListener(e){
const target = e.target;
if(target.className === 'remove-fev' || target.className === 'fa-solid fa-trash-can'){
const id = target.id;
removeFevoriteFromArray(id);
showNotification('Movie delete from your fevourite list...')
}
if(target.className === 'remove-all' || target.className === 'fa-solid fa-eraser'){
var arr2 = JSON.parse(localStorage.getItem("data"));
if(arr2.length > 0){
localStorage.removeItem("data");
localStorage.setItem('data', '[]');
reloadPage();
showNotification('all Item deleted....')
}else{
showNotification('You already deleted All......');
return;
}
}
if(target.className === 'button' || target.className === 'fa-solid fa-house-user'){
window.open("index.html");
}
}
//relode page
// reloadPage();
function reloadPage(){
location.reload();
}
// event listener
document.addEventListener('click',handledClickListener);