Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
105 lines (96 loc) · 2.43 KB
/
Copy pathindex.js
File metadata and controls
105 lines (96 loc) · 2.43 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//GET API
let data;
$.ajax({
url: "https://usman-fake-api.herokuapp.com/api/recipes",
type: "GET",
async: false,
success: function (response) {
data = response;
},
error: function (error) {
console.error("Error:", error);
},
});
const table = document.querySelector(".table-js");
let total = "";
data.forEach((ele) => {
total += `
<tr>
<td>${ele.title}</td>
<td>${ele.body}</td>
<td><button type="button" class="btn btn-outline-primary edit-js" id="${ele._id}"> Edit
</button></td>
<td><button type="button" class="btn btn-outline-primary delete-js" id="${ele._id}"> Delete
</button></td>
</tr>
`;
});
table.innerHTML = total;
//POST API
const add = document.querySelector(".add-js");
add.addEventListener("click", () => {
const title = document.querySelector(".title-js").value;
const recipe = document.querySelector(".recipe-js").value;
const myData = {
title,
body: recipe,
};
$.ajax({
url: "https://usman-fake-api.herokuapp.com/api/recipes",
type: "POST",
contentType: "application/json",
async: false,
data: JSON.stringify(myData),
success: function (response) {
console.log("Success:", response);
},
error: function (error) {
console.error("Error:", error);
},
});
location.reload();
});
//PUT API
$(".edit-js").on("click", function () {
const ID = this.id;
$("#recipeModal").modal("show");
$(".sumbit-js").on("click", () => {
title = $("#title").val();
recipe = $("#recipe").val();
const myData = {
title,
body: recipe,
};
$("#recipeModal").modal("hide");
$.ajax({
url: "https://usman-fake-api.herokuapp.com/api/recipes/" + ID,
type: "PUT",
async: false,
contentType: "application/json",
data: JSON.stringify(myData),
success: function (response) {
console.log("Updated Successfully:", response);
},
error: function (error) {
console.error("Error:", error);
},
});
location.reload();
});
});
//DELETE API
$(".delete-js").on("click", function () {
const ID = this.id;
$.ajax({
url: "https://usman-fake-api.herokuapp.com/api/recipes/" + ID,
type: "DELETE",
async: false,
success: function (response) {
console.log("Success:", response);
},
error: function (error) {
console.error("Error:", error);
},
});
location.reload();
});