From 79fa32d1ddb7a1d6259e1c8ab26376708e22993a Mon Sep 17 00:00:00 2001 From: Kobe Date: Wed, 4 Jun 2025 11:16:43 +0200 Subject: [PATCH] deduplication of trash code --- static/js/file-grid.js | 55 ++---------------------------------------- static/js/trash.js | 6 +++-- 2 files changed, 6 insertions(+), 55 deletions(-) diff --git a/static/js/file-grid.js b/static/js/file-grid.js index 66cceb7..757492b 100644 --- a/static/js/file-grid.js +++ b/static/js/file-grid.js @@ -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'); diff --git a/static/js/trash.js b/static/js/trash.js index 70fa30c..04aaca3 100644 --- a/static/js/trash.js +++ b/static/js/trash.js @@ -51,7 +51,7 @@ window.emptyTrash = function() { } // Get all trashed files to get their room IDs - fetch('/api/rooms/trash') + fetch('/api/trash') .then(r => r.json()) .then(files => { if (!files || !files.length) { @@ -97,7 +97,9 @@ window.emptyTrash = function() { document.querySelector('.modal-backdrop')?.remove(); document.body.classList.remove('modal-open'); // Refresh the view to ensure everything is up to date - fetchFiles(); + if (typeof window.fetchFiles === 'function') { + window.fetchFiles(); + } } else { console.error('Failed to empty trash in some rooms'); // Show error message to user