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

Last change on this file since e8999eb was 53765dd, checked in by gjoko kostadinov <gjokokostadinov@…>, 6 months ago

Fix bugs.

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