Checkpoint/WIP: Completely refactor state management
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<div class="library-header">
|
||||
<div class="library-header-dial">
|
||||
<h3 class="library-name">{{ title() }}</h3>
|
||||
<h3 class="library-name">{{ (library$ | async)?.name }}</h3>
|
||||
<p-speedDial
|
||||
[model]="items!"
|
||||
direction="right"
|
||||
@@ -22,15 +22,15 @@
|
||||
</div>
|
||||
|
||||
<div class="content-container">
|
||||
<div class="card-container">
|
||||
<virtual-scroller *ngIf="books().length > 0" class="virtual-scroller" #scroll [items]="books()">
|
||||
<div class="card-container" *ngIf="books$ | async as books">
|
||||
<virtual-scroller *ngIf="books.length > 0" class="virtual-scroller" #scroll [items]="books">
|
||||
<div class="grid" #container>
|
||||
<div class="virtual-scroller-item" *ngFor="let book of scroll.viewPortItems">
|
||||
<app-book-card [book]="book"></app-book-card>
|
||||
</div>
|
||||
</div>
|
||||
</virtual-scroller>
|
||||
<div *ngIf="books().length === 0" class="empty-message">
|
||||
<div *ngIf="books.length === 0" class="empty-message">
|
||||
<p>No books available to display.</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,152 +1,115 @@
|
||||
import {Component, OnInit, computed, signal, Signal} from '@angular/core';
|
||||
import {Component, OnInit} from '@angular/core';
|
||||
import {ActivatedRoute, Router} from '@angular/router';
|
||||
import {LibraryAndBookService} from '../../service/library-and-book.service';
|
||||
import {ConfirmationService, MenuItem, MessageService} from 'primeng/api';
|
||||
import {Book} from "../../model/book.model";
|
||||
import {LibraryService} from '../../service/library.service';
|
||||
import {BookService} from '../../service/book.service';
|
||||
import {map, switchMap} from 'rxjs/operators';
|
||||
import {Observable, of} from 'rxjs';
|
||||
import {Book} from '../../model/book.model';
|
||||
import {Library} from '../../model/library.model';
|
||||
import {ShelfService} from '../../service/shelf.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-books-browser',
|
||||
standalone: false,
|
||||
templateUrl: './books-browser.component.html',
|
||||
styleUrls: ['./books-browser.component.scss']
|
||||
selector: 'app-books-browser',
|
||||
standalone: false,
|
||||
templateUrl: './books-browser.component.html',
|
||||
styleUrls: ['./books-browser.component.scss']
|
||||
})
|
||||
export class BooksBrowserComponent implements OnInit {
|
||||
private currentEntityId = signal<number | null>(null);
|
||||
private entityType = signal<'library' | 'shelf'>('library');
|
||||
|
||||
books!: Signal<Book[]>;
|
||||
title = computed(() => {
|
||||
const entityId = this.currentEntityId();
|
||||
const type = this.entityType();
|
||||
if (type === 'library') {
|
||||
const library = this.libraryBookService.getLibraries()().find(library => library.id === entityId);
|
||||
return library ? `Library: ${library.name}` : 'Library not found';
|
||||
books$: Observable<Book[]> | undefined;
|
||||
library$: Observable<Library | null> | undefined;
|
||||
items: MenuItem[] | undefined;
|
||||
|
||||
constructor(
|
||||
private activatedRoute: ActivatedRoute,
|
||||
private router: Router,
|
||||
private confirmationService: ConfirmationService,
|
||||
private messageService: MessageService,
|
||||
private libraryService: LibraryService,
|
||||
private bookService: BookService,
|
||||
private shelfService: ShelfService
|
||||
) {
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
const libraryId$ = this.activatedRoute.paramMap.pipe(
|
||||
map(params => Number(params.get('libraryId')))
|
||||
);
|
||||
|
||||
this.books$ = libraryId$.pipe(
|
||||
switchMap(libraryId => {
|
||||
if (!isNaN(libraryId)) {
|
||||
return this.bookService.books$.pipe(
|
||||
map(books => books.filter(book => book.libraryId === libraryId))
|
||||
);
|
||||
} else {
|
||||
return of([]);
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
if (type === 'shelf') {
|
||||
const shelf = this.libraryBookService.getShelves()().find(shelf => shelf.id === entityId);
|
||||
return shelf ? `Shelf: ${shelf.name}` : 'Shelf not found';
|
||||
this.library$ = this.activatedRoute.paramMap.pipe(
|
||||
switchMap(params => {
|
||||
const libraryId = Number(params.get('libraryId'));
|
||||
if (!isNaN(libraryId)) {
|
||||
return this.libraryService.libraries$.pipe(
|
||||
map(libraries => libraries.find(lib => lib.id === libraryId) || null)
|
||||
);
|
||||
}
|
||||
return '';
|
||||
});
|
||||
return of(null);
|
||||
})
|
||||
);
|
||||
|
||||
items: MenuItem[] | undefined;
|
||||
this.initializeLibraryMenuItems();
|
||||
}
|
||||
|
||||
|
||||
constructor(
|
||||
private activatedRoute: ActivatedRoute,
|
||||
private router: Router,
|
||||
private libraryBookService: LibraryAndBookService,
|
||||
private confirmationService: ConfirmationService,
|
||||
private messageService: MessageService
|
||||
) {
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.activatedRoute.paramMap.subscribe((params) => {
|
||||
const libraryId = Number(params.get('libraryId'));
|
||||
const shelfId = Number(params.get('shelfId'));
|
||||
if (libraryId) {
|
||||
this.entityType.set('library');
|
||||
this.currentEntityId.set(libraryId);
|
||||
this.books = this.libraryBookService.getLibraryBooks(libraryId);
|
||||
this.libraryBookService.getLibraries();
|
||||
} else if (shelfId) {
|
||||
this.entityType.set('shelf');
|
||||
this.currentEntityId.set(shelfId);
|
||||
this.books = this.libraryBookService.getShelfBooks(shelfId);
|
||||
this.libraryBookService.getShelves();
|
||||
}
|
||||
});
|
||||
|
||||
if (this.entityType() === 'library') {
|
||||
this.initializeLibraryMenuItems();
|
||||
} else if (this.entityType() === 'shelf') {
|
||||
this.initializeShelfMenuItems();
|
||||
}
|
||||
}
|
||||
|
||||
private initializeLibraryMenuItems(): void {
|
||||
this.items = [
|
||||
{
|
||||
icon: 'pi pi-trash',
|
||||
tooltipOptions: {
|
||||
tooltipLabel: 'Delete',
|
||||
tooltipPosition: 'top'
|
||||
},
|
||||
command: () => {
|
||||
this.confirmationService.confirm({
|
||||
message: 'Sure you want to delete ' + this.title() + "?",
|
||||
header: 'Confirmation',
|
||||
icon: 'pi pi-exclamation-triangle',
|
||||
acceptIcon: 'none',
|
||||
rejectIcon: 'none',
|
||||
rejectButtonStyleClass: 'p-button-text',
|
||||
accept: () => {
|
||||
this.libraryBookService.deleteLibrary(this.currentEntityId()).subscribe({
|
||||
complete: () => {
|
||||
this.router.navigate(['/']);
|
||||
this.messageService.add({
|
||||
severity: 'info',
|
||||
summary: 'Success',
|
||||
detail: 'Library was deleted'
|
||||
});
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Failed',
|
||||
detail: 'Failed to delete library',
|
||||
life: 3000
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
private initializeLibraryMenuItems(): void {
|
||||
this.items = [
|
||||
{
|
||||
icon: 'pi pi-trash',
|
||||
tooltipOptions: {
|
||||
tooltipLabel: 'Delete',
|
||||
tooltipPosition: 'top'
|
||||
},
|
||||
command: () => {
|
||||
this.library$?.subscribe(library => {
|
||||
if (library) {
|
||||
this.confirmationService.confirm({
|
||||
message: `Are you sure you want to delete ${library.name}?`,
|
||||
header: 'Confirmation',
|
||||
icon: 'pi pi-exclamation-triangle',
|
||||
acceptIcon: 'none',
|
||||
rejectIcon: 'none',
|
||||
rejectButtonStyleClass: 'p-button-text',
|
||||
accept: () => {
|
||||
if (library.id) {
|
||||
this.libraryService.deleteLibrary(library.id).subscribe({
|
||||
complete: () => {
|
||||
this.router.navigate(['/']);
|
||||
this.messageService.add({
|
||||
severity: 'info',
|
||||
summary: 'Success',
|
||||
detail: 'Library was deleted'
|
||||
});
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Failed',
|
||||
detail: 'Failed to delete library',
|
||||
life: 3000
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
];
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
private initializeShelfMenuItems(): void {
|
||||
this.items = [
|
||||
{
|
||||
icon: 'pi pi-trash',
|
||||
tooltipOptions: {
|
||||
tooltipLabel: 'Delete',
|
||||
tooltipPosition: 'top'
|
||||
},
|
||||
command: () => {
|
||||
this.confirmationService.confirm({
|
||||
message: 'Sure you want to delete ' + this.title() + "?",
|
||||
header: 'Confirmation',
|
||||
icon: 'pi pi-exclamation-triangle',
|
||||
acceptIcon: 'none',
|
||||
rejectIcon: 'none',
|
||||
rejectButtonStyleClass: 'p-button-text',
|
||||
accept: () => {
|
||||
this.libraryBookService.deleteShelf(this.currentEntityId()!).subscribe({
|
||||
complete: () => {
|
||||
this.router.navigate(['/']);
|
||||
this.messageService.add({
|
||||
severity: 'info',
|
||||
summary: 'Success',
|
||||
detail: 'Shelf was deleted'
|
||||
});
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Failed',
|
||||
detail: 'Failed to delete shelf',
|
||||
life: 3000
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user