-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
125 lines (99 loc) · 3.06 KB
/
main.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
let addToy = false;
document.addEventListener("DOMContentLoaded", () => {
const addBtn = document.querySelector("#new-toy-btn");
const toyFormContainer = document.querySelector(".container");
addBtn.addEventListener("click", () => {
// hide & seek with the form
addToy = !addToy;
toyFormContainer.style.display = addToy ? "block" : "none";
});
const form = document.querySelector("form.add-toy-form");
form.addEventListener("submit", (e) => {
e.preventDefault();
const formData = Object.fromEntries(new FormData(e.target));
sentItOut(formData);
});
getData();
});
const getData = async () => {
const response = await fetch("https://toy-tale-backend.onrender.com/toys");
const getToys = await response.json();
getToys.forEach((toy) => createElement(toy));
if (response) {
hideLoader();
}
};
function hideLoader() {
document.getElementById("loading").style.display = "none";
}
const createElement = (toy, addDeleteButton = false) => {
let card = document.createElement("div");
card.classList.add("card");
let h2 = document.createElement("h2");
h2.textContent = toy.name;
let img = document.createElement("img");
img.src = toy.image;
img.classList.add("toy-avatar");
let p = document.createElement("p");
p.textContent = `${toy.likes} Likes`;
let button = document.createElement("button");
button.textContent = "Like ❤️";
button.classList.add("like-btn");
button.addEventListener("click", () => {
p.textContent = `${(toy.likes += 1)} Likes`;
updateLikes(toy.id, toy.likes);
});
// add elements to the dom
card.append(img, h2, p, button /*deleteButton*/);
document.getElementById("main-container").appendChild(card);
if (addDeleteButton) {
// Add a delete button if specified
let deleteButton = document.createElement("button");
deleteButton.textContent = "Delete 🗑️";
deleteButton.classList.add("delete-btn");
deleteButton.addEventListener("click", () => {
card.remove();
deleteToy(toy.id);
});
// Append the delete button to the card
card.appendChild(deleteButton);
}
};
// function for update likes
function updateLikes(id, numberOfNewLikes) {
fetch(`https://toy-tale-backend.onrender.com/toys/${id}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({
likes: numberOfNewLikes,
}),
});
}
function sentItOut(newToy) {
fetch("https://toy-tale-backend.onrender.com/toys", {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({
...newToy,
likes: 0,
}),
})
.then((response) => response.json())
.then((responseToy) => createElement(responseToy, true));
}
// add delete function to delete spacific div from database
function deleteToy(id) {
fetch(`https://toy-tale-backend.onrender.com/toys/${id}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
});
}