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() { async function fetchFiles() {
try { try {
const endpoint = isTrashPage ? '/api/rooms/trash' : '/api/rooms/starred'; const endpoint = isTrashPage ? '/api/trash' : '/api/rooms/starred';
const response = await fetch(endpoint); const response = await fetch(endpoint);
const files = await response.json(); const files = await response.json();
if (files) { if (files) {
@@ -581,57 +581,6 @@ function showEmptyTrashModal() {
modal.show(); 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. * Shows the file details modal.
* @function * @function
@@ -743,7 +692,7 @@ document.addEventListener('DOMContentLoaded', function() {
if (isTrashPage) { if (isTrashPage) {
const confirmEmptyTrashBtn = document.getElementById('confirmEmptyTrash'); const confirmEmptyTrashBtn = document.getElementById('confirmEmptyTrash');
if (confirmEmptyTrashBtn) { if (confirmEmptyTrashBtn) {
confirmEmptyTrashBtn.addEventListener('click', emptyTrash); confirmEmptyTrashBtn.addEventListener('click', window.emptyTrash);
} }
const confirmPermanentDeleteBtn = document.getElementById('confirmPermanentDelete'); const confirmPermanentDeleteBtn = document.getElementById('confirmPermanentDelete');

View File

@@ -51,7 +51,7 @@ window.emptyTrash = function() {
} }
// Get all trashed files to get their room IDs // Get all trashed files to get their room IDs
fetch('/api/rooms/trash') fetch('/api/trash')
.then(r => r.json()) .then(r => r.json())
.then(files => { .then(files => {
if (!files || !files.length) { if (!files || !files.length) {
@@ -97,7 +97,9 @@ window.emptyTrash = function() {
document.querySelector('.modal-backdrop')?.remove(); document.querySelector('.modal-backdrop')?.remove();
document.body.classList.remove('modal-open'); document.body.classList.remove('modal-open');
// Refresh the view to ensure everything is up to date // Refresh the view to ensure everything is up to date
fetchFiles(); if (typeof window.fetchFiles === 'function') {
window.fetchFiles();
}
} else { } else {
console.error('Failed to empty trash in some rooms'); console.error('Failed to empty trash in some rooms');
// Show error message to user // Show error message to user