-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtask-5.html
More file actions
186 lines (164 loc) · 6.44 KB
/
task-5.html
File metadata and controls
186 lines (164 loc) · 6.44 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Task 5: Fetch phones via XHR.</title>
<link rel="stylesheet" href="css/base.css">
</head>
<body>
<main class="flex justify-center min-h-screen">
<div class="container mx-auto py-20 flex">
<section class="flex-1">
<div class="flex justify-between mb-5">
<hgroup>
<h1 class="text-3xl mb-0">Highend Phones</h1>
<h5 class="mt-0 font-sans"></h5>
</hgroup>
<button role="refresh" class="appearance-none py-2 px-4 border border-gray rounded-full font-sans">REFRESH</button>
</div>
<!-- add form here -->
<form id="insert-form" class="flex">
<input type="text" placeholder="Add a phone model" class="flex-1 border border-gray-500 p-2 pl-5"/>
<button class="px-4 appearance-none hover:bg-gray-200 border border-gray-900">INSERT</button>
</form>
<!-- add preloader -->
<div data-preload class="py-2 text-center bg-black text-white">Loading...</div>
<ul class="list-holder p-0">
</ul>
</section>
<section class="flex-1 ml-10 border border-gray-200 p-5">
<div class="flex justify-between mb-5">
<h1>SOLD PHONES</h1>
</div>
<ul class="sold-list-holder p-0">
</ul>
</section>
</div>
</main>
<script>
const preload = () => {
const preloader = document.querySelector('[data-preload]')
setInterval(() => {
if (preloader.textContent.length >= 10)
preloader.textContent = "Loading"
preloader.append('.');
}, 1000);
}
const hidePreloader = () => {
document.querySelector('[data-preload]').classList.add('hidden')
}
const showPreloader = () => {
document.querySelector('[data-preload]').classList.remove('hidden')
}
const PHONES_ENDPOINT = 'https://frontend-tasks.wigxel.now.sh/mocks/phones.json'
const PhoneHolder = {
soldPhones: [],
phones: [],
get() {
return this.phones;
},
set(newPhones) {
return this.phones = newPhones;
},
getSold() {
return this.soldPhones
},
setSold(phones) {
this.soldPhones = phones
},
}
// adds the click event to
const activeToggleButton = (listEl) => {
const arrowButton = listEl.querySelector('.toggle-btn');
const toggleAction = () => {
if (listEl.classList.contains('is-active')) {
listEl.classList.remove('is-active');
arrowButton.textContent = '<';
} else {
listEl.classList.add('is-active');
arrowButton.textContent = '>';
}
}
// call the toggleAction
arrowButton.addEventListener('click', toggleAction);
}
// add activeDeleteButton function here
const activeDeleteButton = (listEl, doAfterDelete) => {
const deleteButton = listEl.querySelector('button[role=delete]');
deleteButton.addEventListener('click', () => {
listEl.classList.add('hide__self');
setTimeout(() => {
listEl.outerHTML = '';
doAfterDelete()
}, 1000);
})
}
const createList = (phone_name) => {
// NB: Virtual Elements don't exist in DOM after creation.
const listEl = document.createElement('li');
listEl.classList.add('fancy__list');
listEl.innerHTML = `
<div class="text">${phone_name}</div>
<button class="toggle-btn"><</button>
<button class="bg-teal-200" role="add-to-sold">+</button>
<button class="bg-orange-200">-</button>
<button class="bg-red-200" role="delete">x</button>
`;
const onDelete = () => {
const existingPhones = [];
for (let phone of PhoneHolder.get()) {
if (phone !== phone_name) {
existingPhones.push(phone);
}
}
PhoneHolder.set(existingPhones)
refreshList();
}
activeToggleButton(listEl);
activeDeleteButton(listEl, onDelete);
// activateAddButton(listEl, phone_name);
return listEl
}
// adds the list to the DOM
const addToList = (arr, selector) => {
const listHolder = document.querySelector(selector)
listHolder.innerHTML = '';
for (let listEl of arr) {
listHolder.append(listEl)
}
}
const refreshList = () => {
// create the phoneList
const phoneList = []
const soldList = []
for (let model of PhoneHolder.get()) {
const listEl = createList(model)
phoneList.push(listEl)
}
for (let model of PhoneHolder.getSold()) {
const listEl = createList(model)
soldList.push(listEl)
}
// call addToList here
addToList(phoneList, '.list-holder');
addToList(soldList, '.sold-list-holder');
}
// add movePhoneToSold() here from the previous task
// add the activeAddButton() here from the previous task
// add the activateInsertForm() here from the previous task
const startApp = () => {
const refreshButton = document.querySelector('[role=refresh]')
refreshButton.addEventListener('click', refreshList)
refreshButton.click();
preload();
setTimeout(() => {
fetchPhones();
}, 2000);
activateInsertForm();
}
startApp()
</script>
</body>
</html>