Refactorings

This commit is contained in:
aditya.chandel
2024-12-30 15:23:15 -07:00
parent ea272ea1de
commit b46ce591b1
2 changed files with 37 additions and 38 deletions
@@ -1,6 +1,6 @@
import {Component, OnInit} from '@angular/core'; import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router'; import {ActivatedRoute} from '@angular/router';
import {ConfirmationService, MenuItem, MessageService} from 'primeng/api'; import {MenuItem, MessageService} from 'primeng/api';
import {LibraryService} from '../../service/library.service'; import {LibraryService} from '../../service/library.service';
import {BookService} from '../../service/book.service'; import {BookService} from '../../service/book.service';
import {map, switchMap} from 'rxjs/operators'; import {map, switchMap} from 'rxjs/operators';
@@ -43,7 +43,7 @@ export class BookBrowserComponent implements OnInit {
selectedSort: SortOption | null = null; selectedSort: SortOption | null = null;
sortOptions: SortOption[] = []; sortOptions: SortOption[] = [];
isDrawerVisible: boolean = false; isDrawerVisible: boolean = false;
ref: DynamicDialogRef | undefined; dynamicDialogRef: DynamicDialogRef | undefined;
constructor( constructor(
private activatedRoute: ActivatedRoute, private activatedRoute: ActivatedRoute,
@@ -62,27 +62,23 @@ export class BookBrowserComponent implements OnInit {
const isAllBooksRoute = this.activatedRoute.snapshot.routeConfig?.path === 'all-books'; const isAllBooksRoute = this.activatedRoute.snapshot.routeConfig?.path === 'all-books';
if (isAllBooksRoute) { if (isAllBooksRoute) {
this.entityType$ = this.activatedRoute.url.pipe(
map(segments => {
return EntityType.ALL_BOOKS;
})
);
this.entityType = EntityType.ALL_BOOKS; this.entityType = EntityType.ALL_BOOKS;
this.entityType$ = of(EntityType.ALL_BOOKS);
this.bookState$ = this.fetchAllBooks(); this.bookState$ = this.fetchAllBooks();
} else { } else {
const routeParam$ = this.getRouteParams(); const routeParam$ = this.getEntityInfoFromRoute();
this.bookState$ = routeParam$.pipe( this.bookState$ = routeParam$.pipe(
switchMap(({libraryId, shelfId}) => this.fetchBooksByEntity(libraryId, shelfId)), switchMap(({entityId, entityType}) => this.fetchBooksByEntity(entityId, entityType)),
); );
this.entity$ = routeParam$.pipe( this.entity$ = routeParam$.pipe(
switchMap(({libraryId, shelfId}) => this.fetchEntity(libraryId, shelfId)) switchMap(({entityId, entityType}) => this.fetchEntity(entityId, entityType))
); );
this.entityType$ = routeParam$.pipe( this.entityType$ = routeParam$.pipe(
map(({libraryId, shelfId}) => libraryId ? EntityType.LIBRARY : shelfId ? EntityType.SHELF : EntityType.ALL_BOOKS) map(({ entityType }) => entityType)
); );
this.entity$.subscribe(entity => { this.entity$.subscribe(entity => {
@@ -98,40 +94,41 @@ export class BookBrowserComponent implements OnInit {
} }
private getRouteParams(): Observable<{ libraryId: number; shelfId: number }> { private getEntityInfoFromRoute(): Observable<{ entityId: number; entityType: EntityType }> {
return this.activatedRoute.paramMap.pipe( return this.activatedRoute.paramMap.pipe(
map(params => { map(params => {
const libraryId = Number(params.get('libraryId')); const libraryId = Number(params.get('libraryId') || NaN);
const shelfId = Number(params.get('shelfId')); const shelfId = Number(params.get('shelfId') || NaN);
return {libraryId, shelfId}; if (!isNaN(libraryId)) {
return { entityId: libraryId, entityType: EntityType.LIBRARY };
} else if (!isNaN(shelfId)) {
return { entityId: shelfId, entityType: EntityType.SHELF };
} else {
return { entityId: NaN, entityType: EntityType.ALL_BOOKS };
}
}) })
); );
} }
private fetchEntity(libraryId: number, shelfId: number): Observable<Library | Shelf | null> { private fetchEntity(entityId: number, entityType: EntityType): Observable<Library | Shelf | null> {
if (libraryId) { if (entityType == EntityType.LIBRARY) {
this.entityType = EntityType.LIBRARY;
this.libraryShelfMenuService.initializeLibraryMenuItems(this.entity); this.libraryShelfMenuService.initializeLibraryMenuItems(this.entity);
return this.fetchLibrary(libraryId); return this.fetchLibrary(entityId);
} else if (shelfId) { } else if (EntityType.SHELF) {
this.entityType = EntityType.SHELF;
this.libraryShelfMenuService.initializeLibraryMenuItems(this.entity); this.libraryShelfMenuService.initializeLibraryMenuItems(this.entity);
return this.fetchShelf(shelfId); return this.fetchShelf(entityId);
} }
return of(null); return of(null);
} }
private fetchBooksByEntity(libraryId: number, shelfId: number): Observable<BookState> { private fetchBooksByEntity(entityId: number, entityType: EntityType): Observable<BookState> {
if (libraryId) { if (entityType == EntityType.LIBRARY) {
return this.fetchBooksByLibrary(libraryId); return this.fetchBooksByLibrary(entityId);
} else if (shelfId) { } else if (entityType == EntityType.SHELF) {
return this.fetchBooksByShelf(shelfId); return this.fetchBooksByShelf(entityId);
} else {
return this.fetchAllBooks();
} }
return of({
books: null,
loaded: true,
error: 'Invalid entity type'
});
} }
private fetchBooksByLibrary(libraryId: number): Observable<BookState> { private fetchBooksByLibrary(libraryId: number): Observable<BookState> {
@@ -250,9 +247,9 @@ export class BookBrowserComponent implements OnInit {
if (this.entityType === EntityType.ALL_BOOKS) { if (this.entityType === EntityType.ALL_BOOKS) {
this.bookState$ = this.fetchAllBooks(); this.bookState$ = this.fetchAllBooks();
} else { } else {
const routeParam$ = this.getRouteParams(); const routeParam$ = this.getEntityInfoFromRoute();
this.bookState$ = routeParam$.pipe( this.bookState$ = routeParam$.pipe(
switchMap(({libraryId, shelfId}) => this.fetchBooksByEntity(libraryId, shelfId)) switchMap(({entityId, entityType}) => this.fetchBooksByEntity(entityId, entityType))
); );
} }
if (this.entityType === EntityType.LIBRARY) { if (this.entityType === EntityType.LIBRARY) {
@@ -276,7 +273,7 @@ export class BookBrowserComponent implements OnInit {
} }
openShelfAssigner() { openShelfAssigner() {
this.ref = this.dialogService.open(ShelfAssignerComponent, { this.dynamicDialogRef = this.dialogService.open(ShelfAssignerComponent, {
header: `Update Books Shelves`, header: `Update Books Shelves`,
modal: true, modal: true,
closable: true, closable: true,
@@ -293,7 +290,7 @@ export class BookBrowserComponent implements OnInit {
bookIds: this.selectedBooks bookIds: this.selectedBooks
}, },
}); });
this.ref.onClose.subscribe(() => { this.dynamicDialogRef.onClose.subscribe(() => {
this.selectedBooks.clear(); this.selectedBooks.clear();
}); });
} }
@@ -3,6 +3,8 @@ import { MenuItem, ConfirmationService, MessageService } from 'primeng/api';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import {LibraryService} from './library.service'; import {LibraryService} from './library.service';
import {ShelfService} from './shelf.service'; import {ShelfService} from './shelf.service';
import {Library} from '../model/library.model';
import {Shelf} from '../model/shelf.model';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
@@ -16,7 +18,7 @@ export class LibraryShelfMenuService {
private router: Router private router: Router
) {} ) {}
initializeLibraryMenuItems(entity: any): MenuItem[] { initializeLibraryMenuItems(entity: Library | Shelf | null): MenuItem[] {
return [ return [
{ {
label: 'Options', label: 'Options',