-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
104 lines (86 loc) · 3.06 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
const app = document.getElementById("root");
const container = document.createElement("div");
container.classList.add("container");
app.appendChild(container);
const counter = document.getElementById("counter");
let total;
const storage = {
get() {
return JSON.parse(localStorage.getItem("watchedMovies")) || [];
},
set() {
localStorage.setItem("watchedMovies", JSON.stringify(watcheds));
}
}
let watcheds = storage.get();
const verifyMovie = (card) => {
watcheds.forEach(movie => {
if(movie === card.dataset.title) {
card.classList.toggle("active");
card.dataset.watched = "true";
const checkBox = card.childNodes[0];
const check = document.createElement("div");
check.classList.add("check");
checkBox.appendChild(check);
}
})
}
const getMovies = () => {
fetch("https://ghibliapi.herokuapp.com/films")
.then(response => {
if(!response.ok) {
throw new Error(`Ops...something went wrong: error ${response.status}, ${response.statusText}`);
}
return response.json()
})
.then(data => {
data.forEach(movie => {
total = data.length
counter.innerText = `${watcheds.length}/${total}`;
const card = document.createElement("div");
card.classList.add("card");
card.classList.toggle("active");
card.setAttribute("onclick", "toggleMovie(this)");
card.dataset.title = movie.title;
container.appendChild(card);
const checkBox = document.createElement("div");
checkBox.classList.add("check-box");
card.appendChild(checkBox);
verifyMovie(card);
const h1 = document.createElement("h1");
h1.innerText = movie.title;
card.appendChild(h1)
});
})
.catch(err => {
const warning = document.createElement("h1");
warning.setAttribute("class", "warning");
warning.textContent = err;
container.appendChild(warning);
})
}
const toggleMovie = (card) => {
card.classList.toggle("active");
const movieTitle = card.dataset.title;
const checkBox = card.childNodes[0]
if(!card.dataset.watched) {
card.dataset.watched = "true";
watcheds = [...watcheds, movieTitle];
const check = document.createElement("div");
check.classList.add("check");
checkBox.appendChild(check);
storage.set(watcheds);
} else {
card.removeAttribute("data-watched");
card.childNodes[0].childNodes[0].remove();
for(let i = 0; i < watcheds.length; i++) {
let movie = watcheds[i];
if(movieTitle == movie){
watcheds.splice(i, 1);
}
storage.set(watcheds);
}
}
counter.innerText = `${watcheds.length}/${total}`;
}
getMovies();