Update public/js/main.js
Browse files- public/js/main.js +18 -17
public/js/main.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
// Initialize datepicker and load connections on page load
|
| 2 |
document.addEventListener('DOMContentLoaded', () => {
|
| 3 |
// Initialize flatpickr for all date inputs
|
| 4 |
flatpickr('.datepicker', {
|
|
@@ -18,7 +17,6 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 18 |
// Load connections from localStorage first for instant display
|
| 19 |
const storedConnections = localStorage.getItem('connections');
|
| 20 |
if (storedConnections) {
|
| 21 |
-
console.log('Found stored connections:', JSON.parse(storedConnections));
|
| 22 |
displayConnections(JSON.parse(storedConnections));
|
| 23 |
}
|
| 24 |
|
|
@@ -29,12 +27,21 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 29 |
// Function to load connections from server
|
| 30 |
async function loadConnections() {
|
| 31 |
try {
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
if (response.ok) {
|
| 34 |
const connections = await response.json();
|
| 35 |
-
console.log('Fetched connections from server:', connections);
|
| 36 |
localStorage.setItem('connections', JSON.stringify(connections));
|
| 37 |
displayConnections(connections);
|
|
|
|
| 38 |
} else {
|
| 39 |
toastr.error('Failed to load connections');
|
| 40 |
}
|
|
@@ -47,7 +54,6 @@ async function loadConnections() {
|
|
| 47 |
// Function to display connections in the table
|
| 48 |
function displayConnections(connections) {
|
| 49 |
const tbody = document.getElementById('connectionsList');
|
| 50 |
-
console.log('Displaying connections:', connections);
|
| 51 |
|
| 52 |
if (!connections || !connections.length) {
|
| 53 |
tbody.innerHTML = `
|
|
@@ -100,7 +106,6 @@ function hideAddForm() {
|
|
| 100 |
|
| 101 |
// Function to populate edit form
|
| 102 |
function editConnection(connection) {
|
| 103 |
-
console.log('Editing connection:', connection);
|
| 104 |
document.getElementById('formTitle').textContent = 'Edit IPTV Connection';
|
| 105 |
document.getElementById('submitButton').textContent = 'Update';
|
| 106 |
const form = document.getElementById('connectionForm');
|
|
@@ -146,7 +151,6 @@ document.getElementById('connectionForm')?.addEventListener('submit', async (e)
|
|
| 146 |
data.targetUrl = validateUrl(data.targetUrl);
|
| 147 |
|
| 148 |
try {
|
| 149 |
-
console.log('Submitting form data:', { id, ...data });
|
| 150 |
let response;
|
| 151 |
if (id) {
|
| 152 |
response = await fetch(`/api/connections/${id}`, {
|
|
@@ -165,16 +169,13 @@ document.getElementById('connectionForm')?.addEventListener('submit', async (e)
|
|
| 165 |
const result = await response.json();
|
| 166 |
|
| 167 |
if (response.ok) {
|
| 168 |
-
console.log('Server response:', result);
|
| 169 |
toastr.success(id ? 'Connection updated successfully' : 'Connection added successfully');
|
| 170 |
hideAddForm();
|
| 171 |
-
loadConnections();
|
| 172 |
} else {
|
| 173 |
-
console.error('Server error:', result);
|
| 174 |
toastr.error(result.error || 'Failed to save connection');
|
| 175 |
}
|
| 176 |
} catch (error) {
|
| 177 |
-
console.error('Error saving connection:', error);
|
| 178 |
toastr.error('Failed to save connection');
|
| 179 |
}
|
| 180 |
});
|
|
@@ -196,7 +197,6 @@ async function deleteConnection(id) {
|
|
| 196 |
toastr.error(error.error || 'Failed to delete connection');
|
| 197 |
}
|
| 198 |
} catch (error) {
|
| 199 |
-
console.error('Error deleting connection:', error);
|
| 200 |
toastr.error('Failed to delete connection');
|
| 201 |
}
|
| 202 |
}
|
|
@@ -212,7 +212,6 @@ async function logout() {
|
|
| 212 |
toastr.error('Failed to logout');
|
| 213 |
}
|
| 214 |
} catch (error) {
|
| 215 |
-
console.error('Error during logout:', error);
|
| 216 |
toastr.error('Failed to logout');
|
| 217 |
}
|
| 218 |
}
|
|
@@ -258,13 +257,15 @@ function copyToClipboard(text) {
|
|
| 258 |
});
|
| 259 |
}
|
| 260 |
|
| 261 |
-
//
|
| 262 |
-
function
|
| 263 |
document.querySelectorAll('[data-copy]').forEach(element => {
|
| 264 |
const value = element.getAttribute('data-copy');
|
| 265 |
const button = document.createElement('button');
|
| 266 |
-
button.innerHTML = `<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"
|
| 267 |
-
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
|
|
|
|
|
|
| 268 |
</svg>`;
|
| 269 |
button.className = 'ml-2 text-gray-500 hover:text-gray-700';
|
| 270 |
button.onclick = () => copyToClipboard(value);
|
|
|
|
|
|
|
| 1 |
document.addEventListener('DOMContentLoaded', () => {
|
| 2 |
// Initialize flatpickr for all date inputs
|
| 3 |
flatpickr('.datepicker', {
|
|
|
|
| 17 |
// Load connections from localStorage first for instant display
|
| 18 |
const storedConnections = localStorage.getItem('connections');
|
| 19 |
if (storedConnections) {
|
|
|
|
| 20 |
displayConnections(JSON.parse(storedConnections));
|
| 21 |
}
|
| 22 |
|
|
|
|
| 27 |
// Function to load connections from server
|
| 28 |
async function loadConnections() {
|
| 29 |
try {
|
| 30 |
+
// Clear local storage before fetching
|
| 31 |
+
localStorage.removeItem('connections');
|
| 32 |
+
|
| 33 |
+
const response = await fetch('/api/connections', {
|
| 34 |
+
headers: {
|
| 35 |
+
'Cache-Control': 'no-cache',
|
| 36 |
+
'Pragma': 'no-cache'
|
| 37 |
+
}
|
| 38 |
+
});
|
| 39 |
+
|
| 40 |
if (response.ok) {
|
| 41 |
const connections = await response.json();
|
|
|
|
| 42 |
localStorage.setItem('connections', JSON.stringify(connections));
|
| 43 |
displayConnections(connections);
|
| 44 |
+
toastr.success('Connections refreshed');
|
| 45 |
} else {
|
| 46 |
toastr.error('Failed to load connections');
|
| 47 |
}
|
|
|
|
| 54 |
// Function to display connections in the table
|
| 55 |
function displayConnections(connections) {
|
| 56 |
const tbody = document.getElementById('connectionsList');
|
|
|
|
| 57 |
|
| 58 |
if (!connections || !connections.length) {
|
| 59 |
tbody.innerHTML = `
|
|
|
|
| 106 |
|
| 107 |
// Function to populate edit form
|
| 108 |
function editConnection(connection) {
|
|
|
|
| 109 |
document.getElementById('formTitle').textContent = 'Edit IPTV Connection';
|
| 110 |
document.getElementById('submitButton').textContent = 'Update';
|
| 111 |
const form = document.getElementById('connectionForm');
|
|
|
|
| 151 |
data.targetUrl = validateUrl(data.targetUrl);
|
| 152 |
|
| 153 |
try {
|
|
|
|
| 154 |
let response;
|
| 155 |
if (id) {
|
| 156 |
response = await fetch(`/api/connections/${id}`, {
|
|
|
|
| 169 |
const result = await response.json();
|
| 170 |
|
| 171 |
if (response.ok) {
|
|
|
|
| 172 |
toastr.success(id ? 'Connection updated successfully' : 'Connection added successfully');
|
| 173 |
hideAddForm();
|
| 174 |
+
await loadConnections(); // Reload the connections list
|
| 175 |
} else {
|
|
|
|
| 176 |
toastr.error(result.error || 'Failed to save connection');
|
| 177 |
}
|
| 178 |
} catch (error) {
|
|
|
|
| 179 |
toastr.error('Failed to save connection');
|
| 180 |
}
|
| 181 |
});
|
|
|
|
| 197 |
toastr.error(error.error || 'Failed to delete connection');
|
| 198 |
}
|
| 199 |
} catch (error) {
|
|
|
|
| 200 |
toastr.error('Failed to delete connection');
|
| 201 |
}
|
| 202 |
}
|
|
|
|
| 212 |
toastr.error('Failed to logout');
|
| 213 |
}
|
| 214 |
} catch (error) {
|
|
|
|
| 215 |
toastr.error('Failed to logout');
|
| 216 |
}
|
| 217 |
}
|
|
|
|
| 257 |
});
|
| 258 |
}
|
| 259 |
|
| 260 |
+
// Initialize copy buttons if needed
|
| 261 |
+
function initializeCopyButtons() {
|
| 262 |
document.querySelectorAll('[data-copy]').forEach(element => {
|
| 263 |
const value = element.getAttribute('data-copy');
|
| 264 |
const button = document.createElement('button');
|
| 265 |
+
button.innerHTML = `<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
| 266 |
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
| 267 |
+
d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z">
|
| 268 |
+
</path>
|
| 269 |
</svg>`;
|
| 270 |
button.className = 'ml-2 text-gray-500 hover:text-gray-700';
|
| 271 |
button.onclick = () => copyToClipboard(value);
|