feat: add frontend views and file management functionality, enhance user response structure, and update configuration guide
This commit is contained in:
@@ -1,46 +1,73 @@
|
||||
// frontend/static/js/common.js
|
||||
|
||||
(async function checkAuth() {
|
||||
try {
|
||||
const resp = await fetch("/api/whoami");
|
||||
if (resp.ok) {
|
||||
const data = await resp.json();
|
||||
const authSection = document.getElementById("authSection");
|
||||
if (!authSection) return;
|
||||
authSection.innerHTML = '';
|
||||
|
||||
const img = document.createElement('img');
|
||||
img.src = data.picture;
|
||||
img.alt = 'User Avatar';
|
||||
img.className = 'inline-block h-8 w-8 rounded-full mr-2';
|
||||
|
||||
const textNode = document.createTextNode('Logged in as ');
|
||||
const strong = document.createElement('strong');
|
||||
strong.textContent = data.email;
|
||||
|
||||
const logoutLink = document.createElement('a');
|
||||
logoutLink.href = '/logout';
|
||||
logoutLink.className = 'ml-4 text-blue-600 hover:text-blue-800';
|
||||
logoutLink.textContent = 'Logout';
|
||||
|
||||
authSection.appendChild(img);
|
||||
authSection.appendChild(textNode);
|
||||
authSection.appendChild(strong);
|
||||
authSection.appendChild(logoutLink);
|
||||
} else {
|
||||
const authSection = document.getElementById("authSection");
|
||||
if (authSection) {
|
||||
authSection.innerHTML =
|
||||
`<a href="/login" class="text-blue-600">Login</a>`;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
// Fallback if whoami endpoint fails
|
||||
const authSection = document.getElementById("authSection");
|
||||
// Check authentication status and update the auth section
|
||||
(async function() {
|
||||
console.log('Checking authentication status...');
|
||||
try {
|
||||
const response = await fetch('/api/auth/whoami');
|
||||
const data = await response.json();
|
||||
|
||||
|
||||
const authSection = document.getElementById("authSection");
|
||||
const mobileAuthSection = document.getElementById("mobileAuthSection");
|
||||
|
||||
// If we have an email, user is authenticated (the whoami endpoint would have thrown 401 otherwise)
|
||||
if (data.email) {
|
||||
// Get the display name (prefer name, fall back to preferred_username, then email)
|
||||
const displayName = data.name || data.preferred_username || data.email;
|
||||
|
||||
// User is logged in
|
||||
let authHTML = `
|
||||
<div class="flex items-center">
|
||||
<img src="${data.picture}" alt="Avatar" class="w-8 h-8 rounded-full mr-2" />
|
||||
<span>${displayName}</span>
|
||||
<a href="/logout" class="ml-3 text-red-600 hover:text-red-800">
|
||||
<i class="fas fa-sign-out-alt"></i>
|
||||
</a>
|
||||
</div>
|
||||
`;
|
||||
|
||||
if (authSection) {
|
||||
authSection.innerHTML =
|
||||
`<a href="/login" class="text-blue-600">Login</a>`;
|
||||
authSection.innerHTML = authHTML;
|
||||
}
|
||||
|
||||
if (mobileAuthSection) {
|
||||
mobileAuthSection.innerHTML = `
|
||||
<div class="flex items-center justify-between">
|
||||
<div class="flex items-center">
|
||||
<img src="${data.picture}" alt="Avatar" class="w-6 h-6 rounded-full mr-2" />
|
||||
<span>${displayName}</span>
|
||||
</div>
|
||||
<a href="/logout" class="text-red-600 hover:text-red-800">
|
||||
<i class="fas fa-sign-out-alt"></i> Logout
|
||||
</a>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
} else {
|
||||
// User is not logged in (this shouldn't happen with current setup, but keeping as fallback)
|
||||
if (authSection) {
|
||||
authSection.innerHTML = `<a href="/login" class="text-blue-600">Login</a>`;
|
||||
}
|
||||
|
||||
if (mobileAuthSection) {
|
||||
mobileAuthSection.innerHTML = `<a href="/login" class="text-blue-600">Login</a>`;
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
} catch (error) {
|
||||
console.error('Authentication check failed:', error);
|
||||
// Fallback if whoami endpoint fails
|
||||
const authSection = document.getElementById("authSection");
|
||||
const mobileAuthSection = document.getElementById("mobileAuthSection");
|
||||
|
||||
if (authSection) {
|
||||
authSection.innerHTML = `<a href="/login" class="text-blue-600">Login</a>`;
|
||||
}
|
||||
|
||||
if (mobileAuthSection) {
|
||||
mobileAuthSection.innerHTML = `<a href="/login" class="text-blue-600">Login</a>`;
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
// Other common functionality can be added here
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
<!-- Tailwind CSS or other global CSS references -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
|
||||
<link rel="stylesheet" href="/static/styles.css" />
|
||||
<!-- Alpine.js moved to head for earlier loading -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js" defer></script>
|
||||
{% block head_css %}
|
||||
<!-- Tailwind CSS and other CSS -->
|
||||
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
|
||||
@@ -38,26 +40,58 @@
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<!-- Menu Items -->
|
||||
<div class="hidden md:flex space-x-4 items-center">
|
||||
<a href="/" class="text-gray-700 hover:text-gray-900">Home</a>
|
||||
<a href="/upload" class="text-gray-700 hover:text-gray-900">Upload</a>
|
||||
<a href="/files" class="text-gray-700 hover:text-gray-900">Files</a>
|
||||
<a href="/status" class="text-gray-700 hover:text-gray-900">Status</a>
|
||||
<a href="/about" class="text-gray-700 hover:text-gray-900">About</a>
|
||||
<!-- Menu Items - using x-data for mobile menu toggle -->
|
||||
<div x-data="{ mobileMenuOpen: false }">
|
||||
<div class="hidden md:flex space-x-4 items-center">
|
||||
<a href="/" class="text-gray-700 hover:text-gray-900">Home</a>
|
||||
<a href="/upload" class="text-gray-700 hover:text-gray-900">Upload</a>
|
||||
<a href="/files" class="text-gray-700 hover:text-gray-900">Files</a>
|
||||
<a href="/status" class="text-gray-700 hover:text-gray-900">Status</a>
|
||||
<a href="/about" class="text-gray-700 hover:text-gray-900">About</a>
|
||||
|
||||
<!-- Dynamic Auth Section -->
|
||||
<div id="authSection" class="text-gray-700 hover:text-gray-900"></div>
|
||||
</div>
|
||||
|
||||
<!-- Dynamic Auth Section -->
|
||||
<div id="authSection" class="text-gray-700 hover:text-gray-900"></div>
|
||||
</div>
|
||||
|
||||
<!-- Mobile menu button -->
|
||||
<div class="md:hidden">
|
||||
<button type="button" class="inline-flex items-center justify-center p-2 rounded-md text-gray-400 hover:text-gray-500 hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-inset focus:ring-indigo-500" aria-expanded="false">
|
||||
<span class="sr-only">Open main menu</span>
|
||||
<svg class="block h-6 w-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16" />
|
||||
</svg>
|
||||
</button>
|
||||
<!-- Mobile menu button -->
|
||||
<div class="md:hidden">
|
||||
<button
|
||||
@click="mobileMenuOpen = !mobileMenuOpen"
|
||||
type="button"
|
||||
class="inline-flex items-center justify-center p-2 rounded-md text-gray-400 hover:text-gray-500 hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-inset focus:ring-indigo-500"
|
||||
:aria-expanded="mobileMenuOpen"
|
||||
>
|
||||
<span class="sr-only">Open main menu</span>
|
||||
<svg class="block h-6 w-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- Mobile menu, show/hide based on menu state -->
|
||||
<div
|
||||
x-show="mobileMenuOpen"
|
||||
x-transition:enter="transition ease-out duration-100 transform"
|
||||
x-transition:enter-start="opacity-0 scale-95"
|
||||
x-transition:enter-end="opacity-100 scale-100"
|
||||
x-transition:leave="transition ease-in duration-75 transform"
|
||||
x-transition:leave-start="opacity-100 scale-100"
|
||||
x-transition:leave-end="opacity-0 scale-95"
|
||||
class="md:hidden absolute top-16 inset-x-0 bg-white shadow-md z-50"
|
||||
>
|
||||
<div class="px-2 pt-2 pb-3 space-y-1 sm:px-3">
|
||||
<a href="/" class="block px-3 py-2 rounded-md text-base font-medium text-gray-700 hover:text-gray-900 hover:bg-gray-50">Home</a>
|
||||
<a href="/upload" class="block px-3 py-2 rounded-md text-base font-medium text-gray-700 hover:text-gray-900 hover:bg-gray-50">Upload</a>
|
||||
<a href="/files" class="block px-3 py-2 rounded-md text-base font-medium text-gray-700 hover:text-gray-900 hover:bg-gray-50">Files</a>
|
||||
<a href="/status" class="block px-3 py-2 rounded-md text-base font-medium text-gray-700 hover:text-gray-900 hover:bg-gray-50">Status</a>
|
||||
<a href="/about" class="block px-3 py-2 rounded-md text-base font-medium text-gray-700 hover:text-gray-900 hover:bg-gray-50">About</a>
|
||||
|
||||
<!-- Mobile Auth Section -->
|
||||
<div id="mobileAuthSection" class="block px-3 py-2 rounded-md text-base font-medium text-gray-700 hover:text-gray-900 hover:bg-gray-50">
|
||||
<!-- Will be populated by JS -->
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
@@ -74,8 +108,6 @@
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
<!-- Alpine.js for dropdown functionality -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js" defer></script>
|
||||
<!-- Common JS (shared) -->
|
||||
<script src="/static/js/common.js"></script>
|
||||
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
{% extends "base.html" %}
|
||||
{% block title %}File Records{% endblock %}
|
||||
|
||||
{% block head_extra %}
|
||||
<!-- Include Grid.js CSS -->
|
||||
<link href="https://unpkg.com/gridjs/dist/theme/mermaid.min.css" rel="stylesheet" />
|
||||
<script src="/static/js/common.js"></script>
|
||||
<!-- Make sure Alpine.js is properly initialized -->
|
||||
<script>
|
||||
console.log('Before Alpine init on /files page');
|
||||
document.addEventListener('alpine:init', () => {
|
||||
console.log('Alpine.js initialized in files view');
|
||||
|
||||
});
|
||||
console.log('After Alpine init listener registration');
|
||||
</script>
|
||||
<style>
|
||||
.delete-btn {
|
||||
color: #e53e3e;
|
||||
cursor: pointer;
|
||||
padding: 0.25rem 0.5rem;
|
||||
border-radius: 0.25rem;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
.delete-btn:hover {
|
||||
background-color: #fed7d7;
|
||||
}
|
||||
.confirm-delete-modal {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background-color: rgba(0, 0, 0, 0.5);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 100;
|
||||
}
|
||||
.confirm-delete-content {
|
||||
background-color: white;
|
||||
padding: 1.5rem;
|
||||
border-radius: 0.5rem;
|
||||
max-width: 500px;
|
||||
width: 90%;
|
||||
}
|
||||
.hidden {
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="max-w-7xl mx-auto px-4 py-8">
|
||||
<h2 class="text-3xl font-bold mb-6">File Records</h2>
|
||||
<!-- Grid.js will render the table in this container -->
|
||||
<div id="gridjs-wrapper"></div>
|
||||
|
||||
<!-- Confirmation Modal (temporarily disabled) -->
|
||||
<div id="confirmDeleteModal" class="confirm-delete-modal hidden">
|
||||
<div class="confirm-delete-content">
|
||||
<h3 class="text-lg font-medium text-gray-900 mb-4">Confirm Deletion</h3>
|
||||
<p class="text-sm text-gray-500 mb-4">
|
||||
Are you sure you want to delete this file record? This action cannot be undone.
|
||||
</p>
|
||||
<p class="text-sm text-gray-700 mb-4">
|
||||
<strong>File:</strong> <span id="deleteFileName"></span>
|
||||
</p>
|
||||
<div class="flex justify-end space-x-3">
|
||||
<button id="cancelDelete" class="px-4 py-2 bg-gray-200 text-gray-800 rounded hover:bg-gray-300">
|
||||
Cancel
|
||||
</button>
|
||||
<button id="confirmDelete" class="px-4 py-2 bg-red-600 text-white rounded hover:bg-red-700">
|
||||
Delete
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
{% block scripts %}
|
||||
<!-- Include Grid.js JS -->
|
||||
<script src="https://unpkg.com/gridjs/dist/gridjs.umd.js"></script>
|
||||
<script>
|
||||
// File deletion handling
|
||||
let fileToDelete = null;
|
||||
const confirmDeleteModal = document.getElementById('confirmDeleteModal');
|
||||
const deleteFileName = document.getElementById('deleteFileName');
|
||||
const cancelDelete = document.getElementById('cancelDelete');
|
||||
const confirmDelete = document.getElementById('confirmDelete');
|
||||
|
||||
// Close modal
|
||||
function closeDeleteModal() {
|
||||
confirmDeleteModal.classList.add('hidden');
|
||||
fileToDelete = null;
|
||||
}
|
||||
|
||||
// Delete file directly without confirmation
|
||||
async function deleteFile(fileId) {
|
||||
try {
|
||||
const response = await fetch(`/api/files/${fileId}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
// Success - reload the grid
|
||||
grid.forceRender();
|
||||
} else {
|
||||
// Show error
|
||||
const error = await response.json();
|
||||
alert(`Error: ${error.detail || 'Failed to delete file'}`);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Delete error:', err);
|
||||
alert('An error occurred while deleting the file');
|
||||
}
|
||||
}
|
||||
|
||||
// Show delete confirmation modal (temporarily bypassed)
|
||||
function showDeleteModal(fileId, fileName) {
|
||||
// Bypass modal and delete immediately
|
||||
deleteFile(fileId);
|
||||
// The original code is commented out:
|
||||
// fileToDelete = { id: fileId, name: fileName };
|
||||
// deleteFileName.textContent = fileName || `ID: ${fileId}`;
|
||||
// confirmDeleteModal.classList.remove('hidden');
|
||||
}
|
||||
|
||||
// Handle delete confirmation
|
||||
confirmDelete.addEventListener('click', async function() {
|
||||
if (!fileToDelete) return;
|
||||
|
||||
deleteFile(fileToDelete.id);
|
||||
closeDeleteModal();
|
||||
});
|
||||
|
||||
// Cancel delete
|
||||
cancelDelete.addEventListener('click', closeDeleteModal);
|
||||
confirmDeleteModal.addEventListener('click', function(e) {
|
||||
if (e.target === confirmDeleteModal) closeDeleteModal();
|
||||
});
|
||||
|
||||
// Initialize the grid
|
||||
const grid = new gridjs.Grid({
|
||||
columns: [
|
||||
{ id: 'id', name: 'ID' },
|
||||
{ id: 'original_filename', name: 'Original Filename' },
|
||||
{ id: 'file_size', name: 'File Size', formatter: (size) => `${(size / 1024).toFixed(2)} KB` },
|
||||
{ id: 'mime_type', name: 'Mime Type' },
|
||||
{ id: 'created_at', name: 'Created At' },
|
||||
{
|
||||
id: 'actions',
|
||||
name: 'Actions',
|
||||
formatter: (_, row) => {
|
||||
return gridjs.h('button', {
|
||||
className: 'delete-btn',
|
||||
onClick: () => showDeleteModal(row.cells[0].data, row.cells[1].data)
|
||||
}, [
|
||||
gridjs.h('i', { className: 'fas fa-trash' }),
|
||||
]);
|
||||
}
|
||||
}
|
||||
],
|
||||
server: {
|
||||
url: '/api/files',
|
||||
then: data => data.map(file => [
|
||||
file.id,
|
||||
file.original_filename || "",
|
||||
file.file_size,
|
||||
file.mime_type,
|
||||
file.created_at || "",
|
||||
"" // This cell will be rendered by the formatter
|
||||
])
|
||||
},
|
||||
search: true,
|
||||
sort: true,
|
||||
pagination: {
|
||||
limit: 10
|
||||
}
|
||||
}).render(document.getElementById("gridjs-wrapper"));
|
||||
</script>
|
||||
{% endblock %}
|
||||
+192
-122
@@ -2,169 +2,239 @@
|
||||
{% block title %}File Records{% endblock %}
|
||||
|
||||
{% block head_extra %}
|
||||
<!-- Include Grid.js CSS -->
|
||||
<link href="https://unpkg.com/gridjs/dist/theme/mermaid.min.css" rel="stylesheet" />
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
|
||||
<script src="/static/js/common.js"></script>
|
||||
<!-- Add TableSorter library -->
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.3/js/jquery.tablesorter.min.js"></script>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.3/css/theme.bootstrap_4.min.css">
|
||||
<style>
|
||||
.file-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
.file-table th,
|
||||
.file-table td {
|
||||
padding: 0.75rem;
|
||||
text-align: left;
|
||||
border-bottom: 1px solid #e2e8f0;
|
||||
}
|
||||
.file-table th {
|
||||
background-color: #f7fafc;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
}
|
||||
.file-table th:hover {
|
||||
background-color: #edf2f7;
|
||||
}
|
||||
.file-table tbody tr:hover {
|
||||
background-color: #f7fafc;
|
||||
}
|
||||
.delete-btn {
|
||||
color: #e53e3e;
|
||||
cursor: pointer;
|
||||
padding: 0.25rem 0.5rem;
|
||||
border-radius: 0.25rem;
|
||||
background: none;
|
||||
border: none;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
.delete-btn:hover {
|
||||
background-color: #fed7d7;
|
||||
}
|
||||
.confirm-delete-modal {
|
||||
.error-message {
|
||||
background-color: #FEE2E2;
|
||||
border: 1px solid #F87171;
|
||||
color: #B91C1C;
|
||||
padding: 1rem;
|
||||
border-radius: 0.25rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
/* TableSorter specific styles */
|
||||
.tablesorter-header-inner {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
/* Modal styles */
|
||||
.modal {
|
||||
display: none;
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background-color: rgba(0, 0, 0, 0.5);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
z-index: 1000;
|
||||
justify-content: center;
|
||||
z-index: 100;
|
||||
align-items: center;
|
||||
}
|
||||
.confirm-delete-content {
|
||||
.modal-content {
|
||||
background-color: white;
|
||||
padding: 1.5rem;
|
||||
border-radius: 0.5rem;
|
||||
padding: 2rem;
|
||||
max-width: 500px;
|
||||
width: 90%;
|
||||
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
.hidden {
|
||||
display: none;
|
||||
.modal-title {
|
||||
font-size: 1.25rem;
|
||||
font-weight: bold;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
.modal-buttons {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
margin-top: 1.5rem;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
.modal-btn {
|
||||
padding: 0.5rem 1rem;
|
||||
border-radius: 0.25rem;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
.modal-btn-cancel {
|
||||
background-color: #e2e8f0;
|
||||
}
|
||||
.modal-btn-cancel:hover {
|
||||
background-color: #cbd5e0;
|
||||
}
|
||||
.modal-btn-delete {
|
||||
background-color: #e53e3e;
|
||||
color: white;
|
||||
}
|
||||
.modal-btn-delete:hover {
|
||||
background-color: #c53030;
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="max-w-7xl mx-auto px-4 py-8">
|
||||
<div class="container mx-auto px-4 py-8">
|
||||
<h2 class="text-3xl font-bold mb-6">File Records</h2>
|
||||
<!-- Grid.js will render the table in this container -->
|
||||
<div id="gridjs-wrapper"></div>
|
||||
|
||||
<!-- Confirmation Modal -->
|
||||
<div id="confirmDeleteModal" class="confirm-delete-modal hidden">
|
||||
<div class="confirm-delete-content">
|
||||
<h3 class="text-lg font-medium text-gray-900 mb-4">Confirm Deletion</h3>
|
||||
<p class="text-sm text-gray-500 mb-4">
|
||||
Are you sure you want to delete this file record? This action cannot be undone.
|
||||
</p>
|
||||
<p class="text-sm text-gray-700 mb-4">
|
||||
<strong>File:</strong> <span id="deleteFileName"></span>
|
||||
</p>
|
||||
<div class="flex justify-end space-x-3">
|
||||
<button id="cancelDelete" class="px-4 py-2 bg-gray-200 text-gray-800 rounded hover:bg-gray-300">
|
||||
Cancel
|
||||
</button>
|
||||
<button id="confirmDelete" class="px-4 py-2 bg-red-600 text-white rounded hover:bg-red-700">
|
||||
Delete
|
||||
</button>
|
||||
{% if error %}
|
||||
<div class="error-message">
|
||||
<p><strong>Error:</strong> {{ error }}</p>
|
||||
<p class="mt-2">This might be due to a configuration or import issue. Please check the server logs.</p>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
|
||||
<!-- File table -->
|
||||
<div class="overflow-x-auto">
|
||||
<table class="file-table" id="fileTable">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Original Filename</th>
|
||||
<th>File Size</th>
|
||||
<th>Mime Type</th>
|
||||
<th>Created At</th>
|
||||
<th class="sorter-false">Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for file in files %}
|
||||
<tr>
|
||||
<td>{{ file.id }}</td>
|
||||
<td>{{ file.original_filename }}</td>
|
||||
<td data-sort-value="{{ file.file_size }}">{{ (file.file_size / 1024) | round(2) }} KB</td>
|
||||
<td>{{ file.mime_type }}</td>
|
||||
<td>{{ file.created_at }}</td>
|
||||
<td>
|
||||
<button onclick="showDeleteModal('{{ file.id }}')" class="delete-btn">
|
||||
<i class="fas fa-trash"></i>
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
{% else %}
|
||||
<tr>
|
||||
<td colspan="6" class="text-center py-4">No files found</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<!-- Delete confirmation modal -->
|
||||
<div id="deleteModal" class="modal">
|
||||
<div class="modal-content">
|
||||
<div class="modal-title">Confirm Deletion</div>
|
||||
<p>Are you sure you want to delete this file?</p>
|
||||
<div class="modal-buttons">
|
||||
<button id="cancelDelete" class="modal-btn modal-btn-cancel">Cancel</button>
|
||||
<button id="confirmDelete" class="modal-btn modal-btn-delete">Delete</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
{% block scripts %}
|
||||
<!-- Include Grid.js JS -->
|
||||
<script src="https://unpkg.com/gridjs/dist/gridjs.umd.js"></script>
|
||||
|
||||
<!-- Add JavaScript for handling DELETE requests -->
|
||||
<script>
|
||||
// File deletion handling
|
||||
let fileToDelete = null;
|
||||
const confirmDeleteModal = document.getElementById('confirmDeleteModal');
|
||||
const deleteFileName = document.getElementById('deleteFileName');
|
||||
// Modal functionality
|
||||
const modal = document.getElementById('deleteModal');
|
||||
const cancelDelete = document.getElementById('cancelDelete');
|
||||
const confirmDelete = document.getElementById('confirmDelete');
|
||||
|
||||
// Close modal
|
||||
function closeDeleteModal() {
|
||||
confirmDeleteModal.classList.add('hidden');
|
||||
fileToDelete = null;
|
||||
let currentFileId = null;
|
||||
|
||||
function showDeleteModal(fileId) {
|
||||
currentFileId = fileId;
|
||||
modal.style.display = 'flex';
|
||||
}
|
||||
|
||||
// Show delete confirmation modal
|
||||
function showDeleteModal(fileId, fileName) {
|
||||
fileToDelete = { id: fileId, name: fileName };
|
||||
deleteFileName.textContent = fileName || `ID: ${fileId}`;
|
||||
confirmDeleteModal.classList.remove('hidden');
|
||||
}
|
||||
|
||||
// Handle delete confirmation
|
||||
confirmDelete.addEventListener('click', async function() {
|
||||
if (!fileToDelete) return;
|
||||
|
||||
try {
|
||||
const response = await fetch(`/api/files/${fileToDelete.id}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
// Success - reload the grid
|
||||
grid.forceRender();
|
||||
closeDeleteModal();
|
||||
} else {
|
||||
// Show error
|
||||
const error = await response.json();
|
||||
alert(`Error: ${error.detail || 'Failed to delete file'}`);
|
||||
closeDeleteModal();
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Delete error:', err);
|
||||
alert('An error occurred while deleting the file');
|
||||
closeDeleteModal();
|
||||
|
||||
cancelDelete.addEventListener('click', () => {
|
||||
modal.style.display = 'none';
|
||||
});
|
||||
|
||||
confirmDelete.addEventListener('click', () => {
|
||||
deleteFile(currentFileId);
|
||||
modal.style.display = 'none';
|
||||
});
|
||||
|
||||
// Close modal if clicking outside of it
|
||||
window.addEventListener('click', (event) => {
|
||||
if (event.target === modal) {
|
||||
modal.style.display = 'none';
|
||||
}
|
||||
});
|
||||
|
||||
// Cancel delete
|
||||
cancelDelete.addEventListener('click', closeDeleteModal);
|
||||
confirmDeleteModal.addEventListener('click', function(e) {
|
||||
if (e.target === confirmDeleteModal) closeDeleteModal();
|
||||
});
|
||||
|
||||
// Initialize the grid
|
||||
const grid = new gridjs.Grid({
|
||||
columns: [
|
||||
{ id: 'id', name: 'ID' },
|
||||
{ id: 'original_filename', name: 'Original Filename' },
|
||||
{ id: 'file_size', name: 'File Size', formatter: (size) => `${(size / 1024).toFixed(2)} KB` },
|
||||
{ id: 'mime_type', name: 'Mime Type' },
|
||||
{ id: 'created_at', name: 'Created At' },
|
||||
{
|
||||
id: 'actions',
|
||||
name: 'Actions',
|
||||
formatter: (_, row) => {
|
||||
return gridjs.h('button', {
|
||||
className: 'delete-btn',
|
||||
onClick: () => showDeleteModal(row.cells[0].data, row.cells[1].data)
|
||||
}, [
|
||||
gridjs.h('i', { className: 'fas fa-trash' }),
|
||||
]);
|
||||
}
|
||||
|
||||
function deleteFile(fileId) {
|
||||
fetch(`/api/files/${fileId}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
],
|
||||
server: {
|
||||
url: '/api/files',
|
||||
then: data => data.map(file => [
|
||||
file.id,
|
||||
file.original_filename || "",
|
||||
file.file_size,
|
||||
file.mime_type,
|
||||
file.created_at || "",
|
||||
"" // This cell will be rendered by the formatter
|
||||
])
|
||||
},
|
||||
search: true,
|
||||
sort: true,
|
||||
pagination: {
|
||||
limit: 10
|
||||
}
|
||||
}).render(document.getElementById("gridjs-wrapper"));
|
||||
})
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to delete file');
|
||||
}
|
||||
// Reload the page to show updated file list
|
||||
window.location.reload();
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert(`Error deleting file: ${error.message}`);
|
||||
});
|
||||
}
|
||||
|
||||
// Initialize TableSorter
|
||||
$(document).ready(function() {
|
||||
$("#fileTable").tablesorter({
|
||||
theme: 'bootstrap',
|
||||
widthFixed: true,
|
||||
headerTemplate: '{content} {icon}',
|
||||
widgets: ['zebra', 'stickyHeaders'],
|
||||
sortList: [[0, 0]], // Default sort on the first column ascending
|
||||
headers: {
|
||||
5: { sorter: false } // Disable sorting on the Actions column
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user