deduplication of trash code

This commit is contained in:
2025-06-04 11:16:43 +02:00
parent 6ae1ee3365
commit 79fa32d1dd
2 changed files with 6 additions and 55 deletions

View File

@@ -346,7 +346,7 @@ function renderFiles(files) {
*/
async function fetchFiles() {
try {
const endpoint = isTrashPage ? '/api/rooms/trash' : '/api/rooms/starred';
const endpoint = isTrashPage ? '/api/trash' : '/api/rooms/starred';
const response = await fetch(endpoint);
const files = await response.json();
if (files) {
@@ -581,57 +581,6 @@ function showEmptyTrashModal() {
modal.show();
}
/**
* Empties the trash by permanently deleting all trashed files.
* @function
*/
function emptyTrash() {
const csrfToken = getCsrfToken();
if (!csrfToken) {
console.error('CSRF token not available');
return;
}
// Get all trashed files to get their room IDs
fetch('/api/rooms/trash')
.then(r => r.json())
.then(files => {
// Get unique room IDs
const roomIds = [...new Set(files.map(file => file.room_id))];
// Create an array of promises for emptying trash in each room
const emptyPromises = roomIds.map(roomId =>
fetch(`/api/trash/${roomId}/trash/empty`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': csrfToken
}
})
);
// Execute all promises
return Promise.all(emptyPromises);
})
.then(responses => {
// Check if all responses were successful
const allSuccessful = responses.every(r => r.ok);
if (allSuccessful) {
// Clear all files from the current view
currentFiles = [];
renderFiles(currentFiles);
// Close the modal
const modal = bootstrap.Modal.getInstance(document.getElementById('emptyTrashModal'));
modal.hide();
} else {
console.error('Failed to empty trash in some rooms');
}
})
.catch(error => {
console.error('Error emptying trash:', error);
});
}
/**
* Shows the file details modal.
* @function
@@ -743,7 +692,7 @@ document.addEventListener('DOMContentLoaded', function() {
if (isTrashPage) {
const confirmEmptyTrashBtn = document.getElementById('confirmEmptyTrash');
if (confirmEmptyTrashBtn) {
confirmEmptyTrashBtn.addEventListener('click', emptyTrash);
confirmEmptyTrashBtn.addEventListener('click', window.emptyTrash);
}
const confirmPermanentDeleteBtn = document.getElementById('confirmPermanentDelete');