From 0aadd1f5e9ca8cff5a5b8377203febc5d8b7d01c Mon Sep 17 00:00:00 2001 From: Kobe Date: Mon, 26 May 2025 21:36:27 +0200 Subject: [PATCH] Fix trash --- static/js/file-grid.js | 35 ++++++++++---- static/js/trash.js | 79 +++++++++++++++++++++++++++++++- templates/components/header.html | 3 +- 3 files changed, 105 insertions(+), 12 deletions(-) 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 = '
No items found.
'; + // 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); + }); +}; + document.addEventListener('DOMContentLoaded', function() { + console.log('DOM Content Loaded'); + // Add event listener for the empty trash button const emptyTrashBtn = document.getElementById('emptyTrashBtn'); + console.log('Empty Trash Button:', emptyTrashBtn); + if (emptyTrashBtn) { emptyTrashBtn.addEventListener('click', function(e) { + console.log('Empty Trash Button Clicked'); e.preventDefault(); showEmptyTrashModal(); }); @@ -12,8 +87,10 @@ document.addEventListener('DOMContentLoaded', function() { // Add event listener for the confirm empty trash button const confirmEmptyTrashBtn = document.getElementById('confirmEmptyTrash'); + console.log('Confirm Empty Trash Button:', confirmEmptyTrashBtn); + if (confirmEmptyTrashBtn) { - confirmEmptyTrashBtn.addEventListener('click', emptyTrash); + confirmEmptyTrashBtn.addEventListener('click', window.emptyTrash); } // Initialize view diff --git a/templates/components/header.html b/templates/components/header.html index 884cf01..83c5717 100644 --- a/templates/components/header.html +++ b/templates/components/header.html @@ -16,7 +16,8 @@ {% if button_url == "#" %}