forked from ChefJayPeek/petResume
-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomAjaxCalls.js
95 lines (81 loc) · 3.11 KB
/
randomAjaxCalls.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
const apiKey = "66fa386b-c33c-4825-8091-57c9a02e9aa8";
let $gallery = $('#gallery');
let $progress = $('#progress');
let $progressContainer = $('.progress');
$progressContainer.hide();
/*
Template for each card:
<div class="card">
<div class="card-image wave-effect waves-block wave-light">
<img src="https://cdn2.thedogapi.com/images/r1ifZl5E7_390x256.jpg" alt="placeholder" class="responsive-img activator">
<span class="card-title">American Bully</span>
</div>
<div class="card-reveal white lighten-1">
<span class="card-title grey-text text-darken-4">American Bully<i class="material-icons right">close</i></span>
<p>
<span class="grey-text">Farm dog, Cattle herding</span><br>
Height: 17 - 20 in <br>
Weight: 31 - 46 lbs <br>
Life span: 10 - 13 years <br>
Temperament: Friendly, Energetic, Alert, Loyal, Intelligent, Eager <br>
</p>
</div>
</div>
*/
/**
* calls the dog API and requests a bunch of random breeds with pictures.
*/
function requestRandomDogBreeds(pictureSize = "small", limit = 20) {
$.ajax({
url: `https://api.thedogapi.com/v1/images/search? x-api-key=${apiKey}&size=${pictureSize}&order=random&limit=${limit}&format=json`,
method: "GET"
}).done((response) => {
//empty gallery
$gallery.empty();
$gallery.hide();
$progressContainer.show();
let totalPicCount = 0.0;
let loadedPicCount = 0.0;
for(let data of response) {
let url = data.url;
// noinspection JSUnresolvedVariable
if(data.breeds.length) {
let $card = createDogCard(url, data.breeds[0]);
$gallery.append($card);
totalPicCount++;
$('img', $card).on('load', function () {
loadedPicCount++;
$progress.css('width', loadedPicCount/totalPicCount*100 + '%');
if(loadedPicCount >= totalPicCount) {
$gallery.show();
$progressContainer.hide();
}
})
}
}
});
}
function createDogCard(url, breedData) {
return $(`
<div class="card">
<div class="card-image wave-effect waves-block wave-light">
<img src="${url}" alt="${breedData.name}" class="responsive-img activator">
<span class="card-title activator">${breedData.name}</span>
</div>
<div class="card-reveal white lighten-1">
<span class="card-title grey-text text-darken-4">${breedData.name}<i class="material-icons right">close</i></span>
<p>
<span class="grey-text">${breedData.bred_for}</span><br>
Height: ${breedData.height.imperial} in <br>
Weight: ${breedData.weight.imperial} lbs <br>
Life span: ${breedData.life_span} <br>
Temperament: ${breedData.temperament} <br>
</p>
</div>
</div>
`);
}
$('#randomBtn').click(() => {
requestRandomDogBreeds();
});
requestRandomDogBreeds();