-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
124 lines (111 loc) · 3.4 KB
/
script.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
let data = fetch(" https://dog.ceo/api/breeds/list/all");
data
.then((r) => r.json())
.then((res) => {
//console.log("hi", res);
displayBreeds(res);
})
.catch((e) => console.log(e));
let navlist = document.getElementById("navlist");
let subnav = document.getElementById("subnav");
subnav.style.minHeight = "400px";
function displayBreeds(data) {
// console.log(data.message);
let breeds = Object.keys(data.message).filter(
(b) => data.message[b].length > 2
);
// console.log(breeds);
for (let breed of breeds) {
navlist.innerHTML += `<li onclick="displaySubBreeds(event,'${breed}')" >${breed}</li>`;
}
console.log(navlist.children[0]);
navlist.children[0].className = "active";
//console.log(breeds[0]);
displaySubBreeds(null, breeds[0]);
}
function displaySubBreeds(event, breed) {
if (event) {
console.log(event.target);
document.querySelector(".nav-list .active").className = "";
event.target.className = "active";
}
subnav.innerHTML = "";
getSubBreeds(breed)
.then((subbreeds) => {
console.log("subbreeds=", subbreeds);
let count = 1;
for (let sb of subbreeds) {
subnav.innerHTML += `<li onclick="displayImages(event,'${breed}','${sb}')">${sb}</li>`;
if (count++ == 6) break;
}
subnav.children[0].className = "active";
displayImages(null, breed, subbreeds[0]);
})
.catch((error) => {
console.error("Error fetching subbreeds:", error);
});
}
function getSubBreeds(breed) {
return fetch("https://dog.ceo/api/breed/" + breed + "/list")
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then((data) => {
// console.log("Data received:", data);
return data.message; // Extracting the subbreeds from the response
});
}
function displayImages(event, breed, subbreed) {
if (event) {
// console.log(event.target);
document.querySelector(".sub-nav .active").className = "";
event.target.className = "active";
}
fetch(`https://dog.ceo/api/breed/${breed}/${subbreed}/images/random/6`)
.then((res) => res.json())
.then((data) => {
console.log("response", data);
let images = data.message;
let div = document.getElementById("images");
div.innerHTML = `<h1>${breed} - ${subbreed}</h1>`;
let flex = document.createElement("div");
flex.className = "images";
div.append(flex);
for (let i of images) {
let img = document.createElement("img");
img.setAttribute("src", i);
flex.append(img);
}
})
.catch((e) => console.log("Err:", e));
}
/*********************************
function displaySubBreeds(event, breed) {
let subnav = document.getElementById("subnav");
subnav.innerHTML = "";
getSubBreeds(breed)
.then(subbreeds => {
console.log(subbreeds);
subnav.innerHTML += `<li>${breed}</li>`;
})
.catch(error => {
console.error('Error fetching subbreeds:', error);
});
}
function getSubBreeds(breed) {
return fetch("https://dog.ceo/api/breed/" + breed + "/list")
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
console.log("Data received:", data);
return data.message; // Extract the subbreeds from the response
})
}
*/