forked from codewithvk/Movie-Review
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
159 lines (136 loc) · 4.4 KB
/
app.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
const addMovieModal = document.getElementById('add-modal');
// const addMovieModal = document.querySelector('#add-modal');
const startAddMovieButton = document.querySelector('header button');
// const startAddMovieButton = document.querySelector('header').lastElementChild;
const backdrop = document.getElementById('backdrop');
// const backdrop = document.body.firstElementChild;
const cancelAddMovieButton = addMovieModal.querySelector('.btn--passive');
const confirmAddMovieButton = cancelAddMovieButton.nextElementSibling;
const userInputs = addMovieModal.querySelectorAll('input');
// const userInputs = addMovieModal.getElementsByTagName('input');
const entryTextSection = document.getElementById('entry-text');
const deleteMovieModal = document.getElementById('delete-modal');
const movies = [];
const toggleBackdrop = () => {
backdrop.classList.toggle('visible');
};
const updateUI = () => {
if (movies.length === 0) {
entryTextSection.style.display = 'block';
} else {
entryTextSection.style.display = 'none';
}
};
const closeMovieDeletionModal = () => {
toggleBackdrop();
deleteMovieModal.classList.remove('visible');
};
const deleteMovieHandler = movieId => {
let movieIndex = 0;
for (const movie of movies) {
if (movie.id === movieId) {
break;
}
movieIndex++;
}
movies.splice(movieIndex, 1);
const listRoot = document.getElementById('movie-list');
listRoot.children[movieIndex].remove();
// listRoot.removeChild(listRoot.children[movieIndex]);
closeMovieDeletionModal();
updateUI();
};
const startDeleteMovieHandler = movieId => {
deleteMovieModal.classList.add('visible');
toggleBackdrop();
const cancelDeletionButton = deleteMovieModal.querySelector('.btn--passive');
let confirmDeletionButton = deleteMovieModal.querySelector('.btn--danger');
confirmDeletionButton.replaceWith(confirmDeletionButton.cloneNode(true));
confirmDeletionButton = deleteMovieModal.querySelector('.btn--danger');
// confirmDeletionButton.removeEventListener('click', deleteMovieHandler.bind(null, movieId)); // will not work :(
cancelDeletionButton.removeEventListener('click', closeMovieDeletionModal);
cancelDeletionButton.addEventListener('click', closeMovieDeletionModal);
confirmDeletionButton.addEventListener(
'click',
deleteMovieHandler.bind(null, movieId)
);
};
const renderNewMovieElement = (id, title, imageUrl, rating) => {
const newMovieElement = document.createElement('li');
newMovieElement.className = 'movie-element';
newMovieElement.innerHTML = `
<div class="movie-element__image">
<img src="${imageUrl}" alt="${title}">
</div>
<div class="movie-element__info">
<h2>${title}</h2>
<p>${rating}/5 stars</p>
</div>
`;
newMovieElement.addEventListener(
'click',
startDeleteMovieHandler.bind(null, id)
);
const listRoot = document.getElementById('movie-list');
listRoot.append(newMovieElement);
};
const closeMovieModal = () => {
addMovieModal.classList.remove('visible');
};
const showMovieModal = () => {
// function() {}
addMovieModal.classList.add('visible');
toggleBackdrop();
};
const clearMovieInput = () => {
for (const usrInput of userInputs) {
usrInput.value = '';
}
};
const cancelAddMovieHandler = () => {
closeMovieModal();
toggleBackdrop();
clearMovieInput();
};
const addMovieHandler = () => {
const titleValue = userInputs[0].value;
const imageUrlValue = userInputs[1].value;
const ratingValue = userInputs[2].value;
if (
titleValue.trim() === '' ||
imageUrlValue.trim() === '' ||
ratingValue.trim() === '' ||
+ratingValue < 1 ||
+ratingValue > 5.1
) {
alert('Please enter valid values (rating between 1 and 5).');
return;
}
const newMovie = {
id: Math.random().toString(),
title: titleValue,
image: imageUrlValue,
rating: ratingValue
};
movies.push(newMovie);
console.log(movies);
closeMovieModal();
toggleBackdrop();
clearMovieInput();
renderNewMovieElement(
newMovie.id,
newMovie.title,
newMovie.image,
newMovie.rating
);
updateUI();
};
const backdropClickHandler = () => {
closeMovieModal();
closeMovieDeletionModal();
clearMovieInput();
};
startAddMovieButton.addEventListener('click', showMovieModal);
backdrop.addEventListener('click', backdropClickHandler);
cancelAddMovieButton.addEventListener('click', cancelAddMovieHandler);
confirmAddMovieButton.addEventListener('click', addMovieHandler);