-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
53 lines (47 loc) · 1.53 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
const videosContainer = document.getElementById('videosContainer');
const popup = document.getElementById('popup');
const video = document.querySelector('#popup > iframe');
const videoIdInput = document.getElementById('videoId');
const IDS_KEY = 'youTubeVideoIds';
let youTubeVideoIds = [];
const loadVideos = () => {
youTubeVideoIds = JSON.parse(localStorage.getItem(IDS_KEY)) || [];
};
const displayVideos = () => {
const videosStr = youTubeVideoIds
.map(
(id) => `
<li onclick="clickVideo(event, '${id}')">
<img class="thumbnail" src="https://i3.ytimg.com/vi/${id}/sddefault.jpg" alt="Cover image for YouTube video with id ${id}">
<button class="delete-btn" >×</button>
</li>
`
)
.join('');
videosContainer.innerHTML = videosStr;
};
const clickVideo = (event, id) => {
if (event?.target?.classList?.contains('delete-btn')) {
youTubeVideoIds = youTubeVideoIds.filter((i) => i !== id);
localStorage.setItem(IDS_KEY, JSON.stringify(youTubeVideoIds));
displayVideos();
} else {
video.src = `https://www.youtube.com/embed/${id}`;
popup.classList.add('open');
popup.classList.remove('closed');
}
};
const handlePopupClick = () => {
popup.classList.add('closed');
popup.classList.remove('open');
};
const saveVideo = (e) => {
e.preventDefault();
const videoId = videoIdInput.value;
videoIdInput.value = '';
youTubeVideoIds.unshift(videoId);
localStorage.setItem(IDS_KEY, JSON.stringify(youTubeVideoIds));
displayVideos();
};
loadVideos();
displayVideos();