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