-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
645 lines (557 loc) · 20.1 KB
/
app.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
const audio = (() => {
var instance = undefined;
var getInstance = () => {
if (!instance) {
instance = new Audio();
instance.autoplay = true;
instance.src = document.getElementById('tombol-musik').getAttribute('data-url');
instance.load();
instance.currentTime = 0;
instance.volume = 1;
instance.muted = false;
instance.loop = true;
}
return instance;
};
return {
play: () => {
getInstance().play();
},
pause: () => {
getInstance().pause();
}
};
})();
const escapeHtml = (unsafe) => {
return unsafe
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
};
const salin = (btn) => {
navigator.clipboard.writeText(btn.getAttribute('data-nomer'));
let tmp = btn.innerHTML;
btn.innerHTML = 'Tersalin';
btn.disabled = true;
setTimeout(() => {
btn.innerHTML = tmp;
btn.disabled = false;
btn.focus();
}, 1500);
};
const timer = () => {
var countDownDate = (new Date(document.getElementById('tampilan-waktu').getAttribute('data-waktu').replace(' ', 'T'))).getTime();
var time = undefined;
var distance = undefined;
time = setInterval(() => {
distance = countDownDate - (new Date()).getTime();
if (distance < 0) {
clearInterval(time);
time = undefined;
return;
}
document.getElementById('hari').innerText = Math.floor(distance / (1000 * 60 * 60 * 24));
document.getElementById('jam').innerText = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
document.getElementById('menit').innerText = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
document.getElementById('detik').innerText = Math.floor((distance % (1000 * 60)) / 1000);
}, 1000);
};
const buka = async () => {
document.getElementById('tombol-musik').style.display = 'block';
audio.play();
AOS.init();
await login();
timer();
};
const play = (btn) => {
if (btn.getAttribute('data-status').toString() != 'true') {
btn.setAttribute('data-status', 'true');
audio.play();
btn.innerHTML = '<i class="fa-solid fa-circle-pause"></i>';
} else {
btn.setAttribute('data-status', 'false');
audio.pause();
btn.innerHTML = '<i class="fa-solid fa-circle-play"></i>';
}
};
const resetForm = () => {
document.getElementById('kirim').style.display = 'block';
document.getElementById('hadiran').style.display = 'block';
document.getElementById('labelhadir').style.display = 'block';
document.getElementById('batal').style.display = 'none';
document.getElementById('kirimbalasan').style.display = 'none';
document.getElementById('idbalasan').value = null;
document.getElementById('balasan').innerHTML = null;
document.getElementById('formnama').value = null;
document.getElementById('hadiran').value = 0;
document.getElementById('formpesan').value = null;
};
const balasan = async (button) => {
button.disabled = true;
let tmp = button.innerText;
button.innerText = 'Loading...';
let id = button.getAttribute('data-uuid').toString();
let token = localStorage.getItem('token') ?? '';
if (token.length == 0) {
alert('Terdapat kesalahan, token kosong !');
window.location.reload();
return;
}
const BALAS = document.getElementById('balasan');
BALAS.innerHTML = renderLoading(1);
document.getElementById('hadiran').style.display = 'none';
document.getElementById('labelhadir').style.display = 'none';
const REQ = {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
}
};
await fetch(document.querySelector('body').getAttribute('data-url') + '/api/comment/' + id, REQ)
.then((res) => res.json())
.then((res) => {
if (res.code == 200) {
document.getElementById('kirim').style.display = 'none';
document.getElementById('batal').style.display = 'block';
document.getElementById('kirimbalasan').style.display = 'block';
document.getElementById('idbalasan').value = id;
BALAS.innerHTML = `
<div class="card-body bg-light shadow p-3 my-2 rounded-4">
<div class="d-flex flex-wrap justify-content-between align-items-center">
<p class="text-dark text-truncate m-0 p-0" style="font-size: 0.95rem;">
<strong>${escapeHtml(res.data.nama)}</strong>
</p>
<small class="text-dark m-0 p-0" style="font-size: 0.75rem;">${res.data.created_at}</small>
</div>
<hr class="text-dark my-1">
<p class="text-dark m-0 p-0" style="white-space: pre-line">${escapeHtml(res.data.komentar)}</p>
</div>`;
}
if (res.error.length != 0) {
if (res.error[0] == 'Expired token') {
alert('Terdapat kesalahan, token expired !');
window.location.reload();
return;
}
alert(res.error[0]);
}
})
.catch((err) => {
resetForm();
alert(err);
});
document.getElementById('ucapan').scrollIntoView({ behavior: 'smooth' });
button.disabled = false;
button.innerText = tmp;
};
const kirimBalasan = async () => {
let nama = document.getElementById('formnama').value;
let komentar = document.getElementById('formpesan').value;
let token = localStorage.getItem('token') ?? '';
let id = document.getElementById('idbalasan').value;
if (token.length == 0) {
alert('Terdapat kesalahan, token kosong !');
window.location.reload();
return;
}
if (nama.length == 0) {
alert('nama tidak boleh kosong');
return;
}
if (nama.length >= 35) {
alert('panjangan nama maksimal 35');
return;
}
if (komentar.length == 0) {
alert('pesan tidak boleh kosong');
return;
}
document.getElementById('batal').disabled = true;
document.getElementById('kirimbalasan').disabled = true;
let tmp = document.getElementById('kirimbalasan').innerHTML;
document.getElementById('kirimbalasan').innerHTML = `<span class="spinner-border spinner-border-sm me-1"></span>Loading...`;
const REQ = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
},
body: JSON.stringify({
nama: nama,
id: id,
komentar: komentar
})
};
let isSuccess = false;
await fetch(document.querySelector('body').getAttribute('data-url') + '/api/comment', REQ)
.then((res) => res.json())
.then((res) => {
if (res.code == 201) {
isSuccess = true;
}
if (res.error.length != 0) {
if (res.error[0] == 'Expired token') {
alert('Terdapat kesalahan, token expired !');
window.location.reload();
return;
}
alert(res.error[0]);
}
})
.catch((err) => {
resetForm();
alert(err);
});
if (isSuccess) {
await ucapan();
document.getElementById(id).scrollIntoView({ behavior: 'smooth' });
resetForm();
}
document.getElementById('batal').disabled = false;
document.getElementById('kirimbalasan').disabled = false;
document.getElementById('kirimbalasan').innerHTML = tmp;
};
const innerCard = (comment) => {
let result = '';
comment.forEach((data) => {
result += `
<div class="card-body border-start bg-light py-2 ps-2 pe-0 my-2 ms-2 me-0" id="${data.uuid}">
<div class="d-flex flex-wrap justify-content-between align-items-center">
<p class="text-dark text-truncate m-0 p-0" style="font-size: 0.95rem;">
<strong>${escapeHtml(data.nama)}</strong>
</p>
<small class="text-dark m-0 p-0" style="font-size: 0.75rem;">${data.created_at}</small>
</div>
<hr class="text-dark my-1">
<p class="text-dark mt-0 mb-1 mx-0 p-0" style="white-space: pre-line">${escapeHtml(data.komentar)}</p>
<button style="font-size: 0.8rem;" onclick="balasan(this)" data-uuid="${data.uuid}" class="btn btn-sm btn-outline-dark rounded-4 py-0">Balas</button>
${innerCard(data.comment)}
</div>`;
});
return result;
};
const renderCard = (data) => {
const DIV = document.createElement('div');
DIV.classList.add('mb-3');
DIV.innerHTML = `
<div class="card-body bg-light shadow p-3 m-0 rounded-4" id="${data.uuid}">
<div class="d-flex flex-wrap justify-content-between align-items-center">
<p class="text-dark text-truncate m-0 p-0" style="font-size: 0.95rem;">
<strong class="me-1">${escapeHtml(data.nama)}</strong>${data.hadir ? '<i class="fa-solid fa-circle-check text-success"></i>' : '<i class="fa-solid fa-circle-xmark text-danger"></i>'}
</p>
<small class="text-dark m-0 p-0" style="font-size: 0.75rem;">${data.created_at}</small>
</div>
<hr class="text-dark my-1">
<p class="text-dark mt-0 mb-1 mx-0 p-0" style="white-space: pre-line">${escapeHtml(data.komentar)}</p>
<button style="font-size: 0.8rem;" onclick="balasan(this)" data-uuid="${data.uuid}" class="btn btn-sm btn-outline-dark rounded-4 py-0">Balas</button>
${innerCard(data.comment)}
</div>`;
return DIV;
};
const renderLoading = (num) => {
let hasil = '';
for (let index = 0; index < num; index++) {
hasil += `
<div class="mb-3">
<div class="card-body bg-light shadow p-3 m-0 rounded-4">
<div class="d-flex flex-wrap justify-content-between align-items-center placeholder-glow">
<span class="placeholder bg-secondary col-5"></span>
<span class="placeholder bg-secondary col-3"></span>
</div>
<hr class="text-dark my-1">
<p class="card-text placeholder-glow">
<span class="placeholder bg-secondary col-6"></span>
<span class="placeholder bg-secondary col-5"></span>
<span class="placeholder bg-secondary col-12"></span>
</p>
</div>
</div>`;
}
return hasil;
}
const pagination = (() => {
const perPage = 10;
var pageNow = 0;
var resultData = 0;
var disabledPrevious = () => {
document.getElementById('previous').classList.add('disabled');
};
var disabledNext = () => {
document.getElementById('next').classList.add('disabled');
};
var buttonAction = async (button) => {
let tmp = button.innerHTML;
button.disabled = true;
button.innerHTML = `<span class="spinner-border spinner-border-sm me-1"></span>Loading...`;
await ucapan();
button.disabled = false;
button.innerHTML = tmp;
document.getElementById('daftarucapan').scrollIntoView({ behavior: 'smooth' });
};
return {
getPer: () => {
return perPage;
},
getNext: () => {
return pageNow;
},
reset: async () => {
pageNow = 0;
resultData = 0;
await ucapan();
document.getElementById('next').classList.remove('disabled');
disabledPrevious();
},
setResultData: (len) => {
resultData = len;
if (resultData < perPage) {
disabledNext();
}
},
previous: async (button) => {
if (pageNow < 0) {
disabledPrevious();
} else {
pageNow -= perPage;
disabledNext();
await buttonAction(button);
document.getElementById('next').classList.remove('disabled');
if (pageNow <= 0) {
disabledPrevious();
}
}
},
next: async (button) => {
if (resultData < perPage) {
disabledNext();
} else {
pageNow += perPage;
disabledPrevious();
await buttonAction(button);
document.getElementById('previous').classList.remove('disabled');
}
}
};
})();
const ucapan = async () => {
const UCAPAN = document.getElementById('daftarucapan');
UCAPAN.innerHTML = renderLoading(pagination.getPer());
let token = localStorage.getItem('token') ?? '';
if (token.length == 0) {
alert('Terdapat kesalahan, token kosong !');
window.location.reload();
return;
}
const REQ = {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
}
};
await fetch(document.querySelector('body').getAttribute('data-url') + `/api/comment?per=${pagination.getPer()}&next=${pagination.getNext()}`, REQ)
.then((res) => res.json())
.then((res) => {
if (res.code == 200) {
UCAPAN.innerHTML = null;
res.data.forEach((data) => UCAPAN.appendChild(renderCard(data)));
pagination.setResultData(res.data.length);
if (res.data.length == 0) {
UCAPAN.innerHTML = `<div class="h6 text-center">Tidak ada data</div>`;
}
}
if (res.error.length != 0) {
if (res.error[0] == 'Expired token') {
alert('Terdapat kesalahan, token expired !');
window.location.reload();
return;
}
alert(res.error[0]);
}
})
.catch((err) => alert(err));
};
const login = async () => {
document.getElementById('daftarucapan').innerHTML = renderLoading(pagination.getPer());
let body = document.querySelector('body');
const REQ = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: body.getAttribute('data-email').toString(),
password: body.getAttribute('data-password').toString()
})
};
await fetch(body.getAttribute('data-url') + '/api/#', REQ)
.then((res) => res.json())
.then((res) => {
if (res.code == 200) {
localStorage.removeItem('token');
localStorage.setItem('token', res.data.token);
ucapan();
}
if (res.error.length != 0) {
alert('Terdapat kesalahan, ' + res.error[0]);
window.location.reload();
return;
}
})
.catch(() => {
alert('Terdapat kesalahan, otomatis reload halaman');
window.location.reload();
return;
});
};
const kirim = async () => {
let nama = document.getElementById('formnama').value;
let hadir = document.getElementById('hadiran').value;
let komentar = document.getElementById('formpesan').value;
let token = localStorage.getItem('token') ?? '';
if (token.length == 0) {
alert('Terdapat kesalahan, token kosong !');
window.location.reload();
return;
}
if (nama.length == 0) {
alert('nama tidak boleh kosong');
return;
}
if (nama.length >= 35) {
alert('panjangan nama maksimal 35');
return;
}
if (hadir == 0) {
alert('silahkan pilih kehadiran');
return;
}
if (komentar.length == 0) {
alert('pesan tidak boleh kosong');
return;
}
document.getElementById('kirim').disabled = true;
let tmp = document.getElementById('kirim').innerHTML;
document.getElementById('kirim').innerHTML = `<span class="spinner-border spinner-border-sm me-1"></span>Loading...`;
const REQ = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
},
body: JSON.stringify({
nama: nama,
hadir: hadir == 1,
komentar: komentar
})
};
await fetch(document.querySelector('body').getAttribute('data-url') + '/api/comment', REQ)
.then((res) => res.json())
.then((res) => {
if (res.code == 201) {
resetForm();
pagination.reset();
}
if (res.error.length != 0) {
if (res.error[0] == 'Expired token') {
alert('Terdapat kesalahan, token expired !');
window.location.reload();
return;
}
alert(res.error[0]);
}
})
.catch((err) => {
resetForm();
alert(err);
});
document.getElementById('kirim').disabled = false;
document.getElementById('kirim').innerHTML = tmp;
};
const progressBar = (() => {
let bar = document.getElementById('bar');
let clear = null;
let second = 1;
let counter = 1;
let untilOneHundred = parseInt(bar.style.width.replace('%', ''));
clear = setInterval(() => {
if (untilOneHundred < 100) {
untilOneHundred = (counter + (untilOneHundred / 10)) / (second + (untilOneHundred / 100));
setNum(untilOneHundred);
} else {
clearInterval(clear);
}
if (counter % 100 == 0) {
second += 1;
}
counter += 1;
}, 10);
let setNum = (num) => {
bar.style.width = num + "%";
bar.innerText = Math.floor(num) + "%";
};
return {
stop: () => {
clearInterval(clear);
setNum(100.0);
}
};
})();
const opacity = () => {
let modal = new Promise((res) => {
let clear = null;
clear = setInterval(() => {
if (document.getElementById('exampleModal').classList.contains('show')) {
clearInterval(clear);
res();
}
}, 100);
});
modal.then(() => {
progressBar.stop();
let op = parseInt(document.getElementById('loading').style.opacity);
let clear = null;
clear = setInterval(() => {
if (op >= 0) {
op -= 0.025;
document.getElementById('loading').style.opacity = op;
} else {
clearInterval(clear);
document.getElementById('loading').remove();
document.getElementById('exampleModal').classList.add('fade');
}
}, 10);
});
};
const modalFoto = (btn) => {
let modal = new bootstrap.Modal('#modalFoto');
let img = document.getElementById('showModalFoto');
img.src = btn.src;
modal.show();
};
window.addEventListener('load', () => {
let modal = new bootstrap.Modal('#exampleModal');
let name = (new URLSearchParams(window.location.search)).get('to') ?? '';
if (name.length == 0) {
document.getElementById('namatamu').remove();
} else {
let div = document.createElement('div');
div.classList.add('m-2');
div.innerHTML = `
<p class="mt-0 mb-1 mx-0 p-0 text-light">Kepada Yth Bapak/Ibu/Saudara/i</p>
<h2 class="text-light">${escapeHtml(name)}</h2>
`;
document.getElementById('formnama').value = name;
document.getElementById('namatamu').appendChild(div);
}
modal.show();
opacity();
}, false);