mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
added logout api
This commit is contained in:
@@ -1,67 +1,82 @@
|
||||
let pages = 0;
|
||||
let currentPage = 0;
|
||||
let limit = 25;
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
const queries = new URLSearchParams(window.location.search);
|
||||
let page = parseInt(queries.get("page"));
|
||||
let limit = parseInt(queries.get("limit"));
|
||||
|
||||
if (isNaN(page)) {
|
||||
page = 0;
|
||||
}
|
||||
|
||||
if (page < 0) {
|
||||
page = 0;
|
||||
}
|
||||
|
||||
if (isNaN(limit) || limit < 1 || limit > 25) {
|
||||
limit = 25;
|
||||
}
|
||||
|
||||
currentPage = page;
|
||||
|
||||
async function renderArchive(page, limit) {
|
||||
const archiveList = document.getElementById("archives");
|
||||
const pagesElement = document.getElementById("paste-pages");
|
||||
const previousButton = document.getElementById("previous-button");
|
||||
const nextButton = document.getElementById("next-button");
|
||||
|
||||
|
||||
let loadingOverlay = document.getElementById("archive-loading-overlay");
|
||||
if (!loadingOverlay) {
|
||||
loadingOverlay = document.createElement("div");
|
||||
loadingOverlay.id = "archive-loading-overlay";
|
||||
loadingOverlay.innerHTML = '<div class="loading-spinner"><div></div><div></div><div></div></div>';
|
||||
loadingOverlay.style.position = "absolute";
|
||||
loadingOverlay.style.top = 0;
|
||||
loadingOverlay.style.left = 0;
|
||||
loadingOverlay.style.width = "100%";
|
||||
loadingOverlay.style.height = "100%";
|
||||
loadingOverlay.style.display = "flex";
|
||||
loadingOverlay.style.justifyContent = "center";
|
||||
loadingOverlay.style.alignItems = "center";
|
||||
loadingOverlay.style.background = "transparent";
|
||||
loadingOverlay.style.zIndex = 10;
|
||||
archiveList.parentElement.style.position = "relative";
|
||||
archiveList.parentElement.appendChild(loadingOverlay);
|
||||
} else {
|
||||
loadingOverlay.style.display = "flex";
|
||||
}
|
||||
|
||||
try {
|
||||
archiveList.innerHTML = '<div class="loading-spinner"><div></div><div></div><div></div></div>';
|
||||
|
||||
const archiveData = await getPastes(page, limit);
|
||||
|
||||
const pastes = archiveData.pastes;
|
||||
pages = archiveData.pages;
|
||||
|
||||
pagesElement.innerText = `${parseInt(page) + 1}/${pages}`;
|
||||
|
||||
currentPage = page;
|
||||
|
||||
pagesElement.innerText = `${page + 1}/${pages}`;
|
||||
previousButton.disabled = page <= 0;
|
||||
nextButton.disabled = page + 1 >= pages;
|
||||
|
||||
if (pastes.length === 0) {
|
||||
archiveList.innerHTML = '<div class="no-pastes">No pastes found</div>';
|
||||
return;
|
||||
}
|
||||
|
||||
let archiveListHTML = "";
|
||||
pastes.forEach(paste => {
|
||||
const title = paste.title.length > 30 ? paste.title.substring(0, 30) + '...' : paste.title;
|
||||
archiveListHTML += `
|
||||
<li>
|
||||
<a href="/${paste.id}">
|
||||
<p id="archive-title" title="${paste.title}">${title}</p>
|
||||
<p id="archive-date">${formatDate(paste.created)}</p>
|
||||
<p id="archive-syntax">${paste.syntax || 'Plain Text'}</p>
|
||||
</a>
|
||||
</li>
|
||||
`;
|
||||
});
|
||||
|
||||
if (pastes.length === 0) {
|
||||
archiveListHTML = '<div class="no-pastes">No pastes found</div>';
|
||||
} else {
|
||||
pastes.forEach(paste => {
|
||||
const title = paste.title.length > 30 ? paste.title.substring(0, 30) + '...' : paste.title;
|
||||
archiveListHTML += `
|
||||
<li>
|
||||
<a href="/${paste.id}">
|
||||
<p id="archive-title" title="${paste.title}">${title}</p>
|
||||
<p id="archive-date">${formatDate(paste.created)}</p>
|
||||
<p id="archive-syntax">${paste.syntax || 'Plain Text'}</p>
|
||||
</a>
|
||||
</li>
|
||||
`;
|
||||
});
|
||||
}
|
||||
archiveList.innerHTML = archiveListHTML;
|
||||
} catch (error) {
|
||||
console.error('Error fetching pastes:', error);
|
||||
archiveList.innerHTML = '<div class="error-message">Failed to load pastes. Please try again later.</div>';
|
||||
} finally {
|
||||
if (loadingOverlay) loadingOverlay.style.display = "none";
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
const queries = new URLSearchParams(window.location.search);
|
||||
let page = parseInt(queries.get("page"));
|
||||
limit = parseInt(queries.get("limit"));
|
||||
|
||||
if (isNaN(page) || page < 0) page = 0;
|
||||
if (isNaN(limit) || limit < 1 || limit > 25) limit = 25;
|
||||
|
||||
await renderArchive(page, limit);
|
||||
|
||||
document.getElementById("previous-button").addEventListener("click", previousPage);
|
||||
document.getElementById("next-button").addEventListener("click", nextPage);
|
||||
});
|
||||
|
||||
async function getPastes(page, limit) {
|
||||
@@ -95,43 +110,27 @@ function formatDate(unix) {
|
||||
}
|
||||
|
||||
function nextPage() {
|
||||
const queries = new URLSearchParams(window.location.search);
|
||||
let page = parseInt(queries.get("page"));
|
||||
let limit = parseInt(queries.get("limit"));
|
||||
|
||||
if (isNaN(page) || page < 0) {
|
||||
page = 0;
|
||||
}
|
||||
|
||||
if (isNaN(limit) || limit < 1 || limit > 25) {
|
||||
limit = 25;
|
||||
}
|
||||
|
||||
if (page + 1 > pages) {
|
||||
return;
|
||||
}
|
||||
|
||||
page++;
|
||||
window.location.href = `/archive?page=${page}&limit=${limit}`;
|
||||
if (currentPage + 1 >= pages) return;
|
||||
currentPage++;
|
||||
updateUrlAndRender();
|
||||
}
|
||||
|
||||
function previousPage() {
|
||||
const queries = new URLSearchParams(window.location.search);
|
||||
let page = parseInt(queries.get("page"));
|
||||
let limit = parseInt(queries.get("limit"));
|
||||
if (currentPage <= 0) return;
|
||||
currentPage--;
|
||||
updateUrlAndRender();
|
||||
}
|
||||
|
||||
if (isNaN(page) || page < 0) {
|
||||
page = 0;
|
||||
}
|
||||
|
||||
if (isNaN(limit) || limit < 1 || limit > 25) {
|
||||
limit = 25;
|
||||
}
|
||||
function updateUrlAndRender() {
|
||||
const url = `/archive?page=${currentPage}&limit=${limit}`;
|
||||
window.history.pushState({page: currentPage, limit: limit}, '', url);
|
||||
renderArchive(currentPage, limit);
|
||||
}
|
||||
|
||||
if (page <= 0) {
|
||||
return;
|
||||
window.onpopstate = function(event) {
|
||||
if (event.state) {
|
||||
currentPage = event.state.page;
|
||||
limit = event.state.limit;
|
||||
renderArchive(currentPage, limit);
|
||||
}
|
||||
|
||||
page--;
|
||||
window.location.href = `/archive?page=${page}&limit=${limit}`;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user