-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
153 lines (134 loc) · 4.97 KB
/
index.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
//This is the API for the random dog facts that will be appearing towards the top of the page.
// var myHeaders = new Headers();
// myHeaders.append("Content-Type", "application/x-www-form-urlencoded");
let bearerToken = '';
var urlencoded = new URLSearchParams();
urlencoded.append("grant_type", "client_credentials");
urlencoded.append("client_id", "v0i8v5rg1UdfHNvpCqsVXBQdcPYN9wmfI9eZOiDUz3NOUZBHFY");
urlencoded.append("client_secret", "clbO2ocTCF6brZhuxhgQrMJKZtS5eJXleqP9WSdv");
var requestOptionsBearer = {
method: 'POST',
body: urlencoded,
redirect: 'follow'
};
var myHeaders = new Headers();
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
fetch("https://api.petfinder.com/v2/oauth2/token", requestOptionsBearer)
.then(response => response.json())
.then(result => {
bearerToken = result.access_token;
myHeaders.append("Authorization", `Bearer ${bearerToken}`);
fetch(`https://api.petfinder.com/v2/animals?type=dog&sort=-recent`, requestOptions)
.then(response => response.json())
.then(function (data) {
renderDog(data.animals)
});
})
.catch(error => console.log('error', error));
let factNumber = 0;
let dogFact = [];
function renderDogFacts() {
let dogFactHTML = document.getElementById('dogFacts')
axios.get(`https://www.dogfactsapi.ducnguyen.dev/api/v1/facts/?number=1`
// , {
// https://cors-anywhere.herokuapp.com/
// headers: {
// 'Access-Control-Allow-Origin': '*'
// }
// }
).then(response => {
dogFactHTML.innerHTML = response.data.facts;
dogFact.push(response.data.facts);
})
}
function isTheAnimalFixed(currentDog) {
if (currentDog.attributes.spayed_neutered == false) {
return '❌';
} else {
return '✅';
}
};
function isTheAnimalVacced(currentDog) {
if (currentDog.shots_current == false) {
return '❌'
} else {
return '✅';
}
};
//Called function for generating the card for adoptable dogs
function renderDog(animalsArray) {
const dConInner = document.createElement("div")
let dogHTML = animalsArray.map(currentDog => {
// map goes through each element, return creates a new array of each element that is returned
if (currentDog.primary_photo_cropped === null || currentDog.primary_photo_cropped.small === '') {
return `
<div class="col-12 col-md-6 col-lg-3">
<div class="card text-white bg-dark">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQj_vJblJLSn59TSwKbG3-CawQ39uUHDX3vf0nX8q48Mr8anv5aBai_OGk2HrtqlVh1sy0&usqp=CAU" class="card-img-top" alt="...">
<div class="card-body row">
<div class="col-6">
<h5 class="card-title">${currentDog.breeds.primary}</h5>
<p class="card-text">${currentDog.age}</p>
</div>
<div class="col-6">
<p class="card-text">Gender: ${currentDog.gender}</p>
<p class="card-text">Fixed: ${isTheAnimalFixed(currentDog)}</p>
<p class="card-text">Vaccinated:${isTheAnimalVacced(currentDog)}</p>
</div>
<a href="${currentDog.url}" target="_blank" class="btn btn-primary">Adopt Me</a>
</div>
</div>
</div>`
} else {
return `
<div class="col-12 col-md-6 col-lg-3">
<div class="card text-white bg-dark">
<img src="${currentDog.primary_photo_cropped.small}" class="card-img-top" alt="...">
<div class="card-body row">
<div class="col-6">
<h5 class="card-title">${currentDog.breeds.primary}</h5>
<p class="card-text">${currentDog.age}</p>
</div>
<div class="col-6">
<p class="card-text">Gender: ${currentDog.gender}</p>
<p class="card-text">Fixed: ${isTheAnimalFixed(currentDog)}</p>
<p class="card-text">Vaccinated: ${isTheAnimalVacced(currentDog)}</p>
</div>
<a href="${currentDog.url}" target="_blank" class="btn btn-primary">Adopt Me</a>
</div>
</div>
</div>`
}
}).join('');
dogsCon.innerHTML = dogHTML;
}
//This is the API for the Adoptable pets on the page
let dogsCon = document.getElementById('Dogs-Container');
let search = document.getElementById('search-form');
let search_bar = document.getElementById('search-text');
let searchBtn = document.getElementById('search_button');
searchBtn.addEventListener('click', (event) => {
event.preventDefault();
let search_string = search_bar.value;
fetch(`https://api.petfinder.com/v2/animals?type=dog&location=${search_string}`, requestOptions)
.then(response => response.json())
.then(function (data) {
renderDog(data.animals)
});
});
function nextFact() {
factNumber++;
renderDogFacts();
}
function previousFact() {
if (factNumber == 0) {
return;
}
factNumber--;
let dogFactHTML = document.getElementById('dogFacts');
dogFactHTML.innerHTML = dogFact[factNumber];
}