generated from codetracker-learning/LAB-pet-adoption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
464 lines (421 loc) · 15.4 KB
/
main.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
const pets = [
{
id: 1,
name: "Dusty",
color: "Green",
specialSkill: "Gives sincere apologies.",
type: "cat",
imageUrl: "http://kittentoob.com/wp-content/uploads/2015/06/funny-cat-with-a-towel.jpg",
},
{
id: 2,
name: "Trouble",
color: "Brown",
specialSkill: "Just picks the tomatoes off of a sandwich instead of requesting a whole new sandwich.",
type: "dino",
imageUrl: "http://www.jozilife.co.za/wp-content/uploads/The-Dino-Expo.jpg",
},
{
id: 3,
name: "Whiskers",
color: "Yellow",
specialSkill: "Can prove he is a real man by drinking whiskey.",
type: "dino",
imageUrl: "https://mydinosaurs.com/wp-content/uploads/2017/02/2-3-600x400.jpg"
},
{
id: 4,
name: "Coco",
color: "Black",
specialSkill: "Burps minimally.",
type: "dog",
imageUrl: "http://cdn.akc.org/content/article-body-image/funny-pembroke_welsh_corgi.jpg"
},
{
id: 5,
name: "Spooky",
color: "Brown",
specialSkill: "Comfortable in the outdoors for up to eight hours.",
type: "cat",
imageUrl: "http://www.catster.com/wp-content/uploads/2017/08/A-fluffy-cat-looking-funny-surprised-or-concerned.jpg"
},
{
id: 6,
name: "Tiger",
color: "Black",
specialSkill: "Can read (but cannot understand) Hebrew.",
type: "dog",
imageUrl: "https://upload.wikimedia.org/wikipedia/commons/7/7e/Basset_Hound_600.jpg"
},
{
id: 7,
name: "Oreo",
color: "Yellow",
specialSkill: "Able to stop chewing ice or whistling on request.",
type: "cat",
imageUrl: "https://i.pinimg.com/originals/9d/da/3e/9dda3e5fd2b9886fc3d13ee47f52e8a0.jpg"
},
{
id: 8,
name: "Ginger",
color: "Grey",
specialSkill: "Comfortable in the outdoors for up to eight hours.",
type: "dino",
imageUrl: "https://gingerparrot.co.uk/wp/wp-content/uploads/2020/07/Celebrating-Redheads-photography.jpg"
},
{
id: 9,
name: "Sassy",
color: "Brown",
specialSkill: "Adept at talking self and others out of fights.",
type: "cat",
imageUrl: "https://tailandfur.com/wp-content/uploads/2015/09/40-Amazing-Cat-Funny-Moment-Pictures-Feature-Image.jpg"
},
{
id: 10,
name: "Sammy",
color: "Blue",
specialSkill: "Listens attentively to boring stories.",
type: "cat",
imageUrl: "https://i.pinimg.com/originals/04/54/92/0454926d39eeb420f4f01948e94e9e41.jpg"
},
{
id: 11,
name: "Coco",
color: "Orange",
specialSkill: "Can be around food without staring creepily at it.",
type: "dino",
imageUrl: "http://www.cantref.com/userfiles/events/walking%20dino.jpg?w=600"
},
{
id: 12,
name: "Buster",
color: "Green",
specialSkill: "Does not use excessive acronyms.",
type: "dog",
imageUrl: "https://i.pinimg.com/originals/1f/30/8c/1f308c9c108a384b91b39430cc7312e9.jpg"
},
{
id: 13,
name: "Chester",
color: "Red",
specialSkill: "Expertly quotes and recognizes dialogue from early seasons of The Simpsons.",
type: "dog",
imageUrl: "http://1kjkdg1axrkd2g03cnboj761.wpengine.netdna-cdn.com/wp-content/uploads/2017/12/braydon-anderson-105552-e1512684107659.jpg"
},
{
id: 14,
name: "Samantha",
color: "Brown",
specialSkill: "Always up for dessert.",
type: "cat",
imageUrl: "http://3.bp.blogspot.com/-RzIFLsIO-XQ/UFoMLOT66JI/AAAAAAAAVps/JRF98hdA9S8/s640/funny-cat-pictures-016-027.jpg"
},
{
id: 15,
name: "Coco",
color: "Red",
specialSkill: "Burps minimally.",
type: "cat",
imageUrl: "http://cathumor.net/wp-content/uploads/2013/12/cat-humor-funny-karate-cat-2.jpg"
},
{
id: 16,
name: "Smokey",
color: "Brown",
specialSkill: "Drives at a safe rate of speed in snow or rain.",
type: "dino",
imageUrl: "https://images.readwrite.com/wp-content/uploads/2018/03/t-rex-dino-quiz-e1490854556549.jpg"
},
{
id: 17,
name: "Muffin",
color: "Yellow",
specialSkill: "Does not freak out if you haven’t seen his favorite movie (The Big Lebowski).",
type: "cat",
imageUrl: "http://www.funnycatsite.com/pictures/Close_Up_Yawn.jpg"
},
{
id: 18,
name: "Salem",
color: "Brown",
specialSkill: "Proficient in air guitar",
type: "dino",
imageUrl: "https://www.nation.co.ke/image/view/-/4078922/highRes/1742693/-/maxw/600/-/1453yvh/-/DINO.jpg"
},
{
id: 19,
name: "Callie",
color: "Blue",
specialSkill: "Listens attentively to boring stories.",
type: "dog",
imageUrl: "http://dailynewsdig.com/wp-content/uploads/2014/03/Creative-And-Funny-Dog-Stock-Photography-Pictures-2.jpg"
},
{
id: 20,
name: "Spooky",
color: "Black",
specialSkill: "Uses litter box at appropriate hours.",
type: "cat",
imageUrl: "http://www.funnycatsite.com/pictures/Lazy_White_Cat.jpg"
},
{
id: 21,
name: "Miss kitty",
color: "Red",
specialSkill: "Owns a Nintendo Power Glove.",
type: "dino",
imageUrl: "https://img.buzzfeed.com/buzzfeed-static/static/2015-11/2/12/enhanced/webdr15/anigif_enhanced-29802-1446485228-10.gif?crop=250:165;0,0&downsize=715"
},
{
id: 22,
name: "Snuggles",
color: "Orange",
specialSkill: "Is comfortable with jokes about his receding hairline.",
type: "cat",
imageUrl: "http://funnyanimalphoto.com/wp-content/uploads/2013/08/cat_caught_mouse_thegatewaypundit.jpg"
},
{
id: 23,
name: "Buddy",
color: "Red",
specialSkill: "Enjoys fine wine.",
type: "dog",
imageUrl: "http://1.bp.blogspot.com/-VjM0CmtN-vU/T7YX-LXa09I/AAAAAAAADA0/Vt1oGWEG0lw/s1600/sheepdog+border+collie+shakes+off+water+funny+picture+photo+pulling+faces+raspberry+tongue.jpg"
},
{
id: 24,
name: "George",
color: "Brown",
specialSkill: "Participates in karaoke but does not force others to go out to karaoke.",
type: "dog",
imageUrl: "http://www.dogbreedplus.com/dog_breeds/images/basset-hound-4.jpg"
},
{
id: 25,
name: "Salem",
color: "Red",
specialSkill: "Knows the words to 4 rap songs.",
type: "cat",
imageUrl: "http://funbk.s3.amazonaws.com/wp-content/uploads/2016/06/funny-cat-video-which-will-make-you-laugh-louder.jpg"
},
{
id: 26,
name: "Bubba",
color: "Yellow",
specialSkill: "Cleans himself.",
type: "dog",
imageUrl: "https://www.thetrendywhippet.co.uk/wp-content/uploads/2016/11/IMG_1651-600x400.jpg"
},
{
id: 27,
name: "Chloe",
color: "Green",
specialSkill: "Admits he is wrong",
type: "dino",
imageUrl: "https://assets.creationmuseum.org/img/pages/1703-DinoDen-TwoCard.jpg"
},
{
id: 28,
name: "Nala",
color: "Purple",
specialSkill: "Dances when he has to.",
type: "cat",
imageUrl: "https://tailandfur.com/wp-content/uploads/2016/03/40-Scary-and-Funny-Cat-Pictures-8.jpg"
},
{
id: 29,
name: "Oscar",
color: "Green",
specialSkill: "Gives hugs with appropriate pressure and for the right length of time.",
type: "cat",
imageUrl: "http://img.izismile.com/img/img2/20090219/cats_02.jpg"
},
{
id: 30,
name: "Lucy",
color: "Red",
specialSkill: "Doesn’t get weirded out by the word “moist.”",
type: "dino",
imageUrl: "http://lsae2.iypcdn.com/static//modules/uploads/photos/language1/dino-live-22.jpg?119"
}
];
const rootDiv = document.getElementById("root");
for (let i=0; i<pets.length; i++) {
const pet=pets[i];
if (pet.type === 'cat') {
const bootstrapCardString = `
<div class="card">
<h5 class="pet-name">${pet.name.toUpperCase()}</h5>
<div><img src="${pet.imageUrl}" onerror="this.src='images/missingimg.jpeg'" class="img-fluid w-100" alt="a ${pet.type} you need to adopt"></div>
<p class="list-group-item pet-color card">${pet.color}</p>
<p class="list-group-item special-skill card">${pet.specialSkill}</p>
<footer class="catFooter">${pet.type.toUpperCase()}</footer>
<button class="btn btn-danger adopt-me" id="delete--${pets.id}">Please Take Me Home!</button>
</div>
`;
rootDiv.innerHTML+=bootstrapCardString;
} else if (pet.type === 'dog') {
const bootstrapCardString = `
<div class="card">
<h5 class="pet-name">${pet.name.toUpperCase()}</h5>
<div><img src="${pet.imageUrl}" onerror="this.src='images/missingimg.jpeg'" class="img-fluid w-100" alt="a ${pet.type} you need to adopt"></div>
<p class="list-group-item pet-color card">${pet.color}</p>
<p class="list-group-item special-skill card">${pet.specialSkill}</p>
<footer class="dogFooter">${pet.type.toUpperCase()}</footer>
<button class="btn btn-danger adopt-me" id="delete--${pets.id}">Please Take Me Home!</button>
</div>
`;
rootDiv.innerHTML+=bootstrapCardString;
} else if (pet.type === 'dino') {
const bootstrapCardString = `
<div class="card">
<h5 class="pet-name">${pet.name.toUpperCase()}</h5>
<div><img src="${pet.imageUrl}" onerror="this.src='images/missingimg.jpeg'" class="img-fluid w-100" alt="a ${pet.type} you need to adopt"></div>
<p class="list-group-item pet-color card">${pet.color}</p>
<p class="list-group-item special-skill card">${pet.specialSkill}</p>
<footer class="dinoFooter">${pet.type.toUpperCase()}</footer>
<button class="btn btn-danger adopt-me" id="delete--${pets.id}">Please Take Me Home!</button>
</div>
` ;
rootDiv.innerHTML+=bootstrapCardString;
} else {
domString += `
<div class="card">
<h5 class="pet-name">${pet.name.toUpperCase()}</h5>
<div><img src="${pet.imageUrl}" onerror="this.src='images/missingimg.jpeg'" class="img-fluid w-100" alt="a ${pet.type} you need to adopt"></div>
<p class="list-group-item pet-color card">${pet.color}</p>
<p class="list-group-item special-skill card">${pet.specialSkill}</p>
<footer class="defaultFooter">${pet.type.toUpperCase()}</footer>
<button class="btn btn-danger adopt-me" id="delete--${pets.id}">Please Take Me Home!</button>
</div>
`;
}
}
//This shizzzzzzz be so ridiculous
const renderToDom = (divId, htmlToRender) => {
const selectedDiv = document.querySelector(divId);
selectedDiv.innerHTML = htmlToRender;
}; //Renders pets to DOM
const cardsOnDom = (array) => {
let domString = "";
for (const pet of array) {
if (pet.type === 'cat') {
domString += `
<div class="card">
<h5 class="pet-name">${pet.name.toUpperCase()}</h5>
<div><img src="${pet.imageUrl}" onerror="this.src='images/missingimg.jpeg'" class="img-fluid w-100" alt="a ${pet.type} you need to adopt"></div>
<p class="list-group-item pet-color card">${pet.color}</p>
<p class="list-group-item special-skill card">${pet.specialSkill}</p>
<footer class="catFooter">${pet.type.toUpperCase()}</footer>
<button class="btn btn-danger adopt-me" id="delete--${pet.id}">Please Take Me Home!</button>
</div>
`;
} else if (pet.type === 'dog') {
domString += `
<div class="card">
<h5 class="pet-name">${pet.name.toUpperCase()}</h5>
<div><img src="${pet.imageUrl}" onerror="this.src='images/missingimg.jpeg'" class="img-fluid w-100" alt="a ${pet.type} you need to adopt"></div>
<p class="list-group-item pet-color card">${pet.color}</p>
<p class="list-group-item special-skill card">${pet.specialSkill}</p>
<footer class="dogFooter">${pet.type.toUpperCase()}</footer>
<button class="btn btn-danger adopt-me" id="delete--${pet.id}">Please Take Me Home!</button>
</div>
`;
} else if (pet.type === 'dino') {
domString += `
<div class="card">
<h5 class="pet-name">${pet.name.toUpperCase()}</h5>
<div><img src="${pet.imageUrl}" onerror="this.src='images/missingimg.jpeg'" class="img-fluid w-100" alt="a ${pet.type} you need to adopt"></div>
<p class="list-group-item pet-color card">${pet.color}</p>
<p class="list-group-item special-skill card">${pet.specialSkill}</p>
<footer class="dinoFooter">${pet.type.toUpperCase()}</footer>
<button class="btn btn-danger adopt-me" id="delete--${pet.id}">Please Take Me Home!</button>
</div>
`;
} else {
domString += `
<div class="card">
<h5 class="pet-name">${pet.name.toUpperCase()}</h5>
<div><img src="${pet.imageUrl}" onerror="this.src='images/missingimg.jpeg'" class="img-fluid w-100" alt="a ${pet.type} you need to adopt"></div>
<p class="list-group-item pet-color card">${pet.color}</p>
<p class="list-group-item special-skill card">${pet.specialSkill}</p>
<footer class="defaultFooter">${pet.type.toUpperCase()}</footer>
<button class="btn btn-danger adopt-me" id="delete--${pet.id}">Please Take Me Home!</button>
</div>
`;
}
}
renderToDom("#root", domString);
} //Get each card on the DOM
const filter = (array, typeString) => {
const typeArray = []; //Filters terms with specific type
for (const member of array) {
if (member.type === typeString) {
typeArray.push(member);
}
} //Goes through array and adds qualifying member to array
return typeArray;
}
const showCats = document.querySelector(".meow");
const showDogs = document.querySelector(".woof");
const showDino = document.querySelector(".roar");
const showAllPets = document.querySelector(".allPets");
showCats.addEventListener('click', () => {
const kitties = filter(pets, 'cat');
cardsOnDom(kitties);
})
showDogs.addEventListener('click', () => {
const puppies = filter(pets, 'dog');
cardsOnDom(puppies);
})
showDino.addEventListener('click', () => {
const saurus = filter(pets, 'dino');
cardsOnDom(saurus);
})
showAllPets.addEventListener('click', () => {
cardsOnDom(pets);
})
// ADDING THE PETS
// select/target the form on the DOM
const form = document.querySelector('form');
// create a function that grabs all the values from the form
// pushes the new object to the array
// repaints the DOM with the new pet
const createPet = (e) => {
e.preventDefault();
const newPetObj = { //grabs the values
id: pets.length + 1,
name: document.querySelector("#name").value,
color: document.querySelector("#color").value,
specialSkill: document.querySelector("#specialSkill").value,
type: document.querySelector("#type").value.toLowerCase(),
imageUrl: document.querySelector("#imageUrl").value
}
pets.push(newPetObj); // pushes new object
cardsOnDom(pets); // refreshes DOM with new pet
form.reset(); // resets da form
}
// Add an event listener for the form submit
// Runs the function once you click Submit
form.addEventListener('submit', createPet);
// DELETE THE PETS
// Target the root div, create event Listener
const app = document.querySelector("#root");
// Add an event listener to compare clicks
app.addEventListener('click', (e) => {
// check e.target.id includes "delete"
if (e.target.id.includes("delete")) { // target is button
const [, id] = e.target.id.split("--"); // click is e (event)
// add logic to remove from array
const index = pets.findIndex(e => e.id === Number(id));
pets.splice(index, 1); // bubbles
// Reprint the DOM with const app = document.querySelector("#app");th the updated array
cardsOnDom(pets);
}
});
const startApp = () => {
cardsOnDom(pets);
events(); // ALWAYS LAST
}
startApp();