-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.js
700 lines (616 loc) · 27.3 KB
/
services.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
window.onclick = function (event) {
let assign_truck_modal = document.getElementById('assign_truck_modal');
let assign_call_modal = document.getElementById('assign_call_modal');
let create_call_modal = document.getElementById('create_call_modal');
let create_truck_modal = document.getElementById('create_truck_modal');
let update_client_ids_modal = document.getElementById('update_client_ids_modal');
let update_local_client_id_modal = document.getElementById('update_local_client_id_modal');
if (event.target == assign_truck_modal) {
assign_truck_modal.style.display = "none";
} else if (event.target == assign_call_modal) {
assign_call_modal.style.display = "none";
} else if (event.target == create_call_modal) {
create_call_modal.style.display = "none";
} else if (event.target == create_truck_modal) {
create_truck_modal.style.display = "none";
} else if (event.target == update_client_ids_modal) {
update_client_ids_modal.style.display = "none";
} else if (event.target == update_local_client_id_modal) {
update_local_client_id_modal.style.display = "none";
}
}
function checkForClientId() {
let localClientIdBtn = document.getElementById('local_client_id_btn');
if (!getCookie('clientId')) {
localClientIdBtn.classList.remove('btn-success');
localClientIdBtn.classList.add('btn-danger');
} else {
localClientIdBtn.classList.remove('btn-danger');
localClientIdBtn.classList.add('btn-success');
}
};
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function deleteCookie(cname) {
document.cookie = cname + "= ;";
}
function setCookie(cname, cvalue) {
document.cookie = cname + "=" + cvalue + ";";
}
function updateWindowClientId(inputElement) {
console.log(inputElement);
}
function closeModal(modalId) {
let modal = document.getElementById(modalId);
modal.style.display = "none";
}
var domain = 'https://perrystowingserver.herokuapp.com';
if (location.hostname == 'localhost' || location.hostname == '127.0.0.1') {
domain = "http://localhost:8080"
}
function assignTruck(clicker) {
let selector = document.getElementById('truck_selector');
let truckId = selector.options[selector.selectedIndex].value;
let callId = document.getElementById('assign_truck_submit').dataset.callid;
let assign_truck_modal = document.getElementById('assign_truck_modal');
makeRequest('POST', "/calls/assign/" + callId + "/" + truckId).then(() => {
refreshData();
closeModal(assign_truck_modal.id);
});
}
function unAssignTruck(clicker) {
let callId = document.getElementById('assign_truck_submit').dataset.callid;
let assign_truck_modal = document.getElementById('assign_truck_modal');
makeRequest('POST', "/calls/unassign/" + callId).then(() => {
refreshData();
closeModal(assign_truck_modal.id);
});
}
function assignCall(clicker) {
let selector = document.getElementById('call_selector');
let callId = selector.options[selector.selectedIndex].value;
let truckId = document.getElementById('assign_call_submit').dataset.truckid;
let assign_call_modal = document.getElementById('assign_call_modal');
makeRequest('POST', "/calls/assign/" + callId + "/" + truckId).then((call) => {
refreshData();
closeModal(assign_call_modal.id);
});
}
function buildTruckTable() {
makeRequest('GET', "/trucks").then((response) => {
let trucks = JSON.parse(response);
let truck_table_tbody = document.querySelector("#truck_table tbody");
removeChildNodes(truck_table_tbody);
trucks.forEach((truck, index) => {
//create <tr>
let trElement = document.createElement('tr');
trElement.id = "truck_row_" + (index + 1);
//create <td> elements
let tdElement = document.createElement('td');
tdElement.innerText = index + 1;
trElement.appendChild(tdElement);
tdElement = document.createElement('td');
tdElement.innerText = truck['identifier'];
trElement.appendChild(tdElement);
tdElement = document.createElement('td');
tdElement.innerText = truck['driverFirstName'] + ' ' + truck['driverLastName'];
trElement.appendChild(tdElement);
tdElement = document.createElement('td');
tdElement.innerText = truck['truckStatusType'];
trElement.appendChild(tdElement);
tdElement = document.createElement('td');
tdElement.innerText = truck['dropOffLocation'] || 'None';
trElement.appendChild(tdElement);
trElement.appendChild(tdElement);
truck_table_tbody.appendChild(trElement);
let editIcon = document.createElement('i');
editIcon.classList.add('fa');
editIcon.classList.add('fa-pencil-square-o');
editIcon.style.cssFloat = 'right';
editIcon.style.marginRight = '2em';
let span = document.createElement('span');
span.addEventListener('click', () => {
openEditTruckModal(truck['id']);
});
span.style.cursor = "pointer";
span.appendChild(editIcon);
tdElement.appendChild(span);
});
});
}
function removeChildNodes(node) {
while (node.firstChild) {
node.removeChild(node.firstChild);
}
}
function openAssignCallModal(truckId) {
makeRequest('GET', '/calls/available').then((calls) => {
calls = JSON.parse(calls);
let assign_call_modal = document.getElementById('assign_call_modal');
assign_call_modal.style.display = "block";
let call_selector = document.getElementById('call_selector');
call_selector.options.length = 0;
calls.forEach((call) => {
let option = document.createElement("option");
option.value = call['id'];
option.innerText = call['id'] + ": " + call.customer['firstName'] + " " + call.customer['lastName'];
call_selector.appendChild(option);
});
let assign_call_submit = document.getElementById("assign_call_submit")
assign_call_submit.dataset.truckid = truckId;
});
}
function openUpdateLocalClientIdModal(truckId) {
let update_local_client_id_modal = document.getElementById('update_local_client_id_modal');
update_local_client_id_modal.style.display = "block";
document.getElementById('local_client_id').value = getCookie("clientId");
}
function updateLocalClientId() {
let local_client_id = document.getElementById('local_client_id').value;
setCookie('clientId', local_client_id);
closeModal('update_local_client_id_modal');
checkForClientId();
}
function openUpdateClientIdModal() {
makeRequest('GET', '/clients').then((clients) => {
clients = JSON.parse(clients);
let client_id_list = document.getElementById("client_id_list");
removeChildNodes(client_id_list);
let update_client_ids_modal = document.getElementById('update_client_ids_modal');
update_client_ids_modal.style.display = "block";
// build headers
let headers = document.createElement('div');
headers.classList.add('row')
let headerColumn1 = document.createElement('div');
headerColumn1.classList.add('col-sm-1');
headerColumn1.innerText = "ID";
let headerColumn2 = document.createElement('div');
headerColumn2.classList.add('col-sm-5');
headerColumn2.innerText = "Client ID";
let headerColumn3 = document.createElement('div');
headerColumn3.classList.add('col-sm-2');
headerColumn3.innerText = "Role";
let headerColumn4 = document.createElement('div');
headerColumn4.classList.add('col-sm-2');
headerColumn4.innerText = "Action";
headers.appendChild(headerColumn1);
headers.appendChild(headerColumn2);
headers.appendChild(headerColumn3);
headers.appendChild(headerColumn4);
client_id_list.appendChild(headers);
clients.forEach((client) => {
let row = document.createElement('div');
row.classList.add('row');
row.classList.add('client_id_row');
row.id = 'delete_client_id_' + client.id;
let col1 = document.createElement('div');
col1.classList.add('col-sm-1');
col1.innerText = client.id;
let col2Input = document.createElement('input');
col2Input.classList.add('col-sm-5');
col2Input.maxLength = 16;
col2Input.value = client.clientId;
let col3Input = document.createElement('input');
col3Input.classList.add('col-sm-2');
col3Input.maxLength = 2;
col3Input.value = client.role;
let col4Delete = document.createElement('button');
col4Delete.classList.add('col-sm-2');
col4Delete.classList.add('btn-danger');
col4Delete.classList.add('btn');
col4Delete.type = 'button';
col4Delete.innerText = 'Delete';
col4Delete.value = client.id;
col4Delete.onclick = deleteClientId;
row.appendChild(col1);
row.appendChild(col2Input);
row.appendChild(col3Input);
row.appendChild(col4Delete);
client_id_list.appendChild(row);
});
});
}
function deleteClientId(element) {
let clientId = element.currentTarget.value;
makeRequest("GET", "/clients/delete/" + clientId).then(() => {
let client_id_to_delete = document.getElementById('delete_client_id_' + clientId);
if (client_id_to_delete.parentNode) {
client_id_to_delete.parentNode.removeChild(client_id_to_delete);
}
});
}
function createNewClientId() {
let client_id_list = document.getElementById("client_id_list");
let row = document.createElement('div');
row.classList.add('row');
row.classList.add('client_id_row');
row.id = 'delete_client_id_' + 0;
let col1 = document.createElement('div');
col1.classList.add('col-sm-1');
col1.innerText = "";
let col2Input = document.createElement('input');
col2Input.classList.add('col-sm-5');
col2Input.maxLength = 16;
col2Input.value = "";
let col3Input = document.createElement('input');
col3Input.classList.add('col-sm-2');
col3Input.maxLength = 2;
col3Input.value = "";
let col4Delete = document.createElement('button');
col4Delete.classList.add('col-sm-2');
col4Delete.classList.add('btn-danger');
col4Delete.classList.add('btn');
col4Delete.type = 'button';
col4Delete.innerText = 'Delete';
col4Delete.value = 0;
col4Delete.onclick = deleteClientId;
row.appendChild(col1);
row.appendChild(col2Input);
row.appendChild(col3Input);
row.appendChild(col4Delete);
client_id_list.appendChild(row);
}
function updateClientIds() {
let clientIdRows = document.querySelectorAll('.client_id_row');
let clientIdList = [];
clientIdRows.forEach((clientIdRow) => {
let clientId = {};
clientId.clientId = clientIdRow.childNodes[1].value
clientId.role = clientIdRow.childNodes[2].value
clientIdList.push(clientId);
});
let truckId = document.getElementById('assign_call_submit').dataset.truckid;
let assign_call_modal = document.getElementById('assign_call_modal');
makeRequest('POST', "/clients/update", JSON.stringify(clientIdList)).then((call) => {
refreshData();
closeModal('update_client_ids_modal');
});
}
function buildCallsTable() {
makeRequest('GET', "/calls/nonComplete").then((response) => {
let calls = JSON.parse(response);
let call_table_tbody = document.querySelector("#call_table tbody");
removeChildNodes(call_table_tbody);
calls.forEach((call, index) => {
//create <tr>
let trElement = document.createElement('tr');
//create <td> elements
let tdElement = document.createElement('td');
tdElement.innerText = index + 1;
trElement.appendChild(tdElement);
tdElement = document.createElement('td');
tdElement.innerText = call['id'];
trElement.appendChild(tdElement);
tdElement = document.createElement('td');
tdElement.innerText = call.customer['firstName'] + ' ' + call.customer['lastName'];
trElement.appendChild(tdElement);
tdElement = document.createElement('td');
let vehicle = call.customer.vehicle;
tdElement.innerText = vehicle['year'] + ' ' + vehicle['make'] + ' ' + vehicle['model'];
trElement.appendChild(tdElement);
tdElement = document.createElement('td');
tdElement.innerText = call['pickUpLocation'];
trElement.appendChild(tdElement);
tdElement = document.createElement('td');
tdElement.innerText = call['dropOffLocation'];
trElement.appendChild(tdElement);
tdElement = document.createElement('td');
tdElement.classList.add('nowrap');
let callTime = moment.unix(call['insertTime'])
tdElement.innerText = callTime.format('MMM Do h:mm:ss a');
trElement.appendChild(tdElement);
tdElement = document.createElement('td');
tdElement.innerText = call['truckIdentifier'];
trElement.appendChild(tdElement);
tdElement = document.createElement('td');
let assignButton = document.createElement('button');
assignButton.type = 'button';
assignButton.innerText = 'Assign Truck';
assignButton.classList.add('btn', 'btn-primary');
if (call['truckId'] != 0) {
assignButton.classList.remove('btn-primary');
assignButton.classList.add('btn-warning');
assignButton.innerText = 'Re-Assign';
}
assignButton.addEventListener('click', () => {
openAssignTruckModal(call['id']);
});
assignButton.id = "assign_truck_button_" + index;
assignButton.style.cursor = "pointer";
let editIcon = document.createElement('i');
editIcon.classList.add('fa');
editIcon.classList.add('fa-pencil-square-o');
editIcon.style.cssFloat = 'right';
editIcon.style.marginRight = '2em';
let span = document.createElement('span');
span.addEventListener('click', () => {
openEditCallModal(call['id']);
});
span.style.cursor = "pointer";
span.appendChild(editIcon);
tdElement.appendChild(assignButton);
tdElement.appendChild(span);
tdElement.classList.add('nowrap');
trElement.appendChild(tdElement);
call_table_tbody.appendChild(trElement);
});
});
}
function openAssignTruckModal(callId) {
makeRequest('GET', '/trucks/available').then((trucks) => {
trucks = JSON.parse(trucks);
let truck_modal = document.getElementById('assign_truck_modal');
truck_modal.style.display = "block";
let truck_selector = document.getElementById('truck_selector');
truck_selector.options.length = 0;
trucks.forEach((truck) => {
let option = document.createElement("option");
option.value = truck["id"];
option.innerText = truck["identifier"] + ": " + truck['driverFirstName'] + " " + truck['driverLastName'];
truck_selector.appendChild(option);
});
let assign_truck_submit = document.getElementById("assign_truck_submit")
assign_truck_submit.dataset.callid = callId;
});
}
function openCreateCallModal() {
let create_call_modal = document.getElementById('create_call_modal');
document.getElementById('call_id_input').value = "";
document.getElementsByName('customer.firstName')[0].value = "";
document.getElementsByName('customer.lastName')[0].value = "";
document.getElementsByName('customer.phoneNumber')[0].value = "";
document.getElementsByName('customer.priceQuote')[0].value = "";
document.getElementsByName('pickUpLocation')[0].value = "";
document.getElementsByName('dropOffLocation')[0].value = "";
document.getElementsByName('customer.vehicle.make')[0].value = "";
document.getElementsByName('customer.vehicle.model')[0].value = "";
document.getElementsByName('customer.vehicle.year')[0].value = "";
document.getElementsByName('customer.vehicle.color')[0].value = "";
document.getElementsByName('customer.vehicle.licensePlateNumber')[0].value = "";
document.getElementsByName('comment')[0].value = "";
document.getElementsByName('customer.vehicle.keyLocationType')[0].value = 1;
document.getElementsByName('towTruckType')[0].value = 1;
document.getElementsByName('customer.paymentType')[0].value = 1;
document.getElementsByName('callType')[0].value = 1;
document.getElementById('delete_call_submit').style.display = "none";
let create_call_submit = document.getElementById('create_call_submit');
create_call_submit.innerHTML = "<i class='fa fa-check' aria-hidden='true'> </i>Create";
create_call_submit.onclick = createCall;
create_call_modal.style.display = "block";
}
function openEditCallModal(callId) {
makeRequest('GET', '/calls/' + callId).then((callString) => {
let call = JSON.parse(callString);
let create_call_modal = document.getElementById('create_call_modal');
document.getElementById("call_id_input").value = call.id;
document.getElementsByName('customer.firstName')[0].value = call.customer.firstName;
document.getElementsByName('customer.lastName')[0].value = call.customer.lastName;
document.getElementsByName('customer.phoneNumber')[0].value = call.customer.phoneNumber;
document.getElementsByName('customer.priceQuote')[0].value = call.customer.priceQuote.trim();
document.getElementsByName('pickUpLocation')[0].value = call.pickUpLocation;
document.getElementsByName('dropOffLocation')[0].value = call.dropOffLocation;
document.getElementsByName('customer.vehicle.make')[0].value = call.customer.vehicle.make;
document.getElementsByName('customer.vehicle.model')[0].value = call.customer.vehicle.model;
document.getElementsByName('customer.vehicle.year')[0].value = call.customer.vehicle.year;
document.getElementsByName('customer.vehicle.color')[0].value = call.customer.vehicle.color;
document.getElementsByName('customer.vehicle.licensePlateNumber')[0].value = call.customer.vehicle.licensePlateNumber;
document.getElementsByName('comment')[0].value = call.comment.trim();
if (call.towTruckType) {
document.getElementsByName('towTruckType')[0].value = call.towTruckType.id;
} else {
document.getElementsByName('towTruckType')[0].value = 0;
}
if (call.customer.vehicle.keyLocationType) {
document.getElementsByName('customer.vehicle.keyLocationType')[0].value = call.customer.vehicle.keyLocationType.id;
} else {
document.getElementsByName('customer.vehicle.keyLocationType')[0].value = 0;
}
if (call.customer.paymentType) {
document.getElementsByName('customer.paymentType')[0].value = call.customer.paymentType.id;
} else {
document.getElementsByName('customer.paymentType')[0].value = 0;
}
if (call.callType) {
document.getElementsByName('callType')[0].value = call.callType.id;
} else {
document.getElementsByName('callType')[0].value = 0;
}
document.getElementById('delete_call_submit').style.display = "";
let create_call_submit = document.getElementById('create_call_submit');
create_call_submit.innerHTML = "<i class='fa fa-check' aria-hidden='true'> </i>Update";
create_call_submit.onclick = editCall;
create_call_modal.style.display = "block";
});
}
function openCreateTruckModal() {
// console.log('create truck');
let create_truck_modal = document.getElementById('create_truck_modal');
document.getElementById("truck_id_input").value = "";
document.getElementsByName('identifier')[0].value = "";
document.getElementsByName('driverLastName')[0].value = "";
document.getElementsByName('driverFirstName')[0].value = "";
document.getElementById('delete_truck_submit').style.display = "none";
let create_truck_submit = document.getElementById('create_truck_submit');
create_truck_submit.innerHTML = "<i class='fa fa-check' aria-hidden='true'> </i>Create";
create_truck_submit.onclick = createTruck;
create_truck_modal.style.display = "block";
}
function openEditTruckModal(truckId) {
makeRequest('GET', '/trucks/' + truckId).then((truckString) => {
let truck = JSON.parse(truckString);
let create_truck_modal = document.getElementById('create_truck_modal');
document.getElementById("truck_id_input").value = truck.id;
document.getElementsByName('identifier')[0].value = truck.identifier;
document.getElementsByName('driverFirstName')[0].value = truck.driverFirstName;
document.getElementsByName('driverLastName')[0].value = truck.driverLastName;
document.getElementById('delete_truck_submit').style.display = "";
let create_truck_submit = document.getElementById('create_truck_submit');
create_truck_submit.innerHTML = "<i class='fa fa-check' aria-hidden='true'> </i>Update";
create_truck_submit.onclick = editTruck;
create_truck_modal.style.display = "block";
});
}
function serialize(form) {
var json = {};
if (typeof form == 'object' && form.nodeName == "FORM") {
var len = form.elements.length;
for (i = 0; i < len; i++) {
field = form.elements[i];
if (field.name && !field.disabled && field.type != 'file' && field.type != 'reset' && field.type != 'submit' && field.type != 'button') {
if (field.type == 'select-multiple') {
for (j = form.elements[i].options.length - 1; j >= 0; j--) {
if (field.options[j].selected)
s[s.length] = encodeURIComponent(field.name) + "=" + encodeURIComponent(field.options[j].value);
}
} else if ((field.type != 'checkbox' && field.type != 'radio') || field.checked) {
var parsedName = field.name.split(".");
parsedName.reduce((obj, i, index) => {
if (index < parsedName.length - 1) {
if (!obj[i]) {
obj[i] = {};
}
} else {
obj[i] = field.value;
}
return obj[i]
}, json);
}
}
}
}
return json;
}
function createCall() {
let json = serialize(document.getElementById('create_call_form'));
let originalDomForm = document.getElementById('create_call_form')[0];
if (originalDomForm.checkValidity()) {
makeRequest("POST", '/calls/create', JSON.stringify(json)).then(() => {
refreshData();
closeModal('create_call_modal');
});
}
}
function createTruck() {
let json = serialize(document.getElementById('create_truck_form'));
let originalDomForm = document.getElementById('create_truck_form')[0];
if (originalDomForm.checkValidity()) {
makeRequest("POST", '/trucks/create', JSON.stringify(json)).then(() => {
refreshData();
closeModal('create_truck_modal');
});
}
}
function editCall() {
let json = serialize(document.getElementById('create_call_form'));
let originalDomForm = document.getElementById('create_call_form')[0];
if (originalDomForm.checkValidity()) {
makeRequest("POST", '/calls/edit', JSON.stringify(json)).then(() => {
refreshData();
closeModal('create_call_modal');
});
}
}
function editTruck() {
let json = serialize(document.getElementById('create_truck_form'));
let originalDomForm = document.getElementById('create_truck_form')[0];
if (originalDomForm.checkValidity()) {
makeRequest("POST", '/trucks/edit', JSON.stringify(json)).then(() => {
refreshData();
closeModal('create_truck_modal');
});
}
}
function deleteCall() {
let json = serialize(document.getElementById('create_call_form'));
let call_id = document.getElementById("call_id_input").value;
let originalDomForm = document.getElementById('create_call_form')[0];
if (originalDomForm.checkValidity()) {
makeRequest("POST", '/calls/delete/' + call_id).then(() => {
refreshData();
closeModal('create_call_modal');
});
}
}
function deleteTruck() {
let json = serialize(document.getElementById('create_truck_modal'));
let truck_id = document.getElementById("truck_id_input").value;
let originalDomForm = document.getElementById('create_truck_form')[0];
if (originalDomForm.checkValidity()) {
makeRequest("POST", '/trucks/delete/' + truck_id).then(() => {
refreshData();
closeModal('create_truck_modal');
});
}
}
function makeRequest(method, url, data) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
url = domain + url;
xhr.open(method, url);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.withCredentials = true;
xhr.setRequestHeader('Client-Id', getCookie('clientId'))
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send(data);
});
}
let interval;
function createRefreshTimer(duration) {
if (interval) {
clearInterval(interval);
}
let timer = duration;
let minutes;
let seconds;
interval = setInterval(function () {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
document.getElementById("refresh_time").textContent = minutes + "m" + seconds + "s";
if (--timer < 0) {
refreshData();
}
}, 1000);
}
let initializePage = () => {
buildTruckTable();
buildCallsTable();
createRefreshTimer(300);
checkForClientId();
}
let refreshData = () => {
buildTruckTable();
buildCallsTable();
createRefreshTimer(300);
checkForClientId();
}
initializePage();