source: src/main/resources/static/js/business_admin.js

Last change on this file was f29cd58, checked in by gjoko kostadinov <gjokokostadinov@…>, 10 months ago

Add services search functionality.

  • Property mode set to 100755
File size: 11.1 KB
Line 
1$(document).ready(function() {
2 var business = {};
3 var serviceTypeList = {};
4
5 getServiceTypes().then(function (serviceTypes) {
6 serviceTypeList = serviceTypes;
7 getBusinessInfo(serviceTypes).then(function (data) {
8 business = data;
9 });
10 });
11
12
13
14 getAppointments();
15
16 $("#add_service").click(function () {
17 var input_service = $("#input_service").val();
18 // clear the input
19 $("#input_service").val('');
20
21 $("#predefined_services_admin_panel").append(
22 '<div class="form-outline mb-4">' +
23 ' <div class="row">' +
24 ' <div class="col-md-6">\n' +
25 ' <input class="form-check-input" type="checkbox" checked value="' + -1 + '" id="' + -1 + '">\n' +
26 ' <label class="form-check-label" for="' + -1 + '">\n' +
27 input_service +
28 ' </label>\n' +
29 ' </div>' +
30 ' <div class="form-outline col-md-2 d-grid">' +
31 ' <input type="text" id="' + -1 + input_service.replace(/\s/g, "") + "duration" + '" class="form-control" placeholder="time" />' +
32 ' </div>' +
33 ' <div class="form-outline col-md-2 d-grid">' +
34 ' <input type="text" id="' + -1 + input_service.replace(/\s/g, "") + "price"+ '" class="form-control" placeholder="price" />' +
35 ' </div>' +
36 ' </div>' +
37 ' <div class="row" style="margin-top:10px;">' +
38 ' <div class="form-outline col-md-10 d-grid">' +
39 ' <input type="text" id="' + -1 + input_service.replace(/\s/g, "") + "description" + '" class="form-control" placeholder="description" />' +
40 ' </div>' +
41 ' </div>' +
42 '</div>');
43 event.preventDefault();
44 });
45
46 $("#update_services_button").click(function () {
47 businesses = [];
48 servicesObj = [];
49 $.each($('#predefined_services_admin_panel input:checked').siblings(), function (index, label) {
50
51 let service = {};
52 var id = $(label).prop('for');
53 var text = $(label).text();
54 var time = $($($(label).parent()).siblings()[0]).children()[0].value;
55 var price = $($($(label).parent()).siblings()[1]).children()[0].value;
56 var description = $($($($($($(label).parent())).parent()).siblings()[0]).children()[0]).children()[0].value;
57
58 var serviceType = {};
59 if (parseInt(id) != -1) {
60 service['id'] = parseInt(id);
61 serviceType['id'] = business['services'].find(obj => obj.id === parseInt(id))['serviceType']['id'];
62 } else {
63
64 }
65 serviceType['name'] = text.trim();
66 service['serviceType'] = serviceType;
67 service['duration'] = parseInt(time);
68 service['price'] = parseInt(price);
69 service['description'] = description;
70 servicesObj.push(service);
71 });
72
73 var servicesForDelete=[];
74 $.each($('#predefined_services_admin_panel input:not(:checked)').siblings(), function (index, label) {
75 let id = $(label).prop('for');
76 let foundService = business['services'].find(obj => obj.id === parseInt(id));
77 if (foundService !== undefined && foundService['serviceStatus'] === 'ACTIVE') {
78 let service1 = {};
79 service1['id'] = id;
80 servicesForDelete.push(service1);
81 }
82 });
83
84
85 updateServices(servicesObj).then(function (response) {
86 getBusinessInfo(serviceTypeList).then(function (data) {
87 business = data;
88 });
89 });
90
91 if (servicesForDelete.length > 0) {
92 deleteServices(servicesForDelete).then(function (response) {
93 getBusinessInfo(serviceTypeList).then(function (data) {
94 business = data;
95 });
96 });
97 }
98
99 event.preventDefault();
100 });
101
102 $("#update_owner_button").click(function() {
103 businesses = [];
104 owner={};
105 owner['firstName'] = $('#firstName').val();
106 owner['lastName'] = $('#lastName').val();
107 owner['email'] = $('#email').val();
108 owner['phoneNumber'] = $('#phoneNumber').val();
109 owner['username'] = $('#username').val();
110
111 business['owner'] = owner;
112
113 businesses.push(business);
114
115 updateBusinessInfo(businesses).then(function() {
116 getBusinessInfo(serviceTypeList).then(function (found) {
117 business = found;
118 });
119 });
120 });
121 event.preventDefault();
122});
123
124function updateBusinessInfo(businessList) {
125 return $.ajax({
126 url: "http://localhost:8080/api/business",
127 type:"PATCH",
128 data: JSON.stringify(businessList),
129 contentType:"application/json; charset=utf-8",
130 dataType: 'text',
131 success: function(succ){
132 alert( "Updates applied successfully" );
133 },
134 error: function(err) {
135 alert(err);
136 }
137 });
138}
139
140function updateServices(serviceList) {
141 return $.ajax({
142 url: "http://localhost:8080/api/service",
143 type:"PATCH",
144 data: JSON.stringify(serviceList),
145 contentType:"application/json; charset=utf-8",
146 dataType: 'text',
147 success: function(succ){
148 alert( "Updates applied successfully" );
149 },
150 error: function(err) {
151 alert(err);
152 }
153 });
154}
155
156function deleteServices(services) {
157 return $.ajax({
158 url: "http://localhost:8080/api/service/delete",
159 type:"POST",
160 data: JSON.stringify(services),
161 contentType:"application/json; charset=utf-8",
162 dataType: 'text',
163 success: function(succ){
164 console.log("services deleted successfully");
165 },
166 error: function(err) {
167 console.error(err);
168 }
169 });
170}
171
172function cancelAppointment(appointmentId) {
173 if (confirm("Are you sure you want to cancel the appointment?")) {
174 $.ajax({
175 url: "http://localhost:8080/api/appointment/" + appointmentId,
176 type:"DELETE"
177 }).success(function (data) {
178 alert("Appointment successfully canceled.")
179 getAppointments();
180 }).error(function (error) {
181 alert("Something went wrong.");
182 });
183 }
184}
185
186function getAppointments() {
187 $.ajax({
188 url: "http://localhost:8080/api/appointment/future/me"
189 }).then(function (data) {
190 var $el = $("#bookings-table-body");
191 $el.empty();
192 $.each(data, function (index, obj) {
193 var element =
194 '<tr>' +
195 ' <th scope="row">' + (parseInt(index) + 1) + '</th>' +
196 ' <td>' + obj['fullName'] + '</td>' +
197 ' <td>' + obj['email'] + '</td>' +
198 ' <td>' + obj['phoneNumber'] + '</td>' +
199 ' <td>' + obj['timePeriod'] + '</td>' +
200 ' <td>' + obj['serviceName'] + '</td>';
201
202 switch (obj['status']) {
203 case 'NEW':
204 element += ' <td><button type="button" class="btn btn-danger" onclick="cancelAppointment(' + obj['appointmentId'] + ')">Cancel appointment</button></td>';
205 break;
206 case 'CANCELLED_BY_CUSTOMER':
207 element += ' <td><button type="button" class="btn btn-secondary" disabled>Cancelled by customer</button></td>';
208 break;
209 case 'CANCELLED_BY_BUSINESS_OWNER':
210 element += ' <td><button type="button" class="btn btn-secondary" disabled>Cancelled by business owner</button></td>';
211 break;
212 }
213 element+='</tr>';
214
215 $el.append(element);
216 });
217 });
218}
219
220function getServiceTypes() {
221 return $.ajax({
222 url: "http://localhost:8080/api/nomenclature/serviceTypes/me"
223 }).then(function (serviceTypes) {
224 return serviceTypes;
225 });
226}
227
228function getBusinessInfo(serviceTypeList) {
229 return $.ajax({
230 url: "http://localhost:8080/api/business/me"
231 }).then(function (business) {
232 var $header = $("#header");
233
234 // header
235 $header.text("Welcome back " + business["owner"]['firstName'] + " " + business['owner']['lastName']);
236
237 // business info
238 if(business['businessStatus'] == 'NEW') {
239 $('#new_business_warning').removeAttr("hidden");
240 }
241 $('#business_status').val(business['businessStatus']);
242 $('#business_type').val(business['businessType']['text']);
243
244 // owner info
245 $('#firstName').val(business['owner']['firstName']);
246 $('#lastName').val(business['owner']['lastName']);
247 $('#phoneNumber').val(business['owner']['phoneNumber']);
248 $('#email').val(business['owner']['email']);
249 $('#username').val(business['owner']['username']);
250
251 // services info
252 var $el = $("#predefined_services_admin_panel");
253 $el.empty();
254 const existingServiceTypeIds = business['services'].map(service => service['serviceType']['id']);
255 const missingAddedServiceTypes = serviceTypeList.filter(serviceType => existingServiceTypeIds.indexOf(serviceType['id']) === -1);
256 //console.log(missingAddedServiceTypes);
257
258 $.each(business['services'], function (index, obj) {
259
260 var element =
261 '<div class=\"form-outline mb-4\">' +
262 ' <div class="row">' +
263 ' <div class="col-md-6">\n' +
264 ' <input class="form-check-input" type="checkbox" ';
265
266 if (obj['serviceStatus'] === 'ACTIVE') {
267 element += 'checked';
268 }
269
270 element += ' value=\"' + obj.id + '\" id=\"' + obj.id + '\">\n' +
271 ' <label class="form-check-label" for=\"' + obj.id + '\">\n' + obj['serviceType'].name + '</label>\n' +
272 ' </div>' +
273 ' <div class=\"form-outline col-md-2 d-grid\">' +
274 ' <input type=\"text\" id=\"' + obj.id + obj['serviceType'].name.replace(/\s/g, "") + "duration" + '\" class=\"form-control\" placeholder="time" value=\"' + obj.duration + '" />' +
275 ' </div>' +
276 ' <div class=\"form-outline col-md-2 d-grid\">' +
277 ' <input type=\"text\" id=\"' + obj.id + obj['serviceType'].name.replace(/\s/g, "") + "price" + '\" class=\"form-control\" placeholder="price" value=\"' + obj.price + '" />' +
278 ' </div>' +
279 ' </div>' +
280 ' <div class="row" style="margin-top:10px;">' +
281 ' <div class="form-outline col-md-10 d-grid">' +
282 ' <input type="text" id="' + obj.id + obj['serviceType'].name.replace(/\s/g, "") + "description" + '" class="form-control" placeholder="description" value=\"' + obj.description + '" />' +
283 ' </div>' +
284 ' </div>' +
285 '</div>';
286
287 $el.append(element);
288 });
289 return business;
290 });
291}
Note: See TracBrowser for help on using the repository browser.