Optimize Settings logic to avoid duplicate service calls
This commit is contained in:
committed by
Aditya Chandel
parent
9acd062634
commit
b9ad85857a
@@ -12,7 +12,7 @@
|
||||
<p *ngIf="entityType === EntityType.UNSHELVED" class="text-2xl whitespace-nowrap overflow-hidden text-ellipsis">
|
||||
Unshelved Books
|
||||
</p>
|
||||
<div *ngIf="userService.userData$ | async as userData">
|
||||
<div *ngIf="userService.userState$ | async as userData">
|
||||
<div *ngIf="entityType !== EntityType.ALL_BOOKS && entityType !== EntityType.UNSHELVED && (userData.permissions.admin || userData.permissions.canManipulateLibrary) ">
|
||||
<p-button icon="pi pi-ellipsis-v" [rounded]="true" [text]="true" severity="info" (click)="entitymenu.toggle($event)"></p-button>
|
||||
<p-menu #entitymenu [model]="entityOptions" [popup]="true" appendTo="body"></p-menu>
|
||||
@@ -149,7 +149,7 @@
|
||||
pTooltip="Remove from shelf"
|
||||
tooltipPosition="top">
|
||||
</p-button>
|
||||
<div *ngIf="userService.userData$ | async as userData">
|
||||
<div *ngIf="userService.userState$ | async as userData">
|
||||
<p-button
|
||||
*ngIf="userData.permissions.canEditMetadata"
|
||||
icon="pi pi-database"
|
||||
|
||||
@@ -52,7 +52,7 @@ export class BookCardComponent implements OnInit {
|
||||
|
||||
|
||||
ngOnInit(): void {
|
||||
this.userService.userData$
|
||||
this.userService.userState$
|
||||
.pipe(filter(userData => !!userData))
|
||||
.subscribe((userData) => {
|
||||
this.userPermissions = userData.permissions;
|
||||
|
||||
+1
-1
@@ -77,7 +77,7 @@ export class BookMetadataCenterComponent implements OnInit, OnDestroy {
|
||||
this.fetchBookRecommendationsIfNeeded(book.metadata!.bookId);
|
||||
});
|
||||
|
||||
this.userSubscription = this.userService.userData$.subscribe(userData => {
|
||||
this.userSubscription = this.userService.userState$.subscribe(userData => {
|
||||
const userPermissions = userData?.permissions;
|
||||
this.canEditMetadata = userPermissions?.canEditMetadata ?? false;
|
||||
this.admin = userPermissions?.admin ?? false;
|
||||
|
||||
+1
-1
@@ -124,7 +124,7 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div *ngIf="userService.userData$ | async as userData" class="pl-6 pt-4 flex flex-row w-full gap-4">
|
||||
<div *ngIf="userService.userState$ | async as userData" class="pl-6 pt-4 flex flex-row w-full gap-4">
|
||||
<p-button label="Read" icon="pi pi-book" severity="info" (onClick)="read(metadata.bookId)"></p-button>
|
||||
<p-button label="Assign Shelf" icon="pi pi-folder" severity="info" outlined (onClick)="assignShelf(metadata.bookId)"></p-button>
|
||||
<p-button *ngIf="userData.permissions.canDownload || userData.permissions.admin" label="Download" icon="pi pi-download" severity="info" outlined
|
||||
|
||||
+5
-8
@@ -74,14 +74,11 @@ export class AuthenticationSettingsComponent implements OnInit {
|
||||
take(1)
|
||||
).subscribe(settings => this.loadSettings(settings));
|
||||
|
||||
this.libraryService.getAllLibrariesFromAPI().subscribe({
|
||||
next: (libraries) => {
|
||||
this.allLibraries = libraries;
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to load libraries'});
|
||||
},
|
||||
});
|
||||
this.libraryService.libraryState$
|
||||
.pipe(
|
||||
filter(state => !!state?.loaded),
|
||||
take(1)
|
||||
).subscribe(state => this.allLibraries = state.libraries ?? []);
|
||||
}
|
||||
|
||||
loadSettings(settings: AppSettings): void {
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
<div class="dashboard" *ngIf="(bookState$ | async)?.loaded === true">
|
||||
<div>
|
||||
<div class="dashboard-no-library" *ngIf="isLibrariesEmpty$ | async; else dashboardScroller">
|
||||
<div *ngIf="(userService.userData$ | async)?.permissions as permissions">
|
||||
<div *ngIf="(userService.userState$ | async)?.permissions as permissions">
|
||||
<div *ngIf="permissions.admin || permissions.canManipulateLibrary">
|
||||
<h1 class="no-library-header">Looks like you haven't added a library yet. Let's add one!</h1>
|
||||
<p-button
|
||||
|
||||
@@ -46,7 +46,7 @@ export class AppMenuComponent implements OnInit {
|
||||
this.versionInfo = data;
|
||||
});
|
||||
|
||||
this.userService.userData$.pipe(
|
||||
this.userService.userState$.pipe(
|
||||
filter(settings => !!settings))
|
||||
.subscribe(user => {
|
||||
if (user?.userSettings.sidebarLibrarySorting) {
|
||||
|
||||
@@ -55,7 +55,7 @@ export class AppMenuitemComponent implements OnInit, OnDestroy {
|
||||
menuResetSubscription: Subscription;
|
||||
|
||||
constructor(public router: Router, private menuService: MenuService, private userService: UserService) {
|
||||
this.userService.userData$.subscribe(userData => {
|
||||
this.userService.userState$.subscribe(userData => {
|
||||
if (userData) {
|
||||
this.canManipulateLibrary = userData.permissions.canManipulateLibrary;
|
||||
this.admin = userData.permissions.admin;
|
||||
|
||||
@@ -21,13 +21,13 @@
|
||||
<app-book-searcher></app-book-searcher>
|
||||
<ul class="topbar-items pl-6">
|
||||
<div class="flex gap-6">
|
||||
<li *ngIf="userService.userData$ | async as userData">
|
||||
<li *ngIf="userService.userState$ | async as userData">
|
||||
<a class="topbar-item" *ngIf="userData.permissions.canManipulateLibrary || userData.permissions.admin">
|
||||
<i class="pi pi-plus-circle text-surface-700 dark:text-surface-100" (click)="openLibraryCreatorDialog()"
|
||||
pTooltip="Create New Library" tooltipPosition="top"></i>
|
||||
</a>
|
||||
</li>
|
||||
<li *ngIf="userService.userData$ | async as userData">
|
||||
<li *ngIf="userService.userState$ | async as userData">
|
||||
<a class="topbar-item" *ngIf="userData.permissions.canManipulateLibrary || userData.permissions.admin">
|
||||
<i class="pi pi-upload text-surface-700 dark:text-surface-100" (click)="openFileUploadDialog()"
|
||||
pTooltip="Upload Book" tooltipPosition="top"></i>
|
||||
|
||||
+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