-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.jsx
227 lines (226 loc) · 7.81 KB
/
script.jsx
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
215
216
217
218
219
220
221
222
223
224
225
226
227
const navigationBar = document.querySelectorAll('[data-nav-list]');
const addMovieButton = document.querySelector('.movie-list__add-movie');
const movieModal = document.querySelector('.movie__modal-box');
const closeMovieModal = document.querySelector('.close');
//Adding movie into the list
const addMovieToList = document.querySelector('#add-movie');
const movieList = document.querySelector('.movie-system__movie-list');
const selectGenre = document.querySelector('#movie-genre');
const genreLogo = document.querySelector('.add-movie__genre-image');
//Removing the empty message display
const emptyMessage = document.querySelector('.movie-list__empty-item');
//Add movie into the list
const addMovie = () => {
//Create a new item
const newMovieItem = document.createElement('div');
newMovieItem.className = 'movie-list__movie-item';
//Place of movie description container
const newMovieDescriptionContainer = document.createElement('div');
newMovieDescriptionContainer.className = 'movie-description';
//Place of trash bin for delete
const trashBin = document.createElement('div');
trashBin.className = 'trash-icon';
trashBin.innerHTML = '<i class="fas fa-trash-alt fa-2x"></i>';
trashBin.addEventListener('click', () => {
movieList.removeChild(newMovieItem);
//Display empty message
displayEmptyMessage();
});
//Place of logo
const newMovieLogo = document.createElement('div');
newMovieLogo.className = 'movie-logo';
newMovieLogo.innerHTML = changeMovieLogo();
//Movie title
const newMovieTitle = document.createElement('h2');
newMovieTitle.textContent = document.querySelector('#movie-name').value;
//Movie genre
const newMovieGenre = document.createElement('div');
newMovieGenre.classList = 'movie-genre';
newMovieGenre.textContent = document.querySelector('#movie-genre').value;
//Movie rating
const newMovieRating = document.createElement('div');
newMovieRating.className = 'movie-rating';
newMovieRating.innerHTML = changeMovieRating();
//Movie description
const newMovieDescription = document.createElement('p');
newMovieDescription.textContent = document.querySelector('#moviedescription').value;
//Validation in the add movie form
if(validation(
document.querySelector('#movie-name'),
document.querySelector('#movie-genre'),
document.querySelector('#movie-rating'),
document.querySelector('#movie-description')
)) {
movieModal.style.display = 'block';
return;
}
//Append the description information inside the container
newMovieDescriptionContainer.appendChild(newMovieTitle);
newMovieDescriptionContainer.appendChild(newMovieGenre);
newMovieDescriptionContainer.appendChild(newMovieRating);
newMovieDescriptionContainer.appendChild(newMovieDescription);
//Append the container
newMovieItem.appendChild(trashBin);
newMovieItem.appendChild(newMovieLogo);
newMovieItem.appendChild(newMovieDescriptionContainer)
//Add the movie list into the container
movieList.appendChild(newMovieItem);
//Close modal as the item is listed
movieModal.style.display = 'none';
clearFormat(document.querySelector('#movie-name'),
document.querySelector('#movie-genre'),
document.querySelector('#movie-rating'),
document.querySelector('#movie-description')
);
//Remove empty message
displayEmptyMessage();
}
//Change the logo of movie according to genre
const changeMovieLogo = () => {
const genreLogo = document.querySelector('#movie-genre').value;
switch(genreLogo) {
case 'Action':
return '<i class="fas fa-fighter-jet fa-7x"></i>';
case 'Comedy':
return '<i class="far fa-laugh fa-7x"></i>';
case 'Drama':
return '<i class="fas fa-theater-masks fa-7x"></i>';
case 'Horror':
return '<i class="fas fa-ghost fa-7x"></i>';
case 'Romance':
return '<i class="fas fa-heart fa-7x"></i>';
default:
return '';
}
}
//Change the movie ratings
const changeMovieRating = () => {
const rating = document.querySelector('#movie-rating').value;
const remainingStar = 5 - rating;
let starRatings = '';
for(let i = 0; i < rating; i++) {
starRatings += '<i class="fas fa-star" style="margin-right: 4px"></i>';
}
for(let i = 0; i < remainingStar; i++) {
starRatings += '<i class="far fa-star" style="margin-right: 4px"></i>';
}
return starRatings;
}
//Validation of forms
const validation = (title, genre, rating, description) => {
let hasErrors = false;
//Naming validation
if(title.value === undefined || title.value === '') {
title.style.borderColor = 'red';
hasErrors = true;
}
else {
title.style.borderColor = 'white';
}
//Genre validation
if(genre.options[genre.selectedIndex].value === 'None') {
genre.style.borderColor = 'red';
hasErrors = true;
}
else {
genre.style.borderColor = 'white';
}
//Rating validation
if(rating.options[rating.selectedIndex].value === 'None') {
rating.style.borderColor = 'red';
hasErrors = true;
}
else {
rating.style.borderColor = 'white';
}
//Description validation
if(description.value === undefined || description.value === '') {
description.style.borderColor = 'red';
hasErrors = true;
}
else {
description.style.borderColor = 'white';
}
//Return if it has errors
return hasErrors;
}
//Clear error formatting as the movie item is sent
const clearFormat = (title, genre, rating, description) => {
//Remove image
genreLogo.innerHTML ='';
//Default border colors
title.style.borderColor = 'white';
genre.style.borderColor = 'white';
rating.style.borderColor = 'white';
description.style.borderColor = 'white';
//Clear all values
title.value = '';
genre.value = 'None';
rating.value = 'None';
description.value = '';
}
//Whether to display the empty message or not depending on the number of movies
const displayEmptyMessage = () => movieList.childElementCount <= 2 ?
emptyMessage.style.display = 'block' : emptyMessage.style.display = 'none';
//Filter out all movies by genre
const filterMovies = chosenGenre => {
const movieItem = Array.from(document.querySelectorAll('.movie-list__movie-item'));
let selectedGenre = [];
movieItem.forEach(movie => movie.style.display = 'flex');
switch(chosenGenre) {
case 'Action':
selectedGenre = movieItem.filter(movie =>
movie.children[2].children[1].textContent !== 'Action');
break;
case 'Comedy':
selectedGenre = movieItem.filter(movie =>
movie.children[2].children[1].textContent !== 'Comedy');
break;
case 'Drama':
selectedGenre = movieItem.filter(movie =>
movie.children[2].children[1].textContent !== 'Drama');
break;
case 'Horror':
selectedGenre = movieItem.filter(movie =>
movie.children[2].children[1].textContent !== 'Horror');
break;
case 'Romance':
selectedGenre = movieItem.filter(movie =>
movie.children[2].children[1].textContent !== 'Romance');
break;
default:
break;
}
selectedGenre.forEach(movie => movie.style.display = 'none');
}
//Navigation bar setting to active whenever clicked
navigationBar.forEach(navigation => {
navigation.addEventListener('click', e => {
filterMovies(navigation.textContent);
const active = document.querySelector('.active');
active.classList.remove('active');
navigation.classList.add('active');
});
});
//For opening of modal box
addMovieButton.addEventListener('click', () => {
movieModal.style.display = 'block';
});
//When closing of modal box through x icon
closeMovieModal.addEventListener('click', () => {
movieModal.style.display = 'none';
});
//Add movie modal submit button
addMovieToList.addEventListener('click', e => {
addMovie(); //Add movies
});
//Selecting genre and changing its logo in modal
selectGenre.addEventListener('change', e => {
genreLogo.innerHTML = changeMovieLogo();
});
//Whenever the user clicks outside the modal
window.addEventListener('click', e => {
if(e.target === movieModal) { //If the user clicks outside the modal content
movieModal.style.display = 'none';
}
});