refactor: Extract devices.templ JavaScript to TypeScript
Removed 470 lines of inline JavaScript from devices.templ: - Extracted all device management functions to device-management.ts - Added x-data="devices" and x-init for event delegation - Converted static onclick handlers to @click - Dynamic content (edit/delete mapping buttons) uses data attributes - Event delegation handles clicks on dynamically generated buttons The device-management.ts already had the updated loadShelfMappings() function with data-action attributes for event delegation.
This commit is contained in:
+28
-421
@@ -9,13 +9,11 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
|
||||
<meta charset="UTF-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||
<title>Device Management - Bookhoard</title>
|
||||
<script src="/static/htmx.min.js"></script>
|
||||
<script src="/static/toast.js"></script>
|
||||
<script src="/static/device-management.js"></script>
|
||||
<link href="/static/style.css" rel="stylesheet"/>
|
||||
<script src="/static/theme.js"></script>
|
||||
<script src="/static/htmx.min.js"></script>
|
||||
<script src="/static/main.js"></script>
|
||||
<link href="/static/style.css" rel="stylesheet"/>
|
||||
</head>
|
||||
<body class="theme-{ user.Theme }">
|
||||
<body class="theme-{ user.Theme }" x-data="devices" x-init="setupEventDelegation()">
|
||||
@Header(user, "/devices")
|
||||
<div class="w-full px-4 sm:px-6 lg:px-8 py-8">
|
||||
<!-- Header Section -->
|
||||
@@ -25,7 +23,7 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
|
||||
<h1 class="text-3xl font-bold" style="color: var(--text-primary)">Device Management</h1>
|
||||
<p style="color: var(--text-secondary)">Manage your reading devices and sync settings</p>
|
||||
</div>
|
||||
<button onclick="showAddDeviceModal()" class="btn-primary px-4 py-2 rounded-lg">
|
||||
<button @click="showAddDeviceModal()" class="btn-primary px-4 py-2 rounded-lg">
|
||||
➕ Add New Device
|
||||
</button>
|
||||
</div>
|
||||
@@ -38,7 +36,7 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
|
||||
<div class="text-6xl mb-4">📱</div>
|
||||
<h3 class="text-xl font-semibold mb-2" style="color: var(--text-primary)">No Devices Yet</h3>
|
||||
<p class="mb-4">Add your reading devices to enable cross-device sync</p>
|
||||
<button onclick="showAddDeviceModal()" class="btn-primary px-4 py-2 rounded-lg">
|
||||
<button @click="showAddDeviceModal()" class="btn-primary px-4 py-2 rounded-lg">
|
||||
Add Your First Device
|
||||
</button>
|
||||
</div>
|
||||
@@ -61,10 +59,10 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
|
||||
}
|
||||
</div>
|
||||
<div class="flex space-x-2">
|
||||
<button onclick="showShelfMappings('{ device.ID }')" class="p-2 hover:opacity-80 rounded" style="color: var(--text-secondary); background-color: var(--bg-primary);">
|
||||
<button @click="showShelfMappings('{ device.ID }')" class="p-2 hover:opacity-80 rounded" style="color: var(--text-secondary); background-color: var(--bg-primary);">
|
||||
📚
|
||||
</button>
|
||||
<button onclick="showDeviceSettings('{ device.ID }')" class="p-2 hover:opacity-80 rounded" style="color: var(--text-secondary); background-color: var(--bg-primary);">
|
||||
<button @click="showDeviceSettings('{ device.ID }')" class="p-2 hover:opacity-80 rounded" style="color: var(--text-secondary); background-color: var(--bg-primary);">
|
||||
⚙️
|
||||
</button>
|
||||
</div>
|
||||
@@ -97,11 +95,9 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- NEW: Sync URL & Token Management -->
|
||||
<div class="border-t pt-4" style="border-color: var(--border);">
|
||||
<p class="text-xs font-semibold mb-2" style="color: var(--text-secondary)">DEVICE SYNC CONFIGURATION</p>
|
||||
|
||||
if device.DeviceType == "kobo" {
|
||||
<!-- Kobo: Copy Full Sync URL -->
|
||||
<div class="mb-3">
|
||||
@@ -116,7 +112,7 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
|
||||
style="background-color: var(--bg-primary); color: var(--text-primary); border-color: var(--border);"
|
||||
/>
|
||||
<button
|
||||
onclick="copyToClipboard(document.getElementById('sync-url-{ device.ID }').value, 'Kobo sync URL', event)"
|
||||
@click="copyToClipboard(document.getElementById('sync-url-{ device.ID }').value, 'Kobo sync URL')"
|
||||
class="px-3 py-2 text-xs rounded hover:opacity-80"
|
||||
style="background-color: var(--accent); color: white;"
|
||||
>
|
||||
@@ -126,7 +122,6 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
|
||||
<p class="text-xs mt-1" style="color: var(--text-secondary);">Paste this URL into Kobo's <code class="px-1 py-0.5 rounded" style="background-color: var(--bg-primary);">api_endpoint</code> setting</p>
|
||||
</div>
|
||||
}
|
||||
|
||||
if device.DeviceType == "koreader" {
|
||||
<!-- KOReader: Copy Auth Token -->
|
||||
<div class="mb-3">
|
||||
@@ -141,7 +136,7 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
|
||||
style="background-color: var(--bg-primary); color: var(--text-primary); border-color: var(--border); font-family: monospace;"
|
||||
/>
|
||||
<button
|
||||
onclick="copyToClipboard(document.getElementById('auth-token-{ device.ID }').value, 'Auth token', event)"
|
||||
@click="copyToClipboard(document.getElementById('auth-token-{ device.ID }').value, 'Auth token')"
|
||||
class="px-3 py-2 text-xs rounded hover:opacity-80"
|
||||
style="background-color: var(--accent); color: white;"
|
||||
>
|
||||
@@ -151,10 +146,9 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
|
||||
<p class="text-xs mt-1" style="color: var(--text-secondary);">Enter this token in the KOReader plugin settings</p>
|
||||
</div>
|
||||
}
|
||||
|
||||
<!-- Regenerate Token Button -->
|
||||
<button
|
||||
onclick="regenerateDeviceToken('{ device.ID }', event)"
|
||||
@click="regenerateDeviceToken('{ device.ID }', $event)"
|
||||
class="w-full px-3 py-2 text-xs rounded border hover:opacity-80"
|
||||
style="border-color: var(--border); color: var(--text-secondary); background-color: var(--bg-primary);"
|
||||
>
|
||||
@@ -181,10 +175,10 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
|
||||
</p>
|
||||
</div>
|
||||
<div class="flex space-x-2">
|
||||
<button onclick="approveDevice('{ reg.RegistrationID }')" class="btn-primary px-4 py-2 rounded-lg text-sm">
|
||||
<button @click="approveDevice('{ reg.RegistrationID }')" class="btn-primary px-4 py-2 rounded-lg text-sm">
|
||||
Approve
|
||||
</button>
|
||||
<button onclick="rejectDevice('{ reg.RegistrationID }')" class="btn-danger px-4 py-2 rounded-lg text-sm">
|
||||
<button @click="rejectDevice('{ reg.RegistrationID }')" class="btn-danger px-4 py-2 rounded-lg text-sm">
|
||||
Reject
|
||||
</button>
|
||||
</div>
|
||||
@@ -197,7 +191,7 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
|
||||
<div id="sync-queue-section" class="mt-12 hidden">
|
||||
<div class="flex justify-between items-center mb-4">
|
||||
<h2 class="text-2xl font-bold" style="color: var(--text-primary)">Sync Queue</h2>
|
||||
<button onclick="clearSyncQueue()" class="btn-secondary px-4 py-2 rounded-lg">
|
||||
<button @click="clearSyncQueue()" class="btn-secondary px-4 py-2 rounded-lg">
|
||||
Clear Queue
|
||||
</button>
|
||||
</div>
|
||||
@@ -209,9 +203,9 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
|
||||
<div class="card rounded-lg p-6 w-full max-w-md mx-4" style="background-color: var(--bg-secondary); border-color: var(--border);">
|
||||
<div class="flex justify-between items-center mb-6">
|
||||
<h2 class="text-xl font-bold" style="color: var(--text-primary)">Add New Device</h2>
|
||||
<button onclick="hideAddDeviceModal()" class="p-2 hover:opacity-80 rounded" style="color: var(--text-primary)">✕</button>
|
||||
<button @click="hideAddDeviceModal()" class="p-2 hover:opacity-80 rounded" style="color: var(--text-primary)">✕</button>
|
||||
</div>
|
||||
<form id="add-device-form" onsubmit="handleAddDevice(event)">
|
||||
<form id="add-device-form" @submit="handleAddDevice($event)">
|
||||
<div class="mb-4">
|
||||
<label class="block text-sm font-medium mb-2" style="color: var(--text-secondary)">Device Name</label>
|
||||
<input
|
||||
@@ -251,7 +245,7 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
|
||||
<p class="text-xs mt-1" style="color: var(--text-secondary)">Enter your device's unique identifier</p>
|
||||
</div>
|
||||
<div class="flex justify-end space-x-3">
|
||||
<button type="button" onclick="hideAddDeviceModal()" class="btn-secondary px-4 py-2 rounded-lg">
|
||||
<button type="button" @click="hideAddDeviceModal()" class="btn-secondary px-4 py-2 rounded-lg">
|
||||
Cancel
|
||||
</button>
|
||||
<button type="submit" class="btn-primary px-4 py-2 rounded-lg">
|
||||
@@ -266,9 +260,9 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
|
||||
<div class="card rounded-lg p-6 w-full max-w-md mx-4" style="background-color: var(--bg-secondary); border-color: var(--border);">
|
||||
<div class="flex justify-between items-center mb-6">
|
||||
<h2 class="text-xl font-bold" style="color: var(--text-primary)">Device Settings</h2>
|
||||
<button onclick="hideDeviceSettingsModal()" class="p-2 hover:opacity-80 rounded" style="color: var(--text-primary)">✕</button>
|
||||
<button @click="hideDeviceSettingsModal()" class="p-2 hover:opacity-80 rounded" style="color: var(--text-primary)">✕</button>
|
||||
</div>
|
||||
<form id="device-settings-form" onsubmit="handleSaveDeviceSettings(event)">
|
||||
<form id="device-settings-form" @submit="handleSaveDeviceSettings($event)">
|
||||
<input type="hidden" id="settings-device-id"/>
|
||||
<input type="hidden" id="settings-device-type"/>
|
||||
<div class="mb-4">
|
||||
@@ -385,10 +379,10 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex justify-end space-x-3">
|
||||
<button type="button" onclick="hideDeviceSettingsModal()" class="btn-secondary px-4 py-2 rounded-lg">
|
||||
<button type="button" @click="hideDeviceSettingsModal()" class="btn-secondary px-4 py-2 rounded-lg">
|
||||
Cancel
|
||||
</button>
|
||||
<button type="button" onclick="handleRevokeDevice()" class="btn-danger px-4 py-2 rounded-lg">
|
||||
<button type="button" @click="handleRevokeDevice()" class="btn-danger px-4 py-2 rounded-lg">
|
||||
Revoke Device
|
||||
</button>
|
||||
<button type="submit" class="btn-primary px-4 py-2 rounded-lg">
|
||||
@@ -403,17 +397,17 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
|
||||
<div class="card rounded-lg p-6 w-full max-w-2xl mx-4 max-h-[90vh] overflow-y-auto" style="background-color: var(--bg-secondary); border-color: var(--border);">
|
||||
<div class="flex justify-between items-center mb-6">
|
||||
<h2 class="text-xl font-bold" style="color: var(--text-primary)">Collection to Shelf Mappings</h2>
|
||||
<button onclick="hideShelfMappingsModal()" class="p-2 hover:opacity-80 rounded" style="color: var(--text-primary)">✕</button>
|
||||
<button @click="hideShelfMappingsModal()" class="p-2 hover:opacity-80 rounded" style="color: var(--text-primary)">✕</button>
|
||||
</div>
|
||||
<input type="hidden" id="mappings-device-id"/>
|
||||
<div id="shelf-mappings-container" class="space-y-4 mb-6">
|
||||
<p style="color: var(--text-secondary)">Loading mappings...</p>
|
||||
</div>
|
||||
<div class="flex justify-end space-x-3">
|
||||
<button type="button" onclick="hideShelfMappingsModal()" class="btn-secondary px-4 py-2 rounded-lg">
|
||||
<button type="button" @click="hideShelfMappingsModal()" class="btn-secondary px-4 py-2 rounded-lg">
|
||||
Close
|
||||
</button>
|
||||
<button type="button" onclick="showAddMappingModal()" class="btn-primary px-4 py-2 rounded-lg">
|
||||
<button type="button" @click="showAddMappingModal()" class="btn-primary px-4 py-2 rounded-lg">
|
||||
➕ Add Mapping
|
||||
</button>
|
||||
</div>
|
||||
@@ -424,9 +418,9 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
|
||||
<div class="card rounded-lg p-6 w-full max-w-md mx-4" style="background-color: var(--bg-secondary); border-color: var(--border);">
|
||||
<div class="flex justify-between items-center mb-6">
|
||||
<h2 class="text-xl font-bold" style="color: var(--text-primary)">Add Collection Mapping</h2>
|
||||
<button onclick="hideAddMappingModal()" class="p-2 hover:opacity-80 rounded" style="color: var(--text-primary)">✕</button>
|
||||
<button @click="hideAddMappingModal()" class="p-2 hover:opacity-80 rounded" style="color: var(--text-primary)">✕</button>
|
||||
</div>
|
||||
<form id="mapping-form" onsubmit="handleSaveMapping(event)">
|
||||
<form id="mapping-form" @submit="handleSaveMapping($event)">
|
||||
<input type="hidden" id="mapping-device-id"/>
|
||||
<input type="hidden" id="mapping-id"/>
|
||||
<div class="mb-4">
|
||||
@@ -467,7 +461,7 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
|
||||
</select>
|
||||
</div>
|
||||
<div class="flex justify-end space-x-3">
|
||||
<button type="button" onclick="hideAddMappingModal()" class="btn-secondary px-4 py-2 rounded-lg">
|
||||
<button type="button" @click="hideAddMappingModal()" class="btn-secondary px-4 py-2 rounded-lg">
|
||||
Cancel
|
||||
</button>
|
||||
<button type="submit" class="btn-primary px-4 py-2 rounded-lg">
|
||||
@@ -477,394 +471,7 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
function getDeviceIcon(typeName) {
|
||||
const deviceIcons = {
|
||||
'koreader': '📖',
|
||||
'kobo': '📚',
|
||||
'web': '🌐',
|
||||
'mobile': '📱'
|
||||
};
|
||||
return deviceIcons[typeName] || '📱';
|
||||
}
|
||||
|
||||
function showAddDeviceModal() {
|
||||
document.getElementById('add-device-modal').classList.remove('hidden');
|
||||
}
|
||||
|
||||
function hideAddDeviceModal() {
|
||||
document.getElementById('add-device-modal').classList.add('hidden');
|
||||
document.getElementById('add-device-form').reset();
|
||||
}
|
||||
|
||||
function handleAddDevice(event) {
|
||||
event.preventDefault();
|
||||
|
||||
const data = {
|
||||
device_name: document.getElementById('device-name').value,
|
||||
device_type: document.getElementById('device-type').value,
|
||||
device_identifier: document.getElementById('device-identifier').value
|
||||
};
|
||||
|
||||
fetch('/api/devices/register', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + localStorage.getItem('token')
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(result => {
|
||||
showToast('Device registered! Check your device for sync instructions.', 'success');
|
||||
hideAddDeviceModal();
|
||||
loadDevices();
|
||||
})
|
||||
.catch(error => {
|
||||
showToast('Failed to register device', 'error');
|
||||
});
|
||||
}
|
||||
|
||||
function showDeviceSettings(deviceId) {
|
||||
const device = devices.find(d => d.id === deviceId);
|
||||
if (!device) return;
|
||||
|
||||
document.getElementById('settings-device-id').value = deviceId;
|
||||
document.getElementById('settings-device-type').value = device.device_type;
|
||||
document.getElementById('settings-device-name').value = device.device_name;
|
||||
document.getElementById('settings-sync-enabled').checked = device.sync_enabled;
|
||||
document.getElementById('settings-auto-sync').checked = device.auto_sync;
|
||||
document.getElementById('settings-sync-frequency').value = device.sync_frequency_minutes;
|
||||
|
||||
const deviceType = device.device_type;
|
||||
|
||||
fetch('/api/collections', {
|
||||
headers: { 'Authorization': 'Bearer ' + localStorage.getItem('token') }
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(result => {
|
||||
if (result.collections && result.collections.length > 0) {
|
||||
const firstCollection = result.collections[0];
|
||||
const viewSettings = firstCollection.view_settings || {};
|
||||
const deviceSettings = viewSettings[deviceType] || {};
|
||||
|
||||
document.getElementById('settings-view-mode').value = deviceSettings.view_mode || 'grid';
|
||||
document.getElementById('settings-sort-order').value = deviceSettings.sort_order || 'name';
|
||||
document.getElementById('settings-items-per-page').value = deviceSettings.items_per_page || 24;
|
||||
document.getElementById('settings-show-covers').checked = deviceSettings.show_covers !== false;
|
||||
document.getElementById('settings-show-progress').checked = deviceSettings.show_progress || false;
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Failed to load view settings:', error);
|
||||
});
|
||||
|
||||
document.getElementById('device-settings-modal').classList.remove('hidden');
|
||||
}
|
||||
|
||||
function showShelfMappings(deviceId) {
|
||||
document.getElementById('mappings-device-id').value = deviceId;
|
||||
loadShelfMappings(deviceId);
|
||||
document.getElementById('shelf-mappings-modal').classList.remove('hidden');
|
||||
}
|
||||
|
||||
function hideShelfMappingsModal() {
|
||||
document.getElementById('shelf-mappings-modal').classList.add('hidden');
|
||||
}
|
||||
|
||||
function loadShelfMappings(deviceId) {
|
||||
const container = document.getElementById('shelf-mappings-container');
|
||||
|
||||
fetch(`/api/devices/${deviceId}/collections`, {
|
||||
headers: { 'Authorization': 'Bearer ' + localStorage.getItem('token') }
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(result => {
|
||||
if (result.mappings && result.mappings.length > 0) {
|
||||
container.innerHTML = result.mappings.map(mapping => `
|
||||
<div class="card p-4 rounded-lg border" style="background-color: var(--bg-primary); border-color: var(--border);">
|
||||
<div class="flex justify-between items-center">
|
||||
<div>
|
||||
<h4 class="font-semibold" style="color: var(--text-primary)">${mapping.collection_name}</h4>
|
||||
<p class="text-sm" style="color: var(--text-secondary)">
|
||||
→ Device Shelf: <strong>${mapping.device_shelf_name}</strong>
|
||||
</p>
|
||||
<p class="text-xs" style="color: var(--text-secondary)">
|
||||
Sync: ${mapping.sync_direction}
|
||||
</p>
|
||||
</div>
|
||||
<div class="flex space-x-2">
|
||||
<button onclick="editMapping('${mapping.id}', '${mapping.collection_id}', '${mapping.device_shelf_name}', '${mapping.sync_direction}')"
|
||||
class="p-2 hover:opacity-80 rounded" style="color: var(--text-secondary); background-color: var(--bg-secondary);">
|
||||
✏️
|
||||
</button>
|
||||
<button onclick="deleteMapping('${mapping.id}')"
|
||||
class="p-2 hover:opacity-80 rounded" style="color: var(--text-secondary); background-color: var(--bg-secondary);">
|
||||
🗑️
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`).join('');
|
||||
} else {
|
||||
container.innerHTML = '<p style="color: var(--text-secondary)">No shelf mappings configured. Click "Add Mapping" to create one.</p>';
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
container.innerHTML = '<p style="color: var(--error)">Failed to load mappings</p>';
|
||||
});
|
||||
}
|
||||
|
||||
function showAddMappingModal() {
|
||||
const deviceId = document.getElementById('mappings-device-id').value;
|
||||
document.getElementById('mapping-device-id').value = deviceId;
|
||||
document.getElementById('mapping-id').value = '';
|
||||
document.getElementById('mapping-collection').value = '';
|
||||
document.getElementById('mapping-shelf-name').value = '';
|
||||
document.getElementById('mapping-sync-direction').value = 'bidirectional';
|
||||
|
||||
loadCollections();
|
||||
document.getElementById('add-mapping-modal').classList.remove('hidden');
|
||||
}
|
||||
|
||||
function hideAddMappingModal() {
|
||||
document.getElementById('add-mapping-modal').classList.add('hidden');
|
||||
document.getElementById('mapping-form').reset();
|
||||
}
|
||||
|
||||
function loadCollections() {
|
||||
const select = document.getElementById('mapping-collection');
|
||||
|
||||
fetch('/api/collections', {
|
||||
headers: { 'Authorization': 'Bearer ' + localStorage.getItem('token') }
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(result => {
|
||||
if (result.collections) {
|
||||
select.innerHTML = '<option value="">Select collection...</option>' +
|
||||
result.collections.map(col => `<option value="${col.id}">${col.name}</option>`).join('');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Failed to load collections', error);
|
||||
});
|
||||
}
|
||||
|
||||
function editMapping(mappingId, collectionId, shelfName, syncDirection) {
|
||||
document.getElementById('mapping-id').value = mappingId;
|
||||
document.getElementById('mapping-collection').value = collectionId;
|
||||
document.getElementById('mapping-shelf-name').value = shelfName;
|
||||
document.getElementById('mapping-sync-direction').value = syncDirection;
|
||||
|
||||
document.getElementById('add-mapping-modal').classList.remove('hidden');
|
||||
}
|
||||
|
||||
function handleSaveMapping(event) {
|
||||
event.preventDefault();
|
||||
|
||||
const deviceId = document.getElementById('mapping-device-id').value;
|
||||
const mappingId = document.getElementById('mapping-id').value;
|
||||
const isUpdate = mappingId !== '';
|
||||
|
||||
const data = {
|
||||
collection_id: document.getElementById('mapping-collection').value,
|
||||
device_shelf_name: document.getElementById('mapping-shelf-name').value,
|
||||
sync_direction: document.getElementById('mapping-sync-direction').value
|
||||
};
|
||||
|
||||
const url = isUpdate ? `/api/devices/${deviceId}/collections/${mappingId}` : `/api/devices/${deviceId}/collections`;
|
||||
const method = isUpdate ? 'PUT' : 'POST';
|
||||
|
||||
fetch(url, {
|
||||
method: method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + localStorage.getItem('token')
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(result => {
|
||||
showToast(isUpdate ? 'Mapping updated' : 'Mapping created', 'success');
|
||||
hideAddMappingModal();
|
||||
loadShelfMappings(deviceId);
|
||||
})
|
||||
.catch(error => {
|
||||
showToast('Failed to save mapping', 'error');
|
||||
});
|
||||
}
|
||||
|
||||
function deleteMapping(mappingId) {
|
||||
if (!confirm('Are you sure you want to delete this mapping?')) return;
|
||||
|
||||
const deviceId = document.getElementById('mappings-device-id').value;
|
||||
|
||||
fetch(`/api/devices/${deviceId}/collections/${mappingId}`, {
|
||||
method: 'DELETE',
|
||||
headers: { 'Authorization': 'Bearer ' + localStorage.getItem('token') }
|
||||
})
|
||||
.then(response => {
|
||||
if (response.ok) {
|
||||
showToast('Mapping deleted', 'success');
|
||||
loadShelfMappings(deviceId);
|
||||
} else {
|
||||
showToast('Failed to delete mapping', 'error');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
showToast('Failed to delete mapping', 'error');
|
||||
});
|
||||
}
|
||||
|
||||
function hideDeviceSettingsModal() {
|
||||
document.getElementById('device-settings-modal').classList.add('hidden');
|
||||
}
|
||||
|
||||
function handleSaveDeviceSettings(event) {
|
||||
event.preventDefault();
|
||||
|
||||
const deviceId = document.getElementById('settings-device-id').value;
|
||||
const deviceType = document.getElementById('settings-device-type')?.value || '';
|
||||
|
||||
const deviceData = {
|
||||
device_name: document.getElementById('settings-device-name').value,
|
||||
sync_enabled: document.getElementById('settings-sync-enabled').checked,
|
||||
auto_sync: document.getElementById('settings-auto-sync').checked,
|
||||
sync_frequency_minutes: parseInt(document.getElementById('settings-sync-frequency').value)
|
||||
};
|
||||
|
||||
const viewSettings = {
|
||||
view_mode: document.getElementById('settings-view-mode').value,
|
||||
sort_order: document.getElementById('settings-sort-order').value,
|
||||
items_per_page: parseInt(document.getElementById('settings-items-per-page').value),
|
||||
show_covers: document.getElementById('settings-show-covers').checked,
|
||||
show_progress: document.getElementById('settings-show-progress').checked
|
||||
};
|
||||
|
||||
fetch(`/api/devices/${deviceId}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + localStorage.getItem('token')
|
||||
},
|
||||
body: JSON.stringify(deviceData)
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(result => {
|
||||
return fetch('/api/collections', {
|
||||
method: 'GET',
|
||||
headers: { 'Authorization': 'Bearer ' + localStorage.getItem('token') }
|
||||
});
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(result => {
|
||||
const updatePromises = result.collections.map(collection => {
|
||||
const currentSettings = collection.view_settings || {};
|
||||
currentSettings[deviceType] = viewSettings;
|
||||
|
||||
return fetch(`/api/collections/${collection.id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + localStorage.getItem('token')
|
||||
},
|
||||
body: JSON.stringify({
|
||||
...collection,
|
||||
view_settings: currentSettings
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
return Promise.all(updatePromises);
|
||||
})
|
||||
.then(() => {
|
||||
showToast('Device settings saved', 'success');
|
||||
hideDeviceSettingsModal();
|
||||
location.reload();
|
||||
})
|
||||
.catch(error => {
|
||||
showToast('Failed to save settings', 'error');
|
||||
});
|
||||
}
|
||||
|
||||
function handleRevokeDevice() {
|
||||
const deviceId = document.getElementById('settings-device-id').value;
|
||||
if (!confirm('Are you sure you want to revoke this device? It will no longer be able to sync.')) return;
|
||||
|
||||
fetch(`/api/devices/${deviceId}`, {
|
||||
method: 'DELETE',
|
||||
headers: { 'Authorization': 'Bearer ' + localStorage.getItem('token') }
|
||||
})
|
||||
.then(response => {
|
||||
if (response.ok) {
|
||||
showToast('Device revoked successfully', 'success');
|
||||
hideDeviceSettingsModal();
|
||||
location.reload();
|
||||
} else {
|
||||
showToast('Failed to revoke device', 'error');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
showToast('Failed to revoke device', 'error');
|
||||
});
|
||||
}
|
||||
|
||||
function approveDevice(registrationId) {
|
||||
fetch(`/api/devices/approve/${registrationId}`, {
|
||||
method: 'GET',
|
||||
headers: { 'Authorization': 'Bearer ' + localStorage.getItem('token') }
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(result => {
|
||||
showToast('Device approved successfully', 'success');
|
||||
location.reload();
|
||||
})
|
||||
.catch(error => {
|
||||
showToast('Failed to approve device', 'error');
|
||||
});
|
||||
}
|
||||
|
||||
function rejectDevice(registrationId) {
|
||||
fetch(`/api/devices/reject/${registrationId}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Authorization': 'Bearer ' + localStorage.getItem('token') }
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(result => {
|
||||
showToast('Device registration rejected', 'info');
|
||||
location.reload();
|
||||
})
|
||||
.catch(error => {
|
||||
showToast('Failed to reject device', 'error');
|
||||
});
|
||||
}
|
||||
|
||||
function clearSyncQueue() {
|
||||
if (!confirm('Are you sure you want to clear all sync queue items?')) return;
|
||||
|
||||
fetch('/api/queue/clear', {
|
||||
method: 'DELETE',
|
||||
headers: { 'Authorization': 'Bearer ' + localStorage.getItem('token') }
|
||||
})
|
||||
.then(response => {
|
||||
if (response.ok) {
|
||||
showToast('Sync queue cleared', 'success');
|
||||
} else {
|
||||
showToast('Failed to clear queue', 'error');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function logout() {
|
||||
localStorage.removeItem('token');
|
||||
window.location.href = '/login';
|
||||
}
|
||||
|
||||
// Load devices on page load
|
||||
loadDevices();
|
||||
</script>
|
||||
|
||||
@ErrorToast(errorMessage)
|
||||
@ErrorToast(errorMessage)
|
||||
</body>
|
||||
</html>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user