-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
143 lines (121 loc) · 4.02 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
const preloader = document.querySelector('.preloader-wrap');
const list = document.querySelector('.list');
const photos = document.querySelector('.photos');
const todos = document.querySelector('.todos');
const blockWrap = document.querySelector('.block-wrap');
let dataArr = [];
// const hidePreloader = () => {
// preloader.classList.add('preloader-wrap_hidden');
// }
// setInterval(hidePreloader, 5000);
const renderListCard = (arr) => {
arr.map(item => {
const card = document.createElement('li');
card.classList.add('list__card');
const cardCloseBtn = document.createElement('button');
cardCloseBtn.classList.add('block__close-btn');
const cardId = document.createElement('p');
cardId.classList.add('list__id');
const cardName = document.createElement('p');
cardName.classList.add('list__name');
const cardAddress = document.createElement('p');
cardAddress.classList.add('list__address');
cardId.innerText += `ID: ${item.id}`;
cardName.innerText += `Name: ${item.name}`;
cardAddress.innerText += `Address: City: ${item.address.city} ,Street: ${item.address.street}`;
cardCloseBtn.innerHTML = '×';
card.append(cardId, cardName, cardAddress, cardCloseBtn)
list.append(card);
});
}
const renderPhotosCard = (arr) => {
arr.map(item => {
const card = document.createElement('div');
card.classList.add('photos__card');
//
const cardCloseBtn = document.createElement('button');
cardCloseBtn.classList.add('block__close-btn');
//
const cardPict = document.createElement('img');
cardPict.classList.add('photos__pict');
//
if (Math.random() <= 1 && Math.random() >= 0.5) {
cardPict.src = item.thumbnailUrl;
cardPict.alt += `ID: ${item.id}`;
cardCloseBtn.innerHTML = '×';
} else if (Math.random() < 0.5) {
cardPict.src = item.url;
cardPict.alt += `ID: ${item.id}`;
cardCloseBtn.innerHTML = '×';
}
//
card.append(cardPict, cardCloseBtn);
photos.appendChild(card);
});
}
const renderToDosCard = (arr) => {
arr.map(item => {
const card = document.createElement('li');
card.classList.add('list__card');
//
const cardUserId = document.createElement('p');
cardUserId.classList.add('list__user-id');
//
const cardId = document.createElement('p');
cardId.classList.add('list__id');
//
const cardTilte = document.createElement('p');
cardTilte.classList.add('list__title');
//
const cardCompleted = document.createElement('p');
cardCompleted.classList.add('list__completed');
//
const cardCloseBtn = document.createElement('button');
cardCloseBtn.classList.add('block__close-btn');
//
cardUserId.innerText += `User ID: ${item.userId}`
cardId.innerText += `ID: ${item.id}`;
cardTilte.innerText += `Title: ${item.title}`;
if (item.completed)
cardCompleted.innerText += 'Completed: ✔️';
else
cardCompleted.innerText += 'Completed: ❌'
cardCloseBtn.innerHTML = '×';
//
card.append(cardUserId, cardId, cardTilte, cardCompleted, cardCloseBtn);
todos.appendChild(card);
});
}
const ckeckEvent = (e) => {
e.stopPropagation();
const item = e.target;
// console.log(e);
if (item.classList[0] === 'block__close-btn') {
const elem = item.parentElement;
elem.remove();
}
if (item.classList[0] === 'block') {
item.classList.add('block_hidden');
}
if (item.classList[0] === 'block-wrap') {
const elem = document.querySelector('.block');
elem.classList.remove('block_hidden');
}
}
const getData = async (data) => {
const getData = await fetch(`https://jsonplaceholder.typicode.com/${data}`);
dataArr = await getData.json();
return dataArr;
};
getData('users').then(val => {
renderListCard(val);
});
getData('todos').then(val => {
renderToDosCard(val);
});
getData('photos').then(val => {
renderPhotosCard(val);
});
list.addEventListener('click', ckeckEvent);
photos.addEventListener('click', ckeckEvent);
blockWrap.addEventListener('click', ckeckEvent);