-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
81 lines (69 loc) · 2.27 KB
/
main.js
File metadata and controls
81 lines (69 loc) · 2.27 KB
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
const inputNums = document.getElementById("numCatsFacts");
const inputNumPhotos = document.getElementById("numCatsphotos");
const submitFactsBtn = document.querySelector(".submit-btn");
const photosBtn = document.getElementById("photos-btn");
const catsPhotos = document.querySelector(".photos");
const appResult = document.getElementById("app-result");
async function getFacts(number) {
const response = await axios.get(
`https://meowfacts.herokuapp.com/?count=${number}`
);
return response.data;
}
async function getPhotos(number) {
const response = await axios.get(
`https://api.thecatapi.com/v1/images/search?limit=${number}`
);
return response.data;
}
submitFactsBtn.addEventListener("click", async () => {
let number = inputNums.value;
if (number === "") {
appResult.innerHTML = "<h3>Tap an input</h3> ";
return;
}
submitFactsBtn.setAttribute("disabled", true);
try {
const { data } = await getFacts(number);
const list = document.createElement("ul");
list.classList.add("result-list");
data.forEach((fact) => {
const listItem = document.createElement("li");
listItem.innerText = fact;
list.appendChild(listItem);
});
appResult.innerHTML = "";
appResult.appendChild(list);
} catch (error) {
appResult.innerHTML = "<h3>something went wrong try again <h3>";
} finally {
submitFactsBtn.removeAttribute("disabled");
}
});
photosBtn.addEventListener("click", async () => {
let number = inputNumPhotos.value;
if (number === "") {
appResult.innerHTML = "<h3>Tap an input</h3> ";
return;
}
photosBtn.setAttribute("disabled", true);
try {
const images = await getPhotos(number);
const div = document.createElement("div");
div.classList.add("result-images");
images.forEach((item) => {
const container = document.createElement("div");
container.classList.add("result-images-container");
const image = document.createElement("img");
image.setAttribute("src", item.url);
container.appendChild(image);
div.appendChild(container);
});
appResult.innerHTML = "";
appResult.appendChild(div);
} catch (error) {
appResult.innerHTML = "<h3>something went wrong try again <h3>";
} finally {
photosBtn.removeAttribute("disabled");
}
});