Skip to content

Commit bbfee7a

Browse files
committed
Enhance Plex account linking and settings auto-save functionality
- Updated the auto-save logic to differentiate between general settings and specific app settings, improving clarity and functionality. - Modified the Plex account linking process to include error handling and improved response management, ensuring a smoother user experience. - Adjusted API responses to include the new 'pin' field for better integration with the front-end logic.
1 parent ed079f9 commit bbfee7a

File tree

3 files changed

+61
-14
lines changed

3 files changed

+61
-14
lines changed

frontend/static/js/new-main.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,14 +1273,24 @@ let huntarrUI = {
12731273
return;
12741274
}
12751275

1276+
// Determine what type of settings we're saving
12761277
const app = this.currentSettingsTab;
1277-
if (!app) {
1278+
const isGeneralSettings = this.currentSection === 'settings' && !app;
1279+
1280+
if (!app && !isGeneralSettings) {
12781281
console.log('[huntarrUI] No current settings tab for auto-save');
12791282
return;
12801283
}
12811284

1282-
console.log(`[huntarrUI] Triggering immediate settings auto-save for: ${app}`);
1283-
this.autoSaveSettings(app);
1285+
if (isGeneralSettings) {
1286+
console.log('[huntarrUI] Triggering immediate general settings auto-save');
1287+
this.autoSaveGeneralSettings(true).catch(error => {
1288+
console.error('[huntarrUI] General settings auto-save failed:', error);
1289+
});
1290+
} else {
1291+
console.log(`[huntarrUI] Triggering immediate settings auto-save for: ${app}`);
1292+
this.autoSaveSettings(app);
1293+
}
12841294
},
12851295

12861296
// Auto-save settings function
@@ -1563,8 +1573,8 @@ let huntarrUI = {
15631573
});
15641574
},
15651575

1566-
// Auto-save general settings (used by test notification)
1567-
autoSaveGeneralSettings: function() {
1576+
// Auto-save general settings (used by test notification and auto-save)
1577+
autoSaveGeneralSettings: function(silent = false) {
15681578
console.log('[huntarrUI] Auto-saving general settings...');
15691579

15701580
return new Promise((resolve, reject) => {

frontend/static/js/user.js

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -330,15 +330,23 @@ class UserModule {
330330

331331
async linkPlexAccount() {
332332
try {
333-
const response = await fetch('./api/auth/plex/pin', { method: 'POST' });
333+
const response = await fetch('./api/auth/plex/pin', {
334+
method: 'POST',
335+
headers: {
336+
'Content-Type': 'application/json'
337+
},
338+
body: JSON.stringify({
339+
user_mode: true
340+
})
341+
});
334342
const result = await response.json();
335343

336344
if (response.ok) {
337345
document.getElementById('plexLinkPinCode').textContent = result.pin;
338346
document.getElementById('plexLinkStatus').textContent = 'Waiting for authentication...';
339347
document.getElementById('plexLinkModal').style.display = 'flex';
340348

341-
this.plexPinId = result.id;
349+
this.plexPinId = result.pin_id;
342350
this.startPlexPolling();
343351
} else {
344352
const statusElement = document.getElementById('plexMainPageStatus');
@@ -356,14 +364,42 @@ class UserModule {
356364
const response = await fetch(`./api/auth/plex/check/${this.plexPinId}`, { method: 'GET' });
357365
const result = await response.json();
358366

359-
if (response.ok && result.success) {
360-
document.getElementById('plexLinkStatus').textContent = 'Successfully linked!';
361-
document.getElementById('plexLinkStatus').className = 'plex-status success';
367+
if (response.ok && result.success && result.claimed) {
368+
// PIN has been claimed, now link the account
369+
document.getElementById('plexLinkStatus').textContent = 'Linking account...';
362370

363-
setTimeout(() => {
364-
this.cancelPlexLink();
365-
this.loadUserData(); // Refresh user data to show linked account
366-
}, 2000);
371+
try {
372+
const linkResponse = await fetch('./api/auth/plex/link', {
373+
method: 'POST',
374+
headers: {
375+
'Content-Type': 'application/json'
376+
},
377+
body: JSON.stringify({
378+
token: result.token
379+
})
380+
});
381+
382+
const linkResult = await linkResponse.json();
383+
384+
if (linkResponse.ok && linkResult.success) {
385+
document.getElementById('plexLinkStatus').textContent = 'Successfully linked!';
386+
document.getElementById('plexLinkStatus').className = 'plex-status success';
387+
388+
setTimeout(() => {
389+
this.cancelPlexLink();
390+
this.loadUserData(); // Refresh user data to show linked account
391+
}, 2000);
392+
} else {
393+
document.getElementById('plexLinkStatus').textContent = linkResult.error || 'Failed to link account';
394+
document.getElementById('plexLinkStatus').className = 'plex-status error';
395+
}
396+
397+
clearInterval(this.plexPollingInterval);
398+
} catch (linkError) {
399+
document.getElementById('plexLinkStatus').textContent = 'Error linking account';
400+
document.getElementById('plexLinkStatus').className = 'plex-status error';
401+
clearInterval(this.plexPollingInterval);
402+
}
367403
} else if (result.error && result.error !== 'PIN not authorized yet') {
368404
document.getElementById('plexLinkStatus').textContent = result.error;
369405
document.getElementById('plexLinkStatus').className = 'plex-status error';

src/primary/routes/plex_auth_routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def create_pin():
3838
return jsonify({
3939
'success': True,
4040
'pin_id': pin_data['id'],
41+
'pin': pin_data['code'],
4142
'auth_url': pin_data['auth_url']
4243
})
4344
else:

0 commit comments

Comments
 (0)