-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjqcrud.js
111 lines (109 loc) · 3.21 KB
/
jqcrud.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
$(function() {
loadRecipies();
$("#users").on("click", ".btn-outline-danger", handleDelete);
$("#users").on("click", ".btn-outline-warning", handleUpdate);
$("#addBtn").click(addRecipe);
$("#updateSave").click(function() {
var id = $("#updateId").val();
var name = $("#updateName").val();
var email = $("#updateEmail").val();
if(!email.includes(".com") || !email.includes("@"))
{
$("#updateHelpId").html("Incorrect Email, must contain @ and .com");
$("#updateHelpId").css("color","red");
return;
}
$.ajax({
url: "https://jsonplaceholder.typicode.com/users/" + id,
data: { name, email },
method: "PUT",
success: function(response) {
console.log(response);
loadRecipies();
$("#updateModal").modal("hide");
$("#updateHelpId").html("Enter Email to Update");
$("#updateHelpId").css("color","grey");
}
});
});
});
function handleUpdate() {
var btn = $(this);
var parentDiv = btn.closest(".recipe");
let id = parentDiv.attr("data-id");
$.get("https://jsonplaceholder.typicode.com/users/" + id, function(
response
) {
$("#updateId").val(response.id);
$("#updateName").val(response.name);
$("#updateEmail").val(response.email);
$("#updateModal").modal("show");
});
}
function addRecipe() {
var name = $("#title").val();
var email = $("#body").val();
if(name.length < 1)
{
$("#nameHelp").html("Please enter a name");
$("#nameHelp").css("color","red");
return;
}
if(!email.includes(".com") || !email.includes("@"))
{
$("#emailHelp").html("Incorrect Email, must contain @ and .com");
$("#emailHelp").css("color","red");
return;
}
$.ajax({
url: "https://jsonplaceholder.typicode.com/users",
method: "POST",
data: { name, email },
success: function(response) {
console.log(response);
$("#title").val("");
$("#body").val("");
loadRecipies();
$("#addModal").modal("hide");
$("#emailHelp").html("Enter Email");
$("#emailHelp").css("color","grey");
$("#nameHelp").html("Please Name");
$("#nameHelp").css("color","grey");
}
});
}
function handleDelete() {
var btn = $(this);
var parentDiv = btn.closest(".recipe");
let id = parentDiv.attr("data-id");
console.log(id);
$.ajax({
url: "https://jsonplaceholder.typicode.com/users/" + id,
method: "DELETE",
success: function() {
loadRecipies();
}
});
}
function loadRecipies() {
$.ajax({
url: "https://jsonplaceholder.typicode.com/users",
method: "GET",
error: function(response) {
var users = $("#users");
users.html("An Error has occured");
},
success: function(response) {
console.log(response);
var users = $("#users");
users.empty();
for (var i = 0; i < response.length; i++) {
var rec = response[i];
users.append(
`<div class="recipe" data-id="${rec.id}"><h3>${rec.name}</h3><p><button class="btn btn-outline-danger btn-sm float-right">delete</button><button class="btn btn-outline-warning btn-sm float-right">Edit</button> ${rec.email}</p></div>`
);
// users.append("<div><h3>" + rec.title + "</h3></div>");
}
}
});
}