Skip to content

Commit 115d182

Browse files
SweetAlert feature and form validation
1 parent dc7624f commit 115d182

File tree

6 files changed

+124
-161
lines changed

6 files changed

+124
-161
lines changed

css/dashboard.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ div.flexContainer {
175175
#editButton {
176176
border-radius: 3px !important;
177177
width: 5rem !important;
178-
background-color: rgb(240, 212, 88) !important;
178+
background-color: rgb(250, 217, 68) !important;
179179
border-color: white !important;
180180
color: white;
181181
}

css/style.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ a.name {
231231
border-radius: 3px !important;
232232
margin-left: 1rem !important;
233233
width: 5rem !important;
234-
background-color: rgb(240, 212, 88) !important;
234+
background-color: rgb(72, 158, 216) !important;
235235
border-color: white !important;
236236
color: white !important;
237237
}
@@ -241,7 +241,7 @@ a.name {
241241
#viewButton {
242242
border-radius: 3px !important;
243243
width: 5rem !important;
244-
background-color: rgb(132, 240, 122) !important;
244+
background-color: rgb(72, 158, 216) !important;
245245
border-color: white !important;
246246
color: white !important;
247247
}

html/dashboard.html

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
1212
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
1313
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
14+
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script> <!-- sweet alert -->
1415
<link rel="stylesheet" href="../css/bootstrap.css">
1516
<link rel="stylesheet" href="../css/dashboard.css">
1617

@@ -57,7 +58,7 @@ <h4>Records of All animals in the Database</h4><br>
5758
<form>
5859
<div class="dashSearch">
5960
<input type="text" class="form-control" id="editSearch" placeholder="Search for an animal in database">
60-
<button type="button" id="editTab" class="btn btn-primary editSubmit">Submit</button>
61+
<button type="button" id="editTab" class="btn btn-primary editSubmit">Search</button>
6162
</div>
6263
</form>
6364

@@ -81,48 +82,41 @@ <h4>Records of All animals in the Database</h4><br>
8182
<h4 class="containerHeading">Input animal detail below</h4>
8283
<form id="postForm" action="#">
8384
<div class="form-group">
84-
<label for="name">Animal Name
85-
</label> <input type="text" name="name" class="form-control" id="name" placeholder="i.e Tiger">
86-
<br>
85+
<label for="name">Animal Name </label>
86+
<input type="text" name="name" class="form-control" id="name" placeholder="i.e Tiger" required>
8787
</div>
8888

8989
<div class="form-group">
9090
<label for="species">Species
91-
</label> <input type="text" name="species" class="form-control" id="species">
92-
<br>
91+
</label> <input type="text" name="species" class="form-control" id="species" required>
9392
</div>
9493
<div class="form-group">
9594
<label for="family">Family
96-
</label> <input type="text" name="family" class="form-control" id="family">
97-
<br>
95+
</label> <input type="text" name="family" class="form-control" id="family" required>
9896
</div>
9997
<div class="form-group">
10098
<label for="class">Class
101-
</label> <input type="text" name="class" class="form-control" id="class">
102-
<br>
99+
</label> <input type="text" name="class" class="form-control" id="class" required>
103100
</div>
104101
<div class="form-group">
105102
<label for="external">External Link
106103
</label> <input type="text" name="external" class="form-control" id="external">
107-
<br>
108104
</div>
109105
<div class="form-group">
110106
<label for="image">Image Link
111107
</label> <input type="text" name="image" class="form-control" id="image">
112-
<br>
113108
</div>
114109
<div class="form-group">
115110
<label for="category">Does the animals lives in the sea?</label>
116111
<select class="form-control" id="category">
117112
<option>sea</option>
118113
<option>land</option>
119114
</select>
120-
<br>
121115
</div>
122116

123117
<div class="form-group">
124118
<label for="info">Animal description and Info</label>
125-
<textarea class="form-control" name="info" id="info" rows="3"></textarea>
119+
<textarea class="form-control" name="info" id="info" rows="3" required></textarea>
126120
<br>
127121
</div>
128122
<div class="form-group">
@@ -153,8 +147,8 @@ <h5 class="modal-title" id="exampleModalLabel">Edit Animal Details</h5>
153147
<h4 class="containerHeading">Input animal detail below</h4>
154148
<form id="postForm">
155149
<div class="col-sm-8">
156-
<label for="name">Animal Name
157-
</label> <input type="text" class="form-control" id="putName" placeholder="i.e Tiger">
150+
<label for="name">Animal Name</label>
151+
<input type="text" class="form-control" id="putName" placeholder="i.e Tiger" required>
158152
<br>
159153
</div>
160154

@@ -194,7 +188,7 @@ <h4 class="containerHeading">Input animal detail below</h4>
194188

195189
<div class="col-sm-8">
196190
<label for="info">Animal description and Info</label>
197-
<textarea class="form-control" id="putInfo" rows="3"></textarea>
191+
<textarea class="form-control" id="putInfo" rows="3" required></textarea>
198192
<br>
199193
</div>
200194
</form>
@@ -213,9 +207,8 @@ <h4 class="containerHeading">Input animal detail below</h4>
213207

214208

215209
<script src="../javascript/jquery.js"></script>
210+
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.js"></script>
216211
<script src="../javascript/dashboard.js"></script>
217-
218-
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
219212
<script src="../javascript/main.js"></script>
220213

221214
</body>

index.html

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ <h3 class="text">or</h3>
7474
</a>
7575
</div>
7676

77-
78-
79-
8077
<!--Modal here-->
8178
<div class="modal fade" id="exampleModal2" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
8279
aria-hidden="true">

javascript/dashboard.js

Lines changed: 106 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,4 @@
11
$(document).ready(function () {
2-
// //Validation here
3-
// $("#postForm").validate({
4-
// rules : {
5-
// name : {
6-
// required : true
7-
// },
8-
// species : {
9-
// required : true
10-
11-
// },
12-
// family : {
13-
// required : true
14-
15-
// },
16-
// class : {
17-
// required : true
18-
// },
19-
// image : {
20-
// required : true
21-
22-
// },
23-
// info : {
24-
// required : true
25-
26-
// }
27-
// },
28-
// messages: {
29-
// name: "Animal name is required",
30-
// species: "Species is required",
31-
// family: "Animal must belong somewhere",
32-
// class: "Class is required",
33-
// image: "An image link is required",
34-
// info: "Enter some info about this animal"
35-
// },
36-
// submitHandler: function(form) {
37-
// alert("done")
38-
// form.submit();
39-
// }
40-
// });
41-
42-
432
//set current time to dashboard
443
const monthNames = ["January", "February", "March", "April", "May", "June",
454
"July", "August", "September", "October", "November", "December"];
@@ -51,22 +10,34 @@ $(document).ready(function () {
5110
//limit character lenght
5211
function limitChar(string) {
5312
if (string.length > 150) {
54-
return string.substring(0, 150) + '.....';
13+
return string.substring(0, 155) + '.....';
14+
} else {
15+
return string;
5516
}
5617
}
5718

5819
//declaring variable to be used later
59-
let $divAppendEdit = $('#divAppendEdit');
6020
let $divAppendAdmin = $('#divAppendAdmin');
6121

62-
let $name = $('#putName'); //tittle
63-
let $species = $('#putSpecies');
64-
let $family = $('#putFamily');
65-
let $class = $('#putClass');
66-
let $category = $('#putCategory');
67-
let $external = $('#putExternal');
68-
let $image = $('#putImage');
69-
let $info = $('#putInfo');
22+
//for editButton
23+
let $editName = $('#putName'); //tittle
24+
let $editSpecies = $('#putSpecies');
25+
let $editFamily = $('#putFamily');
26+
let $editClass = $('#putClass');
27+
let $editCategory = $('#putCategory');
28+
let $editExternal = $('#putExternal');
29+
let $editImage = $('#putImage');
30+
let $editInfo = $('#putInfo');
31+
32+
//for POST request
33+
let $name = $('#name');
34+
let $species = $('#species');
35+
let $family = $('#family');
36+
let $class = $('#class');
37+
let $category = $("#category");
38+
let $external = $('#external');
39+
let $image = $('#image');
40+
let $info = $('#info');
7041

7142

7243
function addAnimalAdmin(animal) {
@@ -91,6 +62,23 @@ $(document).ready(function () {
9162
`);
9263
}
9364

65+
//Get all animals to admin dashboard
66+
function getAll() {
67+
$.ajax({
68+
type: 'GET',
69+
url: 'http://localhost:3000/animals',
70+
success: function (animals) {
71+
$.each(animals, (i, animal) => {
72+
addAnimalAdmin(animal);
73+
})
74+
},
75+
error: function () {
76+
alert('error loading details');
77+
}
78+
});
79+
}
80+
getAll();
81+
9482
//To search for a specific animal with name
9583
$('#editTab').click(function (e) {
9684
e.preventDefault();
@@ -115,58 +103,108 @@ $(document).ready(function () {
115103
});
116104

117105
//Delete a specific animal
118-
$divAppendEdit.delegate('.remove', 'click', function (e) { //.delete has not been added to the page yet hence
106+
$divAppendAdmin.delegate('.remove', 'click', function (e) { //.delete has not been added to the page yet hence
119107
e.preventDefault();
120108
let $div = $(this).closest('div');
109+
const confirmDelete = confirm("Do you want to Delete this Animal ?")
110+
if (confirmDelete == true) {
111+
$.ajax({
112+
type: 'DELETE',
113+
url: 'http://localhost:3000/animals/' + $(this).attr('data-id'),
114+
success: function () {
115+
$div.fadeOut(300, function () {
116+
$(this).closest('.col-md-4').remove();
117+
//$divAppendAdmin.delegate.remove();
118+
//$divAppendAdmin.empty();
119+
//getAll();
120+
Swal.fire({
121+
position: 'center',
122+
type: 'success',
123+
title: 'Animal deleted from the database',
124+
showConfirmButton: false,
125+
timer: 1500
126+
})
127+
})
128+
},
129+
error: function () {
130+
alert('Error deleting animal');
131+
}
132+
})
133+
}
134+
})
121135

136+
//Edit animal detail
137+
$('#saveEdit').on("click", function (e) {
138+
e.preventDefault();
139+
$('#postForm').validate();
140+
141+
const animal = {
142+
name: $editName.val(),
143+
species: $editSpecies.val(),
144+
family: $editFamily.val(),
145+
class: $editClass.val(),
146+
category: $editCategory.find(":selected").text(),
147+
external: $editExternal.val(),
148+
image: $editImage.val(),
149+
info: $editInfo.val()
150+
};
151+
152+
//$('#saveEdit').trigger("reset");
122153
$.ajax({
123-
type: 'DELETE',
124-
url: 'http://localhost:3000/animals/' + $(this).attr('data-id'),
154+
type: 'PUT',
155+
url: 'http://localhost:3000/animals/' + $("#animalId").val(),
156+
data: animal,
125157
success: function () {
126-
$div.fadeOut(300, function () {
127-
$(this).remove();
128-
})
158+
getAll();
129159
},
130160
error: function () {
131-
alert('Error deleting animal');
161+
alert('Error saving animal details')
132162
}
133163
})
134164
})
135165

136-
//Edit animal detail
137-
$('#saveEdit').on("click", function (e) {
166+
//POST new animal to Database
167+
$('#postForm').on('submit', function (e) {
138168
e.preventDefault();
139169

140170
const animal = {
141171
name: $name.val(),
142172
species: $species.val(),
143173
family: $family.val(),
144174
class: $class.val(),
145-
category: $category.find(":selected").text(),
175+
category: $category.val(),
146176
external: $external.val(),
147177
image: $image.val(),
148178
info: $info.val()
149179
};
150180

151-
//$('#saveEdit').trigger("reset");
181+
$('#postForm').trigger("reset");
152182
$.ajax({
153-
type: 'PUT',
154-
url: 'http://localhost:3000/animals/' + $("#animalId").val(),
183+
type: 'POST',
184+
url: 'http://localhost:3000/animals',
155185
data: animal,
156-
success: function () {
186+
success: function (newAnimal) {
187+
addAnimalAdmin(newAnimal);
188+
Swal.fire({
189+
position: 'center',
190+
type: 'success',
191+
title: 'Animal saved to database',
192+
showConfirmButton: false,
193+
timer: 1500
194+
})
157195
},
158196
error: function () {
159-
alert('Error saving animal details')
160-
}
197+
alert('Error saving animal to database')
198+
},
161199
})
162200
})
163201

202+
164203
//logOut user
165204
$('#log-out').click(function (e) {
166205
e.preventDefault();
167206
localStorage.removeItem("password");
168207
localStorage.removeItem("username")
169208
window.location.replace('../index.html')
170209
});
171-
172210
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy