source: src/main/resources/static/js/customer_admin.js@ 77205be

Last change on this file since 77205be was 77205be, checked in by gjoko kostadinov <gjokokostadinov@…>, 9 months ago

Add entire code

  • Property mode set to 100644
File size: 9.2 KB
Line 
1$(document).ready(function() {
2
3 getCustomerInfo();
4
5 getFutureAppointments();
6
7 getPastAppointments();
8
9 getReviewsForCustomer();
10
11 $("#update_customer_button").click(function() {
12 var customerForSave = customer;
13 customerForSave['firstName'] = $('#firstName').val();
14 customerForSave['lastName'] = $('#lastName').val();
15 customerForSave['email'] = $('#email').val();
16 customerForSave['phoneNumber'] = $('#phoneNumber').val();
17 customerForSave['username'] = $('#username').val();
18
19 console.log(JSON.stringify(customerForSave));
20
21 $.ajax({
22 url: "http://localhost:8080/api/customer",
23 type:"PATCH",
24 data: JSON.stringify(customerForSave),
25 contentType:"application/json; charset=utf-8",
26 dataType: 'text',
27 success: function(succ){
28 alert( "Updates applied successfully" );
29 },
30 error: function(err) {
31 alert(err);
32 }
33 });
34 });
35
36
37 $("#post-review-button").click(function () {
38 var ratingRadios = document.getElementsByName('rating');
39 var rating = 0;
40 for (i = 0; i < ratingRadios.length; i++) {
41 if (ratingRadios[i].checked) {
42 rating = ratingRadios[i].value;
43 }
44 }
45 var id = $('.id-class').attr('id') ;
46 var message = $('#message-text').val();
47 postAppointmentReview(id, rating, message);
48 });
49
50 $('#reviewModal').on('show.bs.modal', function (event) {
51 // Button that triggered the modal
52 const button = event.relatedTarget
53 // Extract info from data-bs-* attributes
54 const recipientName = button.getAttribute('data-bs-fullName');
55 const recipientId = button.getAttribute('data-bs-id');
56
57 console.log(recipientName + ' ' + recipientId);
58 // If necessary, you could initiate an Ajax request here
59 // and then do the updating in a callback.
60
61 // Update the modal's content.
62 const modalTitle = reviewModal.querySelector('.modal-title');
63 const modalIdDiv = reviewModal.querySelector('.id-class');
64 const modalBodyInput = reviewModal.querySelector('.modal-body input');
65
66 modalTitle.textContent = `Review for ${recipientName}`;
67
68 modalIdDiv.id = recipientId;
69 modalBodyInput.value = recipientName;
70 })
71});
72
73function goToHomePage() {
74 window.location = "/homepage";
75}
76
77function getCustomerInfo() {
78 $.ajax({
79 url: "http://localhost:8080/api/customer/me"
80 }).success(function (customer) {
81 var $header = $("#fullName");
82 $header.text(customer['firstName'] + " " + customer['lastName']);
83
84 // customer info
85 $('#firstName').val(customer['firstName']);
86 $('#lastName').val(customer['lastName']);
87 $('#phoneNumber').val(customer['phoneNumber']);
88 $('#email').val(customer['email']);
89 $('#username').val(customer['username']);
90 }).error(function (error) {
91 console.log(JSON.stringify(error));
92 });
93}
94
95function getFutureAppointments(){
96 $.ajax({
97 url: "http://localhost:8080/api/appointment/future/me"
98 }).then(function (data) {
99 console.log(data);
100 var $el = $("#appointments-table-body");
101 $el.empty();
102 $.each(data, function (index, obj) {
103 var element =
104 '<tr>' +
105 ' <th scope="row">' + (parseInt(index) + 1) + '</th>' +
106 ' <td>' + obj['fullName'] + '</td>' +
107 ' <td>' + obj['email'] + '</td>' +
108 ' <td>' + obj['phoneNumber'] + '</td>' +
109 ' <td>' + obj['timePeriod'] + '</td>' +
110 ' <td>' + obj['serviceName'] + '</td>';
111
112 switch (obj['status']) {
113 case 'NEW':
114 element += ' <td><button type="button" class="btn btn-danger" onclick="cancelAppointment(' + obj['appointmentId'] + ')">Cancel appointment</button></td>';
115 break;
116 case 'CANCELLED_BY_CUSTOMER':
117 element += ' <td><button type="button" class="btn btn-secondary" disabled>Cancelled by customer</button></td>';
118 break;
119 case 'CANCELLED_BY_BUSINESS_OWNER':
120 element += ' <td><button type="button" class="btn btn-secondary" disabled>Cancelled by business owner</button></td>';
121 break;
122 }
123 element+='</tr>';
124
125 $el.append(element);
126 });
127 });
128}
129
130function getPastAppointments(){
131 $.ajax({
132 url: "http://localhost:8080/api/appointment/past/me"
133 }).then(function (data) {
134 var $el = $("#past-appointments-table-body");
135 $el.empty();
136 $.each(data, function (index, obj) {
137 var element=
138 '<tr>' +
139 ' <th scope="row">' + (parseInt(index) + 1) + '</th>' +
140 ' <td>' + obj['fullName'] + '</td>' +
141 ' <td>' + obj['email'] + '</td>' +
142 ' <td>' + obj['phoneNumber'] + '</td>' +
143 ' <td>' + obj['timePeriod'] + '</td>' +
144 ' <td>' + obj['serviceName'] + '</td>';
145
146 switch (obj['status']) {
147 case 'NEW':
148 element += '<td><button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#reviewModal" data-bs-id="' + obj['appointmentId'] + '" data-bs-fullName="' + obj['fullName'] + '">Leave a review</button></td>';
149 break;
150 case 'CANCELLED_BY_CUSTOMER':
151 element += ' <td><button type="button" class="btn btn-secondary" disabled>Cancelled by you</button></td>';
152 break;
153 case 'CANCELLED_BY_BUSINESS_OWNER':
154 element += ' <td><button type="button" class="btn btn-secondary" disabled>Cancelled by business owner</button></td>';
155 break;
156 case 'FINISHED_AND_REVIEWED_BY_USER':
157 element += ' <td><button type="button" class="btn btn-success" disabled>Already reviewed</button></td>';
158 break;
159 }
160 element+='</tr>';
161
162 $el.append(element);
163 });
164 });
165}
166function generateStars(number) {
167 return '☆'.repeat(number);
168}
169
170function generateBackgroundColor(number) {
171 var color = '';
172 switch (number) {
173 case 5:
174 color = 'text-bg-primary';
175 break;
176 case 4:
177 color = 'text-bg-success';
178 break;
179 case 3:
180 color = 'text-bg-secondary';
181 break;
182 case 2:
183 color = 'text-bg-warning';
184 break;
185 case 1:
186 color = 'text-bg-danger';
187 break;
188 }
189 return color;
190}
191
192function getReviewsForCustomer(){
193 $.ajax({
194 url: "http://localhost:8080/api/review/me"
195 }).then(function (data) {
196 console.log(data);
197 var $el = $("#my-reviews");
198 $el.empty();
199
200 $.each(data, function (index, obj) {
201 var element = '<div class="card m-3" style="max-width: 300px; padding: 0;">';
202 element += '<div class="card-header ' + generateBackgroundColor(obj['rating']) + '">' + generateStars(obj['rating']) + '</div>';
203 element += '<ul class="list-group list-group-flush">';
204 element += '<li class="list-group-item"><i><b>Business:</b></i> ' + obj['businessName'] + '</li>';
205 element += '<li class="list-group-item"><i><b>Service:</b></i> ' + obj['serviceName'] + '</li>';
206 element += '<li class="list-group-item"><i><b>Reviewer:</b></i> ' + obj['customerName'] + '</li>';
207 element += '<li class="list-group-item"><i><b>Comment:</b></i> ' + obj['comment'] + '</li>';
208 element += '<li class="list-group-item"><small class="text-body-secondary"><i>Created:</i> ' + obj['created'] + '</small></li>';
209 element += '</ul>';
210 element += '</div>';
211
212 $el.append(element);
213 });
214 });
215}
216
217function postAppointmentReview(appointmentId, rating, review) {
218 var appointment = {};
219 var ratingObj = {};
220 appointment['id'] = parseInt(appointmentId);
221 ratingObj['appointment'] = appointment;
222 ratingObj['rating'] = parseInt(rating);
223 ratingObj['comment'] = review;
224
225 $.ajax({
226 url: "http://localhost:8080/api/review",
227 type:"POST",
228 data: JSON.stringify(ratingObj),
229 contentType:"application/json; charset=utf-8",
230 dataType: 'text',
231 success: function(succ){
232 alert("Successful appointment!");
233 $('#reviewModal').modal('toggle');
234 getPastAppointments();
235 },
236 error: function(error) {
237 alert(error.responseText);
238 $('#reviewModal').modal('toggle');
239 }
240 });
241}
242
243function cancelAppointment(appointmentId) {
244 if (confirm("Are you sure you want to cancel the appointment?")) {
245 $.ajax({
246 url: "http://localhost:8080/api/appointment/" + appointmentId,
247 type:"DELETE"
248 }).success(function (data) {
249 alert("Appointment successfully canceled.")
250 getAppointments();
251 }).error(function (error) {
252 alert("Something went wrong.");
253 });
254 }
255}
256
Note: See TracBrowser for help on using the repository browser.