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">
|
<p *ngIf="entityType === EntityType.UNSHELVED" class="text-2xl whitespace-nowrap overflow-hidden text-ellipsis">
|
||||||
Unshelved Books
|
Unshelved Books
|
||||||
</p>
|
</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) ">
|
<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-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>
|
<p-menu #entitymenu [model]="entityOptions" [popup]="true" appendTo="body"></p-menu>
|
||||||
@@ -149,7 +149,7 @@
|
|||||||
pTooltip="Remove from shelf"
|
pTooltip="Remove from shelf"
|
||||||
tooltipPosition="top">
|
tooltipPosition="top">
|
||||||
</p-button>
|
</p-button>
|
||||||
<div *ngIf="userService.userData$ | async as userData">
|
<div *ngIf="userService.userState$ | async as userData">
|
||||||
<p-button
|
<p-button
|
||||||
*ngIf="userData.permissions.canEditMetadata"
|
*ngIf="userData.permissions.canEditMetadata"
|
||||||
icon="pi pi-database"
|
icon="pi pi-database"
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ export class BookCardComponent implements OnInit {
|
|||||||
|
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
this.userService.userData$
|
this.userService.userState$
|
||||||
.pipe(filter(userData => !!userData))
|
.pipe(filter(userData => !!userData))
|
||||||
.subscribe((userData) => {
|
.subscribe((userData) => {
|
||||||
this.userPermissions = userData.permissions;
|
this.userPermissions = userData.permissions;
|
||||||
|
|||||||
+1
-1
@@ -77,7 +77,7 @@ export class BookMetadataCenterComponent implements OnInit, OnDestroy {
|
|||||||
this.fetchBookRecommendationsIfNeeded(book.metadata!.bookId);
|
this.fetchBookRecommendationsIfNeeded(book.metadata!.bookId);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.userSubscription = this.userService.userData$.subscribe(userData => {
|
this.userSubscription = this.userService.userState$.subscribe(userData => {
|
||||||
const userPermissions = userData?.permissions;
|
const userPermissions = userData?.permissions;
|
||||||
this.canEditMetadata = userPermissions?.canEditMetadata ?? false;
|
this.canEditMetadata = userPermissions?.canEditMetadata ?? false;
|
||||||
this.admin = userPermissions?.admin ?? false;
|
this.admin = userPermissions?.admin ?? false;
|
||||||
|
|||||||
+1
-1
@@ -124,7 +124,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</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="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 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
|
<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)
|
take(1)
|
||||||
).subscribe(settings => this.loadSettings(settings));
|
).subscribe(settings => this.loadSettings(settings));
|
||||||
|
|
||||||
this.libraryService.getAllLibrariesFromAPI().subscribe({
|
this.libraryService.libraryState$
|
||||||
next: (libraries) => {
|
.pipe(
|
||||||
this.allLibraries = libraries;
|
filter(state => !!state?.loaded),
|
||||||
},
|
take(1)
|
||||||
error: () => {
|
).subscribe(state => this.allLibraries = state.libraries ?? []);
|
||||||
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to load libraries'});
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
loadSettings(settings: AppSettings): void {
|
loadSettings(settings: AppSettings): void {
|
||||||
|
|||||||
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
<div class="dashboard" *ngIf="(bookState$ | async)?.loaded === true">
|
<div class="dashboard" *ngIf="(bookState$ | async)?.loaded === true">
|
||||||
<div>
|
<div>
|
||||||
<div class="dashboard-no-library" *ngIf="isLibrariesEmpty$ | async; else dashboardScroller">
|
<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">
|
<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>
|
<h1 class="no-library-header">Looks like you haven't added a library yet. Let's add one!</h1>
|
||||||
<p-button
|
<p-button
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ export class AppMenuComponent implements OnInit {
|
|||||||
this.versionInfo = data;
|
this.versionInfo = data;
|
||||||
});
|
});
|
||||||
|
|
||||||
this.userService.userData$.pipe(
|
this.userService.userState$.pipe(
|
||||||
filter(settings => !!settings))
|
filter(settings => !!settings))
|
||||||
.subscribe(user => {
|
.subscribe(user => {
|
||||||
if (user?.userSettings.sidebarLibrarySorting) {
|
if (user?.userSettings.sidebarLibrarySorting) {
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ export class AppMenuitemComponent implements OnInit, OnDestroy {
|
|||||||
menuResetSubscription: Subscription;
|
menuResetSubscription: Subscription;
|
||||||
|
|
||||||
constructor(public router: Router, private menuService: MenuService, private userService: UserService) {
|
constructor(public router: Router, private menuService: MenuService, private userService: UserService) {
|
||||||
this.userService.userData$.subscribe(userData => {
|
this.userService.userState$.subscribe(userData => {
|
||||||
if (userData) {
|
if (userData) {
|
||||||
this.canManipulateLibrary = userData.permissions.canManipulateLibrary;
|
this.canManipulateLibrary = userData.permissions.canManipulateLibrary;
|
||||||
this.admin = userData.permissions.admin;
|
this.admin = userData.permissions.admin;
|
||||||
|
|||||||
@@ -21,13 +21,13 @@
|
|||||||
<app-book-searcher></app-book-searcher>
|
<app-book-searcher></app-book-searcher>
|
||||||
<ul class="topbar-items pl-6">
|
<ul class="topbar-items pl-6">
|
||||||
<div class="flex gap-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">
|
<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()"
|
<i class="pi pi-plus-circle text-surface-700 dark:text-surface-100" (click)="openLibraryCreatorDialog()"
|
||||||
pTooltip="Create New Library" tooltipPosition="top"></i>
|
pTooltip="Create New Library" tooltipPosition="top"></i>
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</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">
|
<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()"
|
<i class="pi pi-upload text-surface-700 dark:text-surface-100" (click)="openFileUploadDialog()"
|
||||||
pTooltip="Upload Book" tooltipPosition="top"></i>
|
pTooltip="Upload Book" tooltipPosition="top"></i>
|
||||||
|
|||||||
+1
-1
@@ -52,7 +52,7 @@ export class UserProfileDialogComponent implements OnInit, OnDestroy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
this.userSubscription = this.userService.userData$.subscribe(user => {
|
this.userSubscription = this.userService.userState$.subscribe(user => {
|
||||||
this.currentUser = user;
|
this.currentUser = user;
|
||||||
this.resetEditForm();
|
this.resetEditForm();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ export class ReaderPreferences implements OnInit, OnDestroy {
|
|||||||
private readonly userService = inject(UserService);
|
private readonly userService = inject(UserService);
|
||||||
private readonly messageService = inject(MessageService);
|
private readonly messageService = inject(MessageService);
|
||||||
|
|
||||||
userData$: Observable<User | null> = this.userService.userData$;
|
userData$: Observable<User | null> = this.userService.userState$;
|
||||||
private subscription?: Subscription;
|
private subscription?: Subscription;
|
||||||
private currentUser: User | null = null;
|
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">
|
<div class="rounded-xl overflow-hidden">
|
||||||
<p-tabs [(value)]="activeTab" scrollable>
|
<p-tabs [(value)]="activeTab" scrollable>
|
||||||
<p-tablist>
|
<p-tablist>
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import {Library} from '../../book/model/library.model';
|
|||||||
import {LibraryService} from '../../book/service/library.service';
|
import {LibraryService} from '../../book/service/library.service';
|
||||||
import {Dialog} from 'primeng/dialog';
|
import {Dialog} from 'primeng/dialog';
|
||||||
import {Password} from 'primeng/password';
|
import {Password} from 'primeng/password';
|
||||||
|
import {filter, take} from 'rxjs/operators';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-user-management',
|
selector: 'app-user-management',
|
||||||
@@ -52,35 +53,16 @@ export class UserManagementComponent implements OnInit {
|
|||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
this.loadUsers();
|
this.loadUsers();
|
||||||
this.loadMyself();
|
|
||||||
this.libraryService.getAllLibrariesFromAPI().subscribe({
|
this.userService.userState$
|
||||||
next: (libraries) => {
|
.pipe(filter(user => !!user), take(1))
|
||||||
this.allLibraries = libraries;
|
.subscribe(user => this.currentUser = user);
|
||||||
},
|
|
||||||
error: () => {
|
this.libraryService.libraryState$
|
||||||
this.messageService.add({
|
.pipe(filter(state => !!state?.loaded), take(1))
|
||||||
severity: 'error',
|
.subscribe(libraries => this.allLibraries = libraries.libraries ?? []);
|
||||||
summary: 'Error',
|
|
||||||
detail: 'Failed to load 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() {
|
loadUsers() {
|
||||||
this.userService.getUsers().subscribe({
|
this.userService.getUsers().subscribe({
|
||||||
|
|||||||
@@ -100,12 +100,12 @@ export class UserService {
|
|||||||
|
|
||||||
private rxStompService?: RxStompService;
|
private rxStompService?: RxStompService;
|
||||||
|
|
||||||
private userDataSubject = new BehaviorSubject<User | null>(null);
|
private userStateSubject = new BehaviorSubject<User | null>(null);
|
||||||
userData$ = this.userDataSubject.asObservable();
|
userState$ = this.userStateSubject.asObservable();
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.getMyself().subscribe(user => {
|
this.getMyself().subscribe(user => {
|
||||||
this.userDataSubject.next(user);
|
this.userStateSubject.next(user);
|
||||||
this.startWebSocket();
|
this.startWebSocket();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -165,11 +165,11 @@ export class UserService {
|
|||||||
headers: {'Content-Type': 'application/json'},
|
headers: {'Content-Type': 'application/json'},
|
||||||
responseType: 'text' as 'json'
|
responseType: 'text' as 'json'
|
||||||
}).subscribe(() => {
|
}).subscribe(() => {
|
||||||
const currentUser = this.userDataSubject.getValue();
|
const currentUser = this.userStateSubject.getValue();
|
||||||
if (currentUser) {
|
if (currentUser) {
|
||||||
const updatedSettings = {...currentUser.userSettings, [key]: value};
|
const updatedSettings = {...currentUser.userSettings, [key]: value};
|
||||||
const updatedUser = {...currentUser, settings: updatedSettings};
|
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 userService = inject(UserService);
|
||||||
private readonly messageService = inject(MessageService);
|
private readonly messageService = inject(MessageService);
|
||||||
|
|
||||||
userData$: Observable<User | null> = this.userService.userData$;
|
userData$: Observable<User | null> = this.userService.userState$;
|
||||||
private subscription?: Subscription;
|
private subscription?: Subscription;
|
||||||
private currentUser: User | null = null;
|
private currentUser: User | null = null;
|
||||||
|
|
||||||
|
|||||||
+9
-8
@@ -11,6 +11,7 @@ import {ShelfService} from '../../../book/service/shelf.service';
|
|||||||
import {combineLatest} from 'rxjs';
|
import {combineLatest} from 'rxjs';
|
||||||
import {FormsModule} from '@angular/forms';
|
import {FormsModule} from '@angular/forms';
|
||||||
import {ToastModule} from 'primeng/toast';
|
import {ToastModule} from 'primeng/toast';
|
||||||
|
import {filter, take} from 'rxjs/operators';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-view-preferences',
|
selector: 'app-view-preferences',
|
||||||
@@ -84,13 +85,13 @@ export class ViewPreferencesComponent implements OnInit {
|
|||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
combineLatest([
|
combineLatest([
|
||||||
this.userService.getMyself(),
|
this.userService.userState$.pipe(filter(user => !!user), take(1)),
|
||||||
this.libraryService.getAllLibrariesFromAPI(),
|
this.libraryService.libraryState$.pipe(filter(libraryState => !!libraryState?.libraries && libraryState.loaded), take(1)),
|
||||||
this.shelfService.shelfState$
|
this.shelfService.shelfState$.pipe(filter(shelfState => !!shelfState?.shelves && shelfState.loaded), take(1)),
|
||||||
]).subscribe(([user, libraries, shelfState]) => {
|
]).subscribe(([userState, librariesState, shelfState]) => {
|
||||||
this.user = user;
|
|
||||||
|
|
||||||
const prefs = user.userSettings?.entityViewPreferences;
|
this.user = userState;
|
||||||
|
const prefs = userState?.userSettings?.entityViewPreferences;
|
||||||
const global = prefs?.global;
|
const global = prefs?.global;
|
||||||
this.selectedSort = global?.sortKey ?? 'title';
|
this.selectedSort = global?.sortKey ?? 'title';
|
||||||
this.selectedSortDir = global?.sortDir ?? 'ASC';
|
this.selectedSortDir = global?.sortDir ?? 'ASC';
|
||||||
@@ -104,7 +105,7 @@ export class ViewPreferencesComponent implements OnInit {
|
|||||||
view: o.preferences.view ?? 'GRID'
|
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,
|
label: lib.name,
|
||||||
value: lib.id!
|
value: lib.id!
|
||||||
}));
|
}));
|
||||||
@@ -170,6 +171,6 @@ export class ViewPreferencesComponent implements OnInit {
|
|||||||
};
|
};
|
||||||
|
|
||||||
this.userService.updateUserSetting(this.user.id, 'entityViewPreferences', payload);
|
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