-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.js
182 lines (146 loc) · 5.08 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
$(document).ready(function () {
const toggle = document.querySelector('.arrow');
const form = document.querySelector('form');
const container = document.querySelector('.form');
var register = $('#register');
var update = $('#update');
var search = $('#search');
let id;
const status = localStorage.getItem('status');
update.hide();
const hide = () => {
$('form').hide();
container.style.height = '70px';
localStorage.setItem('status', 'hidden');
toggle.classList.add('fa-caret-down');
}
const show = () => {
$('form').show();
container.style.height = 'auto';
localStorage.removeItem('status');
toggle.classList.remove('fa-caret-down');
}
const clearFields = () => {
$('form input[type="text"]').val('');
$('form input[type="tel"]').val('');
$('form input[type="number"]').val('');
$('form input[type="email"]').val('');
$('form select').val('');
};
// Get data from database
const get = (choice) => {
// serialize form data
var formData = $("#id").serializeArray();
if(!choice){
formData.push({name: 'id', value: id});
}
const finalUrl = choice ? "http://172.17.100.12:8081/bejerano/IPT-Finals/get.o" : "http://172.17.100.12:8081/bejerano/IPT-Finals/delete.o";
// send AJAX request
$.ajax({
url: finalUrl,
type: 'POST',
data: JSON.stringify(formData),
contentType: 'application/json',
dataType: 'json',
success: function (response) {
if (response.status === "success") {
if (choice) {
var template = Handlebars.compile($("#listitems").html());
var html = template(response);
$("#tbody").html(html);
} else {
$('input[type="text"]').val('');
get(true);
}
} else if (response.status === "error") {
console.log(response.message)
console.log(response.message_additional)
}
},
error: function (xhr, status, error) {
console.log(xhr.responseText);
}
});
}
const post = (choice) => {
var formData = $("#form").serializeArray();
if(!choice){
formData.push({name: 'id', value: id});
}
const finalUrl = choice ? "http://172.17.100.12:8081/bejerano/IPT-Finals/post.o" : "http://172.17.100.12:8081/bejerano/IPT-Finals/update.o";
// send AJAX request
$.ajax({
url: finalUrl,
type: 'POST',
data: JSON.stringify(formData),
contentType: 'application/json',
dataType: 'json',
success: function (response) {
if (response.status === "success") {
console.log(response.message)
console.log(response.data)
clearFields();
get(true);
} else if (response.status === "error") {
console.log(response.message)
console.log(response.message_additional)
}
},
error: function (xhr, status, error) {
console.log(xhr.responseText);
}
});
};
if (status) {
toggle.classList.toggle('fa-caret-down');
hide();
}
get(true);
toggle.addEventListener('click', (e) => {
e.preventDefault();
if (form.style.display === 'none') {
show();
} else {
hide();
}
})
// Search Button
search.click(function (e) {
get(true);
});
// Update Button
update.click(function (e) {
e.preventDefault();
post(false);
clearFields();
update.hide();
register.show();
});
// Register Button
register.click(function (e) {
e.preventDefault();
// serialize form data
post(true);
});
$('body').on('click', '.table .tbody .tr', function (e) {
const list = this.children;
id = list[0].innerHTML;
if (localStorage.getItem('status')) {
show();
}
if(e.target.id === 'delete'){
get(false);
return;
}
register.hide();
update.show();
$('#fullname').val(list[1].innerHTML);
$('#email').val(list[2].innerHTML);
$('#phone').val(list[3].innerHTML);
$('#age').val(list[4].innerHTML);
$('#gender').val(list[5].innerHTML);
$('#municipality').val(list[6].innerHTML);
$('#year').val(list[7].innerHTML);
$('#course').val(list[8].innerHTML);
});
});