-
Notifications
You must be signed in to change notification settings - Fork 0
/
vanilla.js
332 lines (320 loc) · 9.21 KB
/
vanilla.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
const data = {
items: [{
id: 1,
img: "./src/Images/Watches/Heuer-SilverSnoe.jpg",
name: "Heuer Silver Snoe",
price: 6000,
category: "watches"
}, {
id: 2,
img: "./src/Images/Watches/omega-automatic-seamaster.jpg",
name: "Omega Automatic",
price: 6000,
category: "watches"
}, {
id: 3,
img: "./src/Images/Watches/omega-de-ville-watch.jpg",
name: "Omega De Ville",
price: 6000,
category: "watches"
}, {
id: 4,
img: "./src/Images/Watches/schawffhausen-watch.jpg",
name: "IWC SCHAWFFHUSEN",
price: 6000,
category: "watches"
}, {
id: 5,
img: "./src/Images/Men/AmaXhosa/the-xhosa-man-in-blue-and-white.png",
name: "Xhosa Attire",
price: 15000,
category: "guys"
}, {
id: 6,
img: "./src/Images/Men/EmaSwati/swati-man-at-mountain.png",
name: "Swati Attire",
price: 15000,
category: "guys"
}, {
id: 7,
img: './src/Images/Men/EmaSwati/swati-man-in-traditional-attire.jpg',
name: "Siswati Attire",
price: 15000,
category: "guys"
}, {
id: 8,
img: "./src/Images/Men/BaSotho/sotho-guy-at-entrande.png",
name: "Morden Sotho Attire",
price: 6000
}, {
id: 9,
img: "./src/Images/Men/BaSotho/sotho-man-in-sotho-blanket.png",
name: "Sotho Blanket",
price: 1200
}, {
id: 10,
img: "./src/Images/Men/AmaXhosa/xhosa-man-in-white.png",
name: "Xhosa Attire",
price: 15000,
category: "guys"
}, {
id: 11,
img: "./src/Images/Men/AmaXhosa/xhosa-guy-in-white.jpg",
name: "Xhosa Attire",
price: 15000,
category: "guys"
}, {
id: 12,
img: "./src/Images/Women/Basotho/woman-blanket-jacket.png",
name: "Sotho blacket",
price: 15000,
category: "girls"
}, {
id: 13,
img: "./src/Images/Women/EmaSwati/swati-woman-in-blue-dress.png",
name: "Swati traditional dress",
price: 15000,
category: "girls"
}, {
id: 14,
img: "./src/Images/Women/Basotho/sotho-woman-in-seshweshwe.png",
name: "Seshweshwe dress",
price: 15000,
category: "girls"
}, {
id: 15,
img: "./src/Images/Women/AmaXhosa/xhosa-lady.jpg",
name: "Xhosa white dress",
price: 15000,
category: "girls"
}, {
id: 16,
img: "./src/Images/Women/EmaSwati/swati-woman-in-white-wedding-dress.png",
name: "Swati wedding dress",
price: 15000,
category: "girls"
}, {
id: 17,
img: "./src/Images/Women/AmaXhosa/xhosa-woman-in-blknwht.png",
name: "Xhosa white dress",
price: 15000,
category: "girls"
}, {
id: 19,
img: "./src/Images/Women/AmaXhosa/little-xhosa-girl.png",
name: "Xhosa beads work",
price: 15000,
category: "girls"
}, {
id: 20,
img: "./src/Images/Women/AmaXhosa/xhosa-lady.png",
name: "Xhosa Yellow dress",
price: 15000,
category: "girls"
}, {
id: 21,
img: "./src/Images/Women/EmaSwati/swati-lady-in-green.png",
name: "Green Swati dress",
price: 15000,
category: "girls"
}, {
id: 22,
img: "./src/Images/Women/AmaXhosa/xhosa-woman-in-white-attire.png",
name: "Xhosa attire",
price: 15000,
category: "girls"
}, {
id: 23,
img: "./src/Images/Women/Basotho/4b8273e27d70a288a77c7a763cc7eef1.jpg",
name: "Brown Seshweshwe dress",
price: 15000,
category: "girls"
}]
}
const {items} = data;
const girls = items.filter(item=>item.category === "girls");
const guys = items.filter(item=>item.category === "guys");
const watches = items.filter(item=>item.category === "watches");
const getImgName = (id)=>{
/*switch(id){
case "Home":
return "black-white-triangles2.jpg"
break;
case "Men":
return "pebbles.jpg"
break;
case "Women":
return "grass.jpg"
break;
case "Watches":
return "broken-glass2.jpg"
break;
}
*/
}
let count = 0;
const itemsOnCart = [];
const root = document.getElementById("root");
root.innerHTML = MainPage(getOutlet("Home"))
const buttons = document.querySelectorAll("button");
const container = document.querySelector(".products--parent");
const products = ()=>document.querySelectorAll(".product");
const searchForItem = (_id)=>{
let wantedArray = [];
[watches, guys, girls].forEach(obj=>obj.forEach(item=>{
if (item.id === parseInt(_id))
return wantedArray.push(item);
}));
return wantedArray;
}
allowClick();
buttons.forEach(btn=>btn.addEventListener("click", (e)=>{
let {id} = e.target
if (id == "on-cart") {
return itemsList();
}
container.innerHTML = getOutlet(id);
container.style.backgroundImage = `url(./background/${getImgName(id)})`;
allowClick();
}));
function allowClick() {
products().forEach(item=>item.addEventListener("click", (e)=>{
const {id} = event.currentTarget;
const [item] = searchForItem(id)
let s = `
<div id="${item.id}" class='product details'>
<div class="selected-product">
<img src=${item.img} class='product--img' />
<div class="content">
<p id="details" class='product--name'>${item.name}</p>
<p id="details" class='price'>Price: R ${item.price}</p>
<button id="${item.id}" class='btn--cart'>
Add to Cart
</button>
</div>
</div>
<section class = "description">
<h4>Details</h4>
<p class="description">
lorem ipsum ipsum lorem ipusm lorem
ipsum ipsum lorem ipusm<br/>
lorem ipsum ipsum lorem ipusm lorem ipsum ipsum lorem<br/>ipusm
lorem ipsum ipsum lorem ipusm lorem ipsum ipsum lorem ipusm<br/>
</p>
</section>
</div>
</div>`
container.innerHTML = s;
const cartBtn = document.querySelector(".btn--cart");
cartBtn.addEventListener("click", (e)=>handleClick(e));
}
))
}
function handleClick(e) {
const {id} = e.target;
let[item] = searchForItem(id);
updateCount(item);
}
function updateCount(item) {
count++;
itemsOnCart.push(item);
document.getElementById("count").innerText = count;
document.querySelector(".div-cart-icon").style.display = "block";
}
function itemsList() {
if (count) {
//const list = pageItems(itemsOnCart);
//let differentItems= itemsOnCart.filter(item => itemsOnCart.includes(item) === false);
let list = itemsOnCart.reduce((acc,item)=>{
return acc + (`<div id="${item.id}" class='product cart'>
<img src=${item.img} class='product--img' />
<p class='product--name crt'>${item.name}</p>
<p class='price crt'>R ${item.price}</p>
</div>`);
}
, "")
container.style = `display: flex; flex-direction: column;`
container.innerHTML = list;
}
}
function getOutlet(name) {
let array
const {products} = data
switch (name) {
case "Home":
array = randomItemsArray();
break;
case "Men":
array = guys
break;
case "Women":
array = girls
break;
case "Watches":
array = watches;
break;
}
return pageItems(array, name);
}
function pageItems(clothesArray, name) {
const baseString = clothesArray.reduce((acc,item)=>{
let changeColor = true;
let s = item && (`<div id="${item.id}"
class='product ${(name === "Watches") ? "watches" : ""}'
>
<img src=${item.img} class='product--img' />
<p class='product--name'>${item.name}</p>
</div>`) || "";
return acc + s;
}
, "");
return baseString
}
function MainPage(func) {
return (`
<header>
<nav>
<div class="container">
<img src="./background/site-logo-2.png" class="site-logo">
</div>
<div class="pages">
<button id="Home" >Home</button>
<button id="Men" >Men</button>
<button id="Women" >Women</button>
<button id="Watches" >Watches</button>
<div class="div-cart-icon"style="display:none;">
<button id="on-cart"
style="font: 0.5rem white helvetica; font-weight: bold;"
class="items-count"
>
Cart
</button>
<p id="count"></p>
</div>
</div>
</nav>
</header>
<main>
<div class="products--parent">${func}</div>
</main>
<footer>
<p className='p--footer'>© 2023 Siyabonga Mahlalela</p>
</footer>
`)
}
function randomItemsArray() {
let allProducts = [];
let selectedProducts = [];
const randomArray = ()=>{
let randomNum = Math.floor(Math.random() * 3) + 1;
return randomNum < 2 ? girls : randomNum === 2 ? watches : guys;
}
for (let i = 0; i < 9; i++) {
let selectedArray = randomArray()
let randomImage = (i === 0) ? 0 : Math.floor(Math.random() * selectedArray.length) + 1
let item = selectedArray[randomImage];
//setSelectedProducts( (prevItems) => [...prevItems, item] );
selectedProducts.push(item);
}
return selectedProducts
}