Perbaikan API WA
This commit is contained in:
@@ -124,6 +124,9 @@
|
||||
<div class="text-center py-4">
|
||||
<i id="pesanIcon" class="fas fa-info-circle fa-3x text-secondary mb-3"></i>
|
||||
<p id="pesanText" class="mb-0"></p>
|
||||
<button type="button" class="btn btn-primary mt-3 d-none" id="btnMintaTokenBaru">
|
||||
<i class="fas fa-rotate-right"></i> Minta Token Baru
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -229,26 +232,30 @@
|
||||
});
|
||||
}
|
||||
|
||||
function ambilPesanError(xhr) {
|
||||
function ambilDataError(xhr) {
|
||||
try {
|
||||
const data = JSON.parse(xhr.responseText);
|
||||
return data.message || 'Terjadi kesalahan. Silakan coba lagi.';
|
||||
return JSON.parse(xhr.responseText);
|
||||
} catch (e) {
|
||||
return 'Terjadi kesalahan. Silakan coba lagi.';
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function ambilPesanError(xhr) {
|
||||
return ambilDataError(xhr).message || 'Terjadi kesalahan. Silakan coba lagi.';
|
||||
}
|
||||
|
||||
const statusBadgeMap = {
|
||||
'Diajukan': ['bg-warning text-dark', 'Menunggu Verifikasi'],
|
||||
'Diterima': ['bg-success', 'Diterima'],
|
||||
'Ditolak': ['bg-danger', 'Ditolak'],
|
||||
};
|
||||
|
||||
function tampilkanPesan(icon, text, colorClass) {
|
||||
function tampilkanPesan(icon, text, colorClass, bisaMintaTokenBaru = false) {
|
||||
$('#pesanState').removeClass('d-none');
|
||||
$('#detailState').addClass('d-none');
|
||||
$('#pesanIcon').attr('class', `fas ${icon} fa-3x mb-3 ${colorClass}`);
|
||||
$('#pesanText').text(text);
|
||||
$('#btnMintaTokenBaru').toggleClass('d-none', !bisaMintaTokenBaru);
|
||||
}
|
||||
|
||||
function tampilkanDetail(data, state) {
|
||||
@@ -288,24 +295,45 @@
|
||||
url: `${apiBase}?token=${encodeURIComponent(token)}`,
|
||||
method: 'GET',
|
||||
success: function (res) {
|
||||
if (res.new_token) {
|
||||
token = res.new_token;
|
||||
const url = new URL(window.location.href);
|
||||
url.searchParams.set('token', token);
|
||||
window.history.replaceState({}, '', url);
|
||||
}
|
||||
|
||||
tampilkanDetail(res.data, res.state);
|
||||
selesaiLoading();
|
||||
},
|
||||
error: function (xhr) {
|
||||
const pesan = ambilPesanError(xhr);
|
||||
tampilkanPesan('fa-exclamation-triangle', pesan, 'text-danger');
|
||||
const data = ambilDataError(xhr);
|
||||
tampilkanPesan('fa-exclamation-triangle', data.message || 'Terjadi kesalahan. Silakan coba lagi.', 'text-danger', data.state === 'session_expired');
|
||||
selesaiLoading();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$('#btnMintaTokenBaru').on('click', function () {
|
||||
const $btn = $(this);
|
||||
const originalText = $btn.html();
|
||||
$btn.prop('disabled', true).text('Memproses...');
|
||||
|
||||
$.ajax({
|
||||
url: `${apiBase}/perbarui-token?token=${encodeURIComponent(token)}`,
|
||||
method: 'POST',
|
||||
data: { token },
|
||||
success: function (res) {
|
||||
token = res.new_token;
|
||||
const url = new URL(window.location.href);
|
||||
url.searchParams.set('token', token);
|
||||
window.history.replaceState({}, '', url);
|
||||
|
||||
$('#loadingState').removeClass('d-none');
|
||||
$('#mainCard').addClass('d-none');
|
||||
muatData();
|
||||
},
|
||||
error: function (xhr) {
|
||||
showNotification('error', 'Gagal', ambilPesanError(xhr));
|
||||
},
|
||||
complete: function () {
|
||||
$btn.prop('disabled', false).html(originalText);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function selesaiLoading() {
|
||||
$('#loadingState').addClass('d-none');
|
||||
$('#mainCard').removeClass('d-none');
|
||||
|
||||
Reference in New Issue
Block a user