Repository navigation
Expand file tree
/
Copy pathBlogApi.js
More file actions
87 lines (73 loc) · 3.04 KB
/
Copy pathBlogApi.js
File metadata and controls
87 lines (73 loc) · 3.04 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
82
83
84
85
86
87
const urlPost = "http://localhost:3000/posts";
let cardBody = document.querySelector("#cardBody");
fetch(urlPost)
.then(response => response.json())
.then(posts => {
posts.forEach(item => {
cardBody.innerHTML += `<div id="${item.id}" class="col mb-4"><div class="card h-100">
<img src="https://picsum.photos/200/300?random=2" class="card-img-top" alt="randomImg" style="width:100%; >
<div class="card-body active">
<h5 class="card-title">${item.title}</h5>
<div class="btn-group" role="group" aria-label="Basic outlined example">
<button val="${item.id}" type="button" class="btn btn-outline-primary" id="btnDelete">Delete</button>
</div>
</div>
</div>`})
})
const myModal = new bootstrap.Modal("#myModal");
cardBody.addEventListener("click", showModal)
let urlModal;
function showModal(event) {
let id;
if (event.target && event.target.parentElement.matches(".card")) {
myModal.show();
id = event.target.parentElement.parentElement.id;
urlModal = `http://localhost:3000/posts/${id}`;
fetch(urlModal)
.then(res => res.json())
.then(data => loadPost(data));
}else if(event.target.matches("#btnDelete")){
id=event.target.getAttribute("val");
fetch(`http://localhost:3000/posts/${id}`,{
method: 'DELETE',
})
.then(res => res.json())
.then(res => console.log(res))
}
};
function loadPost(data) {
const modalTitle = document.querySelector(".modal-header");
const modalBody = document.querySelector(".modal-body");
modalTitle.innerHTML = `<h4 class="modal-title">${data.title}</h4><button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>`;
modalBody.innerHTML = `<p>${data.body}</p>`;
loadUsers(data.userId);
showComments(data.id);
};
function loadUsers(data){
console.log(data)
let urlModalU = `http://localhost:3000/users/${data}`;
fetch(urlModalU)
.then(res=> res.json())
.then(data => loadEmail(data))
}
function loadEmail(data){
const modalUser = document.querySelector(".modal-user");
modalUser.innerHTML = `<h4>Users</h4><p>${data.username}</p><p>${data.email}</p>`;
};
const btnComments = document.querySelector(".modal-comments");
btnComments.addEventListener("click", loadCommentsbtn);
function loadCommentsbtn (){
let collapse = document.querySelector("#collapseEx");
collapse.classList.toggle("collapse");
};
function showComments(data){
const urlComments = `https://jsonplaceholder.typicode.com/posts/${data}/comments`
const loadCom = document.querySelector(".loading-comments")
fetch(urlComments)
.then(res => res.json())
.then(data => {
loadCom.innerHTML= "";
data.forEach(com => {
loadCom.innerHTML += `<p class="fw-bold">${com.name}</p><p>${com.email}</p><p>${com.body}</p>`}
)}
)};