Optimize Settings logic to avoid duplicate service calls
This commit is contained in:
committed by
Aditya Chandel
parent
9acd062634
commit
b9ad85857a
+1
-1
@@ -52,7 +52,7 @@ export class UserProfileDialogComponent implements OnInit, OnDestroy {
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.userSubscription = this.userService.userData$.subscribe(user => {
|
||||
this.userSubscription = this.userService.userState$.subscribe(user => {
|
||||
this.currentUser = user;
|
||||
this.resetEditForm();
|
||||
});
|
||||
|
||||
@@ -84,7 +84,7 @@ export class ReaderPreferences implements OnInit, OnDestroy {
|
||||
private readonly userService = inject(UserService);
|
||||
private readonly messageService = inject(MessageService);
|
||||
|
||||
userData$: Observable<User | null> = this.userService.userData$;
|
||||
userData$: Observable<User | null> = this.userService.userState$;
|
||||
private subscription?: Subscription;
|
||||
private currentUser: User | null = null;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<div *ngIf="userService.userData$ | async as userData">
|
||||
<div *ngIf="userService.userState$ | async as userData">
|
||||
<div class="rounded-xl overflow-hidden">
|
||||
<p-tabs [(value)]="activeTab" scrollable>
|
||||
<p-tablist>
|
||||
|
||||
@@ -13,6 +13,7 @@ import {Library} from '../../book/model/library.model';
|
||||
import {LibraryService} from '../../book/service/library.service';
|
||||
import {Dialog} from 'primeng/dialog';
|
||||
import {Password} from 'primeng/password';
|
||||
import {filter, take} from 'rxjs/operators';
|
||||
|
||||
@Component({
|
||||
selector: 'app-user-management',
|
||||
@@ -52,35 +53,16 @@ export class UserManagementComponent implements OnInit {
|
||||
|
||||
ngOnInit() {
|
||||
this.loadUsers();
|
||||
this.loadMyself();
|
||||
this.libraryService.getAllLibrariesFromAPI().subscribe({
|
||||
next: (libraries) => {
|
||||
this.allLibraries = libraries;
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Error',
|
||||
detail: 'Failed to load libraries',
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
this.userService.userState$
|
||||
.pipe(filter(user => !!user), take(1))
|
||||
.subscribe(user => this.currentUser = user);
|
||||
|
||||
this.libraryService.libraryState$
|
||||
.pipe(filter(state => !!state?.loaded), take(1))
|
||||
.subscribe(libraries => this.allLibraries = libraries.libraries ?? []);
|
||||
}
|
||||
|
||||
loadMyself() {
|
||||
this.userService.getMyself().subscribe({
|
||||
next: (user) => {
|
||||
this.currentUser = user;
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Error',
|
||||
detail: 'Failed to fetch current user',
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
loadUsers() {
|
||||
this.userService.getUsers().subscribe({
|
||||
|
||||
@@ -100,12 +100,12 @@ export class UserService {
|
||||
|
||||
private rxStompService?: RxStompService;
|
||||
|
||||
private userDataSubject = new BehaviorSubject<User | null>(null);
|
||||
userData$ = this.userDataSubject.asObservable();
|
||||
private userStateSubject = new BehaviorSubject<User | null>(null);
|
||||
userState$ = this.userStateSubject.asObservable();
|
||||
|
||||
constructor() {
|
||||
this.getMyself().subscribe(user => {
|
||||
this.userDataSubject.next(user);
|
||||
this.userStateSubject.next(user);
|
||||
this.startWebSocket();
|
||||
});
|
||||
}
|
||||
@@ -165,11 +165,11 @@ export class UserService {
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
responseType: 'text' as 'json'
|
||||
}).subscribe(() => {
|
||||
const currentUser = this.userDataSubject.getValue();
|
||||
const currentUser = this.userStateSubject.getValue();
|
||||
if (currentUser) {
|
||||
const updatedSettings = {...currentUser.userSettings, [key]: value};
|
||||
const updatedUser = {...currentUser, settings: updatedSettings};
|
||||
this.userDataSubject.next(updatedUser);
|
||||
this.userStateSubject.next(updatedUser);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+1
-1
@@ -31,7 +31,7 @@ export class SidebarSortingPreferencesComponent implements OnInit, OnDestroy {
|
||||
private readonly userService = inject(UserService);
|
||||
private readonly messageService = inject(MessageService);
|
||||
|
||||
userData$: Observable<User | null> = this.userService.userData$;
|
||||
userData$: Observable<User | null> = this.userService.userState$;
|
||||
private subscription?: Subscription;
|
||||
private currentUser: User | null = null;
|
||||
|
||||
|
||||
+9
-8
@@ -11,6 +11,7 @@ import {ShelfService} from '../../../book/service/shelf.service';
|
||||
import {combineLatest} from 'rxjs';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {ToastModule} from 'primeng/toast';
|
||||
import {filter, take} from 'rxjs/operators';
|
||||
|
||||
@Component({
|
||||
selector: 'app-view-preferences',
|
||||
@@ -84,13 +85,13 @@ export class ViewPreferencesComponent implements OnInit {
|
||||
|
||||
ngOnInit(): void {
|
||||
combineLatest([
|
||||
this.userService.getMyself(),
|
||||
this.libraryService.getAllLibrariesFromAPI(),
|
||||
this.shelfService.shelfState$
|
||||
]).subscribe(([user, libraries, shelfState]) => {
|
||||
this.user = user;
|
||||
this.userService.userState$.pipe(filter(user => !!user), take(1)),
|
||||
this.libraryService.libraryState$.pipe(filter(libraryState => !!libraryState?.libraries && libraryState.loaded), take(1)),
|
||||
this.shelfService.shelfState$.pipe(filter(shelfState => !!shelfState?.shelves && shelfState.loaded), take(1)),
|
||||
]).subscribe(([userState, librariesState, shelfState]) => {
|
||||
|
||||
const prefs = user.userSettings?.entityViewPreferences;
|
||||
this.user = userState;
|
||||
const prefs = userState?.userSettings?.entityViewPreferences;
|
||||
const global = prefs?.global;
|
||||
this.selectedSort = global?.sortKey ?? 'title';
|
||||
this.selectedSortDir = global?.sortDir ?? 'ASC';
|
||||
@@ -104,7 +105,7 @@ export class ViewPreferencesComponent implements OnInit {
|
||||
view: o.preferences.view ?? 'GRID'
|
||||
}));
|
||||
|
||||
this.libraryOptions = (libraries ?? []).filter(lib => lib.id !== undefined).map(lib => ({
|
||||
this.libraryOptions = (librariesState.libraries ?? []).filter(lib => lib.id !== undefined).map(lib => ({
|
||||
label: lib.name,
|
||||
value: lib.id!
|
||||
}));
|
||||
@@ -170,6 +171,6 @@ export class ViewPreferencesComponent implements OnInit {
|
||||
};
|
||||
|
||||
this.userService.updateUserSetting(this.user.id, 'entityViewPreferences', payload);
|
||||
this.messageService.add({ severity: 'success', summary: 'Preferences Saved', detail: 'Your sorting and view preferences were saved successfully.' });
|
||||
this.messageService.add({severity: 'success', summary: 'Preferences Saved', detail: 'Your sorting and view preferences were saved successfully.'});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user