-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaxoisRequest.js
54 lines (52 loc) · 1.69 KB
/
axoisRequest.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
function getUsersWithAxios(){
return new Promise((resolve,reject) =>{
axios.get("https://jsonplaceholder.typicode.com/users")
.then((response) =>{
let users = response.data;
document.getElementById("users").innerHTML = "";
for (user of users) {
let content = `
<div class="user-box" onclick="userSelect(${user.id},this)">
<h2 class="userName">${user.username}</h2>
<h4 class="userEmail">Email : ${user.email}</h4>
</div>`;
document.getElementById("users").innerHTML += content;
}
resolve()
})
.catch(function (error) {
// handle error
console.log(error);
})
})
}
function getPosts(userId){
axios.get(`https://jsonplaceholder.typicode.com/posts?userId=${userId}`)
.then((response) =>{
let posts = response.data;
document.getElementById("posts").innerHTML = "";
for (post of posts) {
let content = `
<div class="post">
<h3 class="title">${post.title}</h3>
<p class="body">${post.body}</p>
</div>`;
document.getElementById("posts").innerHTML += content;
}
})
.catch((error) =>{
console.log(error)
})
}
function userSelect(id, element) {
getPosts(id);
let eleSelected = document.getElementsByClassName("active");
for (ele of eleSelected) {
ele.classList.remove("active");
}
element.classList.add("active");
}
getUsersWithAxios()
.then(() =>{
getPosts(1);
})