Checkpoint/WIP: Completely refactor state management

This commit is contained in:
aditya.chandel
2024-12-24 12:54:29 -07:00
parent 043c681132
commit 84724ec014
30 changed files with 535 additions and 923 deletions
@@ -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
});
}
});
}
});
}
}
];
}
}