Add configuration to launch Metadata Center as a dialog or full-page route
This commit is contained in:
+45
-47
@@ -1,44 +1,23 @@
|
||||
import {Component, inject, OnDestroy, OnInit} from '@angular/core';
|
||||
import {Component, inject, OnDestroy, OnInit, Optional} from '@angular/core';
|
||||
import {ActivatedRoute} from '@angular/router';
|
||||
import {UserService} from '../../../settings/user-management/user.service';
|
||||
import {Book, BookRecommendation} from '../../model/book.model';
|
||||
import {Observable, Subscription} from 'rxjs';
|
||||
import {
|
||||
distinctUntilChanged,
|
||||
filter,
|
||||
map,
|
||||
shareReplay,
|
||||
switchMap,
|
||||
take,
|
||||
tap
|
||||
} from 'rxjs/operators';
|
||||
import {BehaviorSubject, Observable, Subscription} from 'rxjs';
|
||||
import {distinctUntilChanged, filter, map, shareReplay, switchMap, take, tap} from 'rxjs/operators';
|
||||
import {BookService} from '../../service/book.service';
|
||||
import {AppSettingsService} from '../../../core/service/app-settings.service';
|
||||
import {
|
||||
Tab,
|
||||
TabList,
|
||||
TabPanel,
|
||||
TabPanels,
|
||||
Tabs
|
||||
} from 'primeng/tabs';
|
||||
import {Tab, TabList, TabPanel, TabPanels, Tabs} from 'primeng/tabs';
|
||||
import {MetadataViewerComponent} from './metadata-viewer/metadata-viewer.component';
|
||||
import {MetadataEditorComponent} from './metadata-editor/metadata-editor.component';
|
||||
import {MetadataSearcherComponent} from './metadata-searcher/metadata-searcher.component';
|
||||
import {DynamicDialogConfig} from 'primeng/dynamicdialog';
|
||||
import {BookMetadataHostService} from '../../../book-metadata-host-service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-book-metadata-center',
|
||||
standalone: true,
|
||||
templateUrl: './book-metadata-center.component.html',
|
||||
imports: [
|
||||
Tabs,
|
||||
TabList,
|
||||
Tab,
|
||||
TabPanels,
|
||||
TabPanel,
|
||||
MetadataViewerComponent,
|
||||
MetadataEditorComponent,
|
||||
MetadataSearcherComponent
|
||||
],
|
||||
imports: [Tabs, TabList, Tab, TabPanels, TabPanel, MetadataViewerComponent, MetadataEditorComponent, MetadataSearcherComponent],
|
||||
styleUrls: ['./book-metadata-center.component.scss']
|
||||
})
|
||||
export class BookMetadataCenterComponent implements OnInit, OnDestroy {
|
||||
@@ -46,6 +25,7 @@ export class BookMetadataCenterComponent implements OnInit, OnDestroy {
|
||||
private bookService = inject(BookService);
|
||||
private userService = inject(UserService);
|
||||
private appSettingsService = inject(AppSettingsService);
|
||||
private metadataHostService = inject(BookMetadataHostService);
|
||||
|
||||
book$!: Observable<Book>;
|
||||
recommendedBooks: BookRecommendation[] = [];
|
||||
@@ -57,35 +37,54 @@ export class BookMetadataCenterComponent implements OnInit, OnDestroy {
|
||||
private tabSubscription: Subscription = Subscription.EMPTY;
|
||||
private recommendationSubscription: Subscription = Subscription.EMPTY;
|
||||
private appSettings$ = this.appSettingsService.appSettings$;
|
||||
private currentBookId$ = new BehaviorSubject<number | null>(null);
|
||||
|
||||
constructor(@Optional() private config?: DynamicDialogConfig) {
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.bookService.loadBooks();
|
||||
const bookIdFromDialog: number | undefined = this.config?.data?.bookId;
|
||||
if (bookIdFromDialog != null) {
|
||||
this.currentBookId$.next(bookIdFromDialog);
|
||||
} else {
|
||||
this.route.paramMap
|
||||
.pipe(
|
||||
map(params => Number(params.get('bookId'))),
|
||||
filter(bookId => !isNaN(bookId))
|
||||
)
|
||||
.subscribe(bookId => this.currentBookId$.next(bookId));
|
||||
}
|
||||
|
||||
const bookId$ = this.route.paramMap.pipe(
|
||||
map(params => Number(params.get('bookId'))),
|
||||
filter(bookId => !isNaN(bookId)),
|
||||
this.metadataHostService.bookSwitches$
|
||||
.pipe(
|
||||
filter((bookId): bookId is number => !!bookId),
|
||||
distinctUntilChanged()
|
||||
)
|
||||
.subscribe(bookId => {
|
||||
this.currentBookId$.next(bookId);
|
||||
});
|
||||
|
||||
this.book$ = this.currentBookId$.pipe(
|
||||
filter((bookId): bookId is number => bookId != null),
|
||||
distinctUntilChanged(),
|
||||
shareReplay(1)
|
||||
);
|
||||
|
||||
this.book$ = bookId$.pipe(
|
||||
switchMap(bookId =>
|
||||
this.bookService.bookState$.pipe(
|
||||
map(state => state.books?.find(b => b.id === bookId)),
|
||||
filter((book): book is Book => !!book && !!book.metadata),
|
||||
distinctUntilChanged((a, b) => a.id === b.id && a.metadata === b.metadata),
|
||||
switchMap(book => this.bookService.getBookByIdFromAPI(book.id, true))
|
||||
)
|
||||
),
|
||||
shareReplay(1)
|
||||
);
|
||||
|
||||
this.recommendationSubscription = bookId$.pipe(
|
||||
this.recommendationSubscription = this.currentBookId$.pipe(
|
||||
filter((id): id is number => id != null),
|
||||
tap(bookId => this.fetchBookRecommendationsIfNeeded(bookId))
|
||||
).subscribe();
|
||||
|
||||
this.tabSubscription = this.route.queryParamMap.pipe(
|
||||
map(queryParams => queryParams.get('tab') ?? 'view'),
|
||||
map(params => params.get('tab') ?? 'view'),
|
||||
distinctUntilChanged()
|
||||
).subscribe(tab => {
|
||||
const validTabs = ['view', 'edit', 'match'];
|
||||
@@ -93,18 +92,11 @@ export class BookMetadataCenterComponent implements OnInit, OnDestroy {
|
||||
});
|
||||
|
||||
this.userSubscription = this.userService.userState$.subscribe(userData => {
|
||||
const userPermissions = userData?.permissions;
|
||||
this.canEditMetadata = userPermissions?.canEditMetadata ?? false;
|
||||
this.admin = userPermissions?.admin ?? false;
|
||||
this.canEditMetadata = userData?.permissions?.canEditMetadata ?? false;
|
||||
this.admin = userData?.permissions?.admin ?? false;
|
||||
});
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.userSubscription.unsubscribe();
|
||||
this.tabSubscription.unsubscribe();
|
||||
this.recommendationSubscription.unsubscribe();
|
||||
}
|
||||
|
||||
private fetchBookRecommendationsIfNeeded(bookId: number): void {
|
||||
this.appSettings$
|
||||
.pipe(
|
||||
@@ -121,4 +113,10 @@ export class BookMetadataCenterComponent implements OnInit, OnDestroy {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.userSubscription.unsubscribe();
|
||||
this.tabSubscription.unsubscribe();
|
||||
this.recommendationSubscription.unsubscribe();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user