diff --git a/static/js/file-grid.js b/static/js/file-grid.js index b54fd58..f76cbe5 100644 --- a/static/js/file-grid.js +++ b/static/js/file-grid.js @@ -400,16 +400,31 @@ function emptyTrash() { return; } - fetch('/api/rooms/empty-trash', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'X-CSRFToken': csrfToken - } - }) + // Get all trashed files to get their room IDs + fetch('/api/rooms/trash') .then(r => r.json()) - .then(res => { - if (res.success) { + .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/rooms/${roomId}/trash/empty`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-CSRFToken': 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); @@ -417,7 +432,7 @@ function emptyTrash() { const modal = bootstrap.Modal.getInstance(document.getElementById('emptyTrashModal')); modal.hide(); } else { - console.error('Failed to empty trash:', res.error); + console.error('Failed to empty trash in some rooms'); } }) .catch(error => { diff --git a/static/js/trash.js b/static/js/trash.js index d201227..c5bb932 100644 --- a/static/js/trash.js +++ b/static/js/trash.js @@ -1,10 +1,85 @@ let currentView = 'grid'; +// Make functions globally available +window.showEmptyTrashModal = function() { + console.log('Showing Empty Trash Modal'); + const modalEl = document.getElementById('emptyTrashModal'); + console.log('Modal Element:', modalEl); + + if (!modalEl) { + console.error('Empty Trash Modal element not found'); + return; + } + + try { + const modal = new bootstrap.Modal(modalEl); + console.log('Modal instance created:', modal); + modal.show(); + } catch (error) { + console.error('Error showing modal:', error); + } +}; + +window.emptyTrash = function() { + console.log('Emptying Trash'); + const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content'); + 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/rooms/${roomId}/trash/empty`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-CSRFToken': 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 + window.currentFiles = []; + // Update the grid directly + const grid = document.getElementById('fileGrid'); + grid.innerHTML = '