-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
555 lines (443 loc) · 18.3 KB
/
script.js
File metadata and controls
555 lines (443 loc) · 18.3 KB
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
// INI UNTUK INDEX
// Isi dengan token jwt
const token = '';
// Fungsi ini berguna untuk inputan automatis Uppercase
function toUpperCase(input) {
input.value = input.value.toUpperCase();
}
// Fungsi Membuka side section
document.addEventListener('alpine:init', () => {
Alpine.data('layout', () => ({
profileOpen: false,
asideOpen: true,
}));
});
// Ini untuk fatch data banyak order per status (tampilkan di card paling atas)
const apiUrlOrderCount = 'https://thrivein-api-v1-0-0-sxbz2gldiq-et.a.run.app/order-count';
document.addEventListener('DOMContentLoaded', async function () {
try {
const responseOrderCount = await fetch(apiUrlOrderCount, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
}
});
if (!responseOrderCount.ok) {
throw new Error(`Network response was not ok (${responseOrderCount.status} ${responseOrderCount.statusText})`);
}
const orderCountData = await responseOrderCount.json();
// Display hasil data fetch
const baruElement = document.getElementById('baruCount');
baruElement.textContent = orderCountData.baru;
const diprosesElement = document.getElementById('diprosesCount');
diprosesElement.textContent = orderCountData.diproses;
const selesaiElement = document.getElementById('selesaiCount');
selesaiElement.textContent = orderCountData.selesai;
} catch (error) {
console.error('Error fetching data:', error);
}
});
// Ini untuk Fatch data banyaknya user (tampilkan di card paling atas)
const apiUrlUserCount = 'https://thrivein-api-v1-0-0-sxbz2gldiq-et.a.run.app/user-count';
document.addEventListener('DOMContentLoaded', async function () {
try {
const responseUserCount = await fetch(apiUrlUserCount, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
}
});
if (!responseUserCount.ok) {
throw new Error(`Network response was not ok (${responseUserCount.status} ${responseUserCount.statusText})`);
}
const userCountData = await responseUserCount.json();
const totalUserElement = document.getElementById('totalUser');
totalUserElement.textContent = userCountData.user;
} catch (error) {
console.error('Error fetching data:', error);
}
});
//Fetching data order (tampilkan di tabel order)
const apiUrlOrder = 'https://thrivein-api-v1-0-0-sxbz2gldiq-et.a.run.app/order';
//token
const fetchDataOrder = async (page, size) => {
const response = await fetch(`${apiUrlOrder}?page=${page}&size=${size}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
}
});
if (!response.ok) {
throw new Error(`Network response was not ok (${response.status} ${response.statusText})`);
}
const data = await response.json();
return data.order_data;
};
//rander table order
const renderTableOrder = (orders) => {
const tableBody = document.getElementById('orderTableBody');
tableBody.innerHTML = '';
orders.forEach(order => {
const row = document.createElement('tr');
row.innerHTML = `
<td class="px-4 py-2">${order.order_id}</td>
<td class="px-4 py-2">${order.name}</td>
<td class="px-4 py-2">${order.title}</td>
<td class="px-4 py-2">${new Date(order.transaction_date).toLocaleString()}</td>
<td class="px-4 py-2">${order.status}</td>
`;
tableBody.appendChild(row);
});
};
//rander dan tampilkan pagination
const renderPaginationOrder = (currentPage, totalPages) => {
const paginationContainer = document.getElementById('paginationContainerOrder');
paginationContainer.innerHTML = '';
for (let i = 1; i <= totalPages; i++) {
const pageLink = document.createElement('a');
pageLink.href = '#';
pageLink.textContent = i;
pageLink.classList.add('px-4', 'py-2', 'text-blue-500', 'cursor-pointer');
pageLink.addEventListener('click', async () => {
const orders = await fetchDataOrder(i, 10); // Ganti 10 dengan ukuran halaman yang sesuai
renderTable(orders);
});
if (i === currentPage) {
pageLink.classList.add('bg-blue-500', 'text-white');
}
paginationContainer.appendChild(pageLink);
}
};
const initOrder = async () => {
const currentPage = 1;
const pageSize = 10; // Ganti dengan ukuran halaman yang sesuai
const orders = await fetchDataOrder(currentPage, pageSize);
renderTableOrder(orders);
// Dummy total pages, you should replace this with the actual total pages from your API response
const totalPages = 5;
renderPaginationOrder(currentPage, totalPages);
};
initOrder();
//Order Manager Page
async function cariOrderData() {
const orderId = document.getElementById('id').value;
try {
const apiUrl = `https://thrivein-api-v1-0-0-sxbz2gldiq-et.a.run.app/order/${orderId}`;
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
}
});
if (!response.ok) {
throw new Error(`Network response was not ok (${response.status} ${response.statusText})`);
}
const data = await response.json();
// Set nilai masing-masing input berdasarkan hasil pencarian
document.getElementById('namaUser').value = data.name || '';
document.getElementById('namaOrder').value = data.title || '';
document.getElementById('waktu').value = data.transaction_date || '';
document.getElementById('status').value = data.status || '';
} catch (error) {
console.error('Error fetching data:', error);
}
}
async function editOrderData() {
const orderId = document.getElementById('id').value;
const status = document.getElementById('status').value;
const apiUrl = `https://thrivein-api-v1-0-0-sxbz2gldiq-et.a.run.app/order-progress/${orderId}`;
try {
const response = await fetch(apiUrl, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({
status: status
})
});
if (!response.ok) {
throw new Error(`Network response was not ok (${response.status} ${response.statusText})`);
}
console.log('Order data updated successfully!');
} catch (error) {
console.error('Error updating data:', error);
}
}
//Fatching Data banner (tampilkan di tabel banners)
document.addEventListener('DOMContentLoaded', async function () {
const apiUrl = 'https://thrivein-api-v1-0-0-sxbz2gldiq-et.a.run.app/banners';
try {
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
}
});
if (!response.ok) {
throw new Error(`Network response was not ok (${response.status} ${response.statusText})`);
}
const data = await response.json();
const tableBody = document.getElementById('bannerTableBody');
data.banners.forEach(banner => {
const row = document.createElement('tr');
const idCell = document.createElement('td');
idCell.textContent = banner.id;
row.appendChild(idCell);
const bannerImgCell = document.createElement('td');
bannerImgCell.textContent = banner.banner_url;
row.appendChild(bannerImgCell);
const bannerTxtCell = document.createElement('td');
bannerTxtCell.textContent = banner.banner_txt;
row.appendChild(bannerTxtCell);
const titleCell = document.createElement('td');
titleCell.textContent = banner.title;
row.appendChild(titleCell);
tableBody.appendChild(row);
});
} catch (error) {
console.error('Error fetching data:', error);
}
});
//Banner Manager content Page
async function updateBannerData() {
const id_banners = document.getElementById('id_banners').value;
const bannerUrl = document.getElementById('bannerUrl_banners').value;
const bannerText = document.getElementById('bannerText_banners').value;
const title = document.getElementById('title_banners').value;
try {
const apiUrl = `https://thrivein-api-v1-0-0-sxbz2gldiq-et.a.run.app/banners/${id_banners}`;
const response = await fetch(apiUrl, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
},
body: JSON.stringify({
banner_txt: bannerText,
banner_url: bannerUrl,
title: title,
}),
});
if (!response.ok) {
throw new Error(`Network response was not ok (${response.status} ${response.statusText})`);
}
console.log('Banner data updated successfully!');
} catch (error) {
console.error('Error updating data:', error);
}
}
//fetching data Articles (tampilkan di tabel articles)
document.addEventListener('DOMContentLoaded', async function () {
const apiUrl = 'https://thrivein-api-v1-0-0-sxbz2gldiq-et.a.run.app/articles';
const pageSize = 10;
const fetchDataArticle = async (page, size) => {
const response = await fetch(`${apiUrl}?page=${page}&size=${size}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
}
});
if (!response.ok) {
throw new Error(`Network response was not ok (${response.status} ${response.statusText})`);
}
const data = await response.json();
return data.articles;
};
const renderTableArticle = (articles) => {
const tableBody = document.getElementById('articleTableBody');
tableBody.innerHTML = ''; // Clear existing content
const truncateText = (text, maxLength) => {
return text.length > maxLength ? text.slice(0, maxLength) + '...' : text;
};
articles.forEach(article => {
const row = document.createElement('tr');
const idCell = document.createElement('td');
idCell.textContent = article.article_id;
row.appendChild(idCell);
const titleCell = document.createElement('td');
titleCell.textContent = article.title;
row.appendChild(titleCell);
const contentCell = document.createElement('td');
// Truncate content to a certain length (e.g., 100 characters)
contentCell.textContent = truncateText(article.content, 100);
row.appendChild(contentCell);
const bannerUrlCell = document.createElement('td');
bannerUrlCell.textContent = article.banner_url;
row.appendChild(bannerUrlCell);
const uploadedDateCell = document.createElement('td');
uploadedDateCell.textContent = article.uploaded_date;
row.appendChild(uploadedDateCell);
tableBody.appendChild(row);
});
};
const renderPaginationArticle = async (currentPage, totalPages) => {
const paginationContainer = document.getElementById('paginationContainerArticles');
paginationContainer.innerHTML = '';
for (let i = 1; i <= totalPages; i++) {
const pageLink = document.createElement('a');
pageLink.href = '#';
pageLink.textContent = i;
pageLink.classList.add('px-4', 'py-2', 'text-blue-500', 'cursor-pointer');
pageLink.addEventListener('click', async () => {
const articles = await fetchDataArticle(i, pageSize);
renderTableArticle(articles);
});
if (i === currentPage) {
pageLink.classList.add('bg-blue-500', 'text-white');
}
paginationContainer.appendChild(pageLink);
}
};
const initArticle = async () => {
const currentPage = 1;
const totalArticles = await fetchDataArticle(currentPage, pageSize);
const totalPages = 5
const articles = await fetchDataArticle(currentPage, pageSize);
renderTableArticle(articles);
renderPaginationArticle(currentPage, totalPages);
};
initArticle();
});
//Articles Manageger Content post Page
const postArticle = async () => {
const apiUrl = 'https://thrivein-api-v1-0-0-sxbz2gldiq-et.a.run.app/post-article';
const bannerUrl = document.getElementById('bannerUrlArticle').value;
const content = document.getElementById('bannerTextArticle').value;
const title = document.getElementById('titleArticle').value;
try {
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
},
body: JSON.stringify({
banner_url: bannerUrl,
content: content,
title: title,
}),
});
if (!response.ok) {
throw new Error(`Network response was not ok (${response.status} ${response.statusText})`);
}
// Assuming you want to do something with the successful response, you can handle it here
const result = await response.json();
console.log('Article posted successfully:', result);
} catch (error) {
console.error('Error posting article:', error);
}
};
//Fatching data services (tampilkan di tabel services)
document.addEventListener('DOMContentLoaded', async function () {
const apiUrl = 'https://thrivein-api-v1-0-0-sxbz2gldiq-et.a.run.app/getservice/all';
try {
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
},
});
if (!response.ok) {
throw new Error(`Network response was not ok (${response.status} ${response.statusText})`);
}
const data = await response.json();
const services = data['list-services'];
renderServicesTable(services);
} catch (error) {
console.error('Error fetching services data:', error);
}
});
const renderServicesTable = (services) => {
const tableBody = document.getElementById('servicesTableBody');
services.forEach(service => {
const row = document.createElement('tr');
const serviceIdCell = document.createElement('td');
serviceIdCell.textContent = service.service_id;
row.appendChild(serviceIdCell);
const categoryCell = document.createElement('td');
categoryCell.textContent = service.category;
row.appendChild(categoryCell);
const titleCell = document.createElement('td');
titleCell.textContent = service.title;
row.appendChild(titleCell);
const descriptionCell = document.createElement('td');
descriptionCell.textContent = service.description;
row.appendChild(descriptionCell);
const iconUrlCell = document.createElement('td');
iconUrlCell.textContent = service.icon_url;
row.appendChild(iconUrlCell);
tableBody.appendChild(row);
});
};
// Service Manager Page
const serviceId = document.getElementById('serviceId').value;
const apiUrl = `https://thrivein-api-v1-0-0-sxbz2gldiq-et.a.run.app/getservice/`;
const fetchServiceDetails = async (serviceId) => {
const url = apiUrl + serviceId;
try {
const response = await fetch(url, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
},
});
if (!response.ok) {
throw new Error(`Network response was not ok (${response.status} ${response.statusText})`);
}
const data = await response.json();
return data;
} catch (error) {
console.error('Error fetching service details:', error);
return null;
}
};
const fillInputFields = (serviceDetails) => {
const iconUrlInput = document.getElementById('iconUrl_services');
const descriptionInput = document.getElementById('description_services');
if (serviceDetails) {
iconUrlInput.value = serviceDetails.icon_url || '';
descriptionInput.value = serviceDetails.description || '';
} else {
console.log('Service details not found.');
// Clear input fields if service details are not found
iconUrlInput.value = '';
descriptionInput.value = '';
}
};
const searchService = async () => {
const serviceId = document.getElementById('serviceId').value;
try {
const serviceDetails = await fetchServiceDetails(serviceId);
fillInputFields(serviceDetails);
} catch (error) {
console.error('Error fetching service details:', error);
// You might want to display an error message to the user
}
};
const editService = async () => {
const serviceId = document.getElementById('serviceId').value;
const url = `https://thrivein-api-v1-0-0-sxbz2gldiq-et.a.run.app/service/${serviceId}`;
const newData = {
icon_url: document.getElementById('iconUrl_services').value,
description: document.getElementById('description_services').value,
title: document.getElementById('title_services').value,
};
try {
const response = await fetch(url, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(newData),
});
if (!response.ok) {
throw new Error(`Network response was not ok (${response.status} ${response.statusText})`);
}
console.log('Service updated successfully');
} catch (error) {
console.error('Error updating service:', error);
}
};