document.addEventListener('DOMContentLoaded', () => { // Show test modal when the test button is clicked document.querySelectorAll('.test-provider-btn').forEach(button => { button.addEventListener('click', async (e) => { e.preventDefault(); const providerId = button.dataset.providerId; const providerName = button.dataset.providerName; const testModal = document.getElementById('test-provider-modal'); // Show the modal and add classes to make it visible testModal.classList.remove('hidden'); testModal.classList.add('flex'); // Create loading indicator testModal.innerHTML = `

Testing Connection: ${providerName}

Testing connection...
`; // Add event listener for close button testModal.querySelector('.close-modal').addEventListener('click', () => { testModal.classList.add('hidden'); testModal.classList.remove('flex'); }); try { // Make API call to test the provider const response = await fetch(`/storage-providers/${providerId}/test`, { method: 'POST', headers: { 'Content-Type': 'application/json', } }); // Get the HTML response directly const htmlResult = await response.text(); // Replace the test result container with the server-rendered HTML document.getElementById('test-result').innerHTML = htmlResult; } catch (err) { console.error('Error testing provider:', err); document.getElementById('test-result').innerHTML = `

Connection Failed

Network error: Failed to connect to server

`; } }); }); // Handle delete confirmation document.querySelectorAll('.delete-provider-btn').forEach(button => { button.addEventListener('click', (e) => { e.preventDefault(); const dialogId = button.dataset.dialogId; const providerId = button.dataset.providerId; // Clone the template const templateContent = document.getElementById('delete-dialog-template').content.cloneNode(true); const dialog = document.createElement('div'); dialog.setAttribute('id', dialogId); dialog.classList.add('fixed', 'top-0', 'right-0', 'left-0', 'z-50', 'flex', 'justify-center', 'items-center', 'w-full', 'md:inset-0', 'h-[calc(100%-1rem)]', 'max-h-full'); dialog.appendChild(templateContent); document.body.appendChild(dialog); // Add event listeners dialog.querySelector('.delete-confirm-btn').addEventListener('click', async () => { try { const response = await fetch(`/storage-providers/${providerId}`, { method: 'DELETE', headers: { 'Content-Type': 'application/json', 'X-HTTP-Method-Override': 'DELETE' } }); if (response.ok) { // Reload the page to show updated list window.location.reload(); } else { const data = await response.json(); showToast('error', `Failed to delete provider: ${data.message || response.statusText}`); dialog.remove(); } } catch (err) { console.error('Error deleting provider:', err); showToast('error', `Network error: ${err.message || 'Failed to connect to server'}`); dialog.remove(); } }); dialog.querySelector('.cancel-btn').addEventListener('click', () => { dialog.remove(); }); }); }); // Helper function to show toast messages function showToast(type, message) { const toastContainer = document.getElementById('toast-container'); if (!toastContainer) { console.error('Toast container not found'); return; } const toast = document.createElement('div'); toast.className = `flex items-center w-full max-w-xs p-4 mb-4 text-gray-500 bg-white rounded-lg shadow dark:text-gray-400 dark:bg-gray-800 ${type === 'error' ? 'border-l-4 border-red-500' : 'border-l-4 border-green-500'}`; let icon = ''; if (type === 'error') { icon = ``; } else { icon = ``; } toast.innerHTML = `
${icon}
${message}
`; toastContainer.appendChild(toast); // Remove toast after 5 seconds setTimeout(() => { toast.remove(); }, 5000); // Handle close button const closeButton = toast.querySelector('button'); closeButton.addEventListener('click', () => { toast.remove(); }); } });