спиннер загрузки с сервера
This commit is contained in:
@@ -3,3 +3,31 @@ body {
|
||||
background-repeat: repeat;
|
||||
background-size: 512px auto;
|
||||
}
|
||||
|
||||
.loader-bg {
|
||||
width: 50px;
|
||||
height: 50px;
|
||||
border-radius: 50%;
|
||||
|
||||
/* светлый фон */
|
||||
background: rgba(255, 255, 255, 0.85);
|
||||
|
||||
/* мягкая тень = эффект размытия краёв */
|
||||
box-shadow:
|
||||
0 0 0 8px rgba(255, 255, 255, 0.35),
|
||||
0 0 20px rgba(0, 0, 0, 0.08);
|
||||
|
||||
/* чуть сглаживаем */
|
||||
backdrop-filter: blur(2px);
|
||||
}
|
||||
|
||||
/* плавное появление */
|
||||
#globalLoader {
|
||||
transition: opacity .15s ease-in-out;
|
||||
}
|
||||
#globalLoader.d-none {
|
||||
opacity: 0;
|
||||
}
|
||||
#globalLoader:not(.d-none) {
|
||||
opacity: 1;
|
||||
}
|
||||
+26
-3
@@ -1,4 +1,23 @@
|
||||
// api.js
|
||||
let loaderTimeout = null;
|
||||
|
||||
function showLoader() {
|
||||
const loader = document.getElementById('globalLoader');
|
||||
if (!loader) return;
|
||||
|
||||
clearTimeout(loaderTimeout);
|
||||
loader.classList.remove('d-none');
|
||||
}
|
||||
|
||||
function hideLoader() {
|
||||
const loader = document.getElementById('globalLoader');
|
||||
if (!loader) return;
|
||||
|
||||
loaderTimeout = setTimeout(() => {
|
||||
loader.classList.add('d-none');
|
||||
}, 200); // задержка 0.2 сек
|
||||
}
|
||||
|
||||
export async function apiRequest(url, payload = {}, method = 'POST') {
|
||||
method = method.toUpperCase();
|
||||
|
||||
@@ -12,15 +31,16 @@ export async function apiRequest(url, payload = {}, method = 'POST') {
|
||||
credentials: 'same-origin'
|
||||
};
|
||||
|
||||
// --- Если GET → добавляем payload в URL ---
|
||||
if (method === 'GET') {
|
||||
const params = new URLSearchParams(payload);
|
||||
finalUrl = `${url}?${params.toString()}`;
|
||||
} else {
|
||||
// --- Для остальных методов → отправляем body ---
|
||||
options.body = JSON.stringify(payload);
|
||||
}
|
||||
|
||||
showLoader();
|
||||
|
||||
try {
|
||||
const res = await fetch(finalUrl, options);
|
||||
|
||||
if (!res.ok) {
|
||||
@@ -28,5 +48,8 @@ export async function apiRequest(url, payload = {}, method = 'POST') {
|
||||
throw new Error(`HTTP ${res.status}: ${text}`);
|
||||
}
|
||||
|
||||
return res.json();
|
||||
return await res.json();
|
||||
} finally {
|
||||
hideLoader();
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,14 @@
|
||||
<body>
|
||||
{% block body %}{% endblock %}
|
||||
|
||||
<div id="globalLoader" class="position-fixed bottom-0 end-0 m-3 d-none" style="z-index: 1055;">
|
||||
<div class="loader-bg d-flex align-items-center justify-content-center">
|
||||
<div class="spinner-border text-primary" role="status">
|
||||
<span class="visually-hidden">Loading...</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Bootstrap 5 JS Bundle -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js"
|
||||
integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI"
|
||||
|
||||
Reference in New Issue
Block a user