Revamp state management

This commit is contained in:
aditya.chandel
2024-12-22 11:57:06 -07:00
parent e343e7e7f3
commit 4704d9f5f0
17 changed files with 624 additions and 419 deletions
@@ -1,7 +1,8 @@
import { Component, OnInit, computed, signal } 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 {Component, OnInit, computed, signal, Signal} 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";
@Component({
selector: 'app-books-browser',
@@ -10,27 +11,27 @@ import { ConfirmationService, MenuItem, MessageService } from 'primeng/api';
styleUrls: ['./books-browser.component.scss']
})
export class BooksBrowserComponent implements OnInit {
private currentEntityId = signal<number | null>(null);
private entityType = signal<'library' | 'shelf'>('library'); // Default to library
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';
}
if (type === 'shelf') {
const shelf = this.libraryBookService.getShelves()().find(shelf => shelf.id === entityId);
return shelf ? `Shelf: ${shelf.name}` : 'Shelf not found';
}
return '';
});
items: MenuItem[] | undefined;
protected currentEntityBooks = computed(() => {
if (this.currentEntityId() != null) {
return this.entityType() === 'library'
? this.libraryBookService.getLibraryBooks(this.currentEntityId()!)()
: this.libraryBookService.getShelfBooks(this.currentEntityId()!)();
}
return [];
});
protected currentEntityName = computed(() => {
if (this.entityType() === 'library') {
return 'Library: ' + (this.libraryBookService.libraries().find(library => library.id === this.currentEntityId())?.name || 'Library not found');
}
return 'Shelf: ' + (this.libraryBookService.shelves().find(shelf => shelf.id === this.currentEntityId())?.name || 'Shelf not found');
});
constructor(
private activatedRoute: ActivatedRoute,
@@ -38,70 +39,35 @@ export class BooksBrowserComponent implements OnInit {
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.libraryBookService.loadLibraryBooks(libraryId);
this.books = this.libraryBookService.getLibraryBooks(libraryId);
this.libraryBookService.getLibraries();
} else if (shelfId) {
this.entityType.set('shelf');
this.currentEntityId.set(shelfId);
this.libraryBookService.loadLibraryBooks(shelfId);
this.books = this.libraryBookService.getShelfBooks(shelfId);
this.libraryBookService.getShelves();
}
});
if (this.entityType() === 'library') {
this.initializeLibraryMenuItems();
} else if (this.entityType() === 'shelf') {
//this.initializeShelfMenuItems();
this.initializeShelfMenuItems();
}
}
private initializeLibraryMenuItems(): void {
this.items = [
{
icon: 'pi pi-pencil',
tooltipOptions: {
tooltipLabel: 'Rename',
tooltipPosition: 'top'
},
command: () => {
this.confirmationService.confirm({
message: 'Sure you want to delete: ' + this.currentEntityName() + "?",
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
});
}
});
}
});
}
},
{
icon: 'pi pi-trash',
tooltipOptions: {
@@ -110,7 +76,7 @@ export class BooksBrowserComponent implements OnInit {
},
command: () => {
this.confirmationService.confirm({
message: 'Sure you want to delete: ' + this.currentEntityName() + "?",
message: 'Sure you want to delete ' + this.title() + "?",
header: 'Confirmation',
icon: 'pi pi-exclamation-triangle',
acceptIcon: 'none',
@@ -142,45 +108,8 @@ export class BooksBrowserComponent implements OnInit {
];
}
/*private initializeShelfMenuItems(): void {
private initializeShelfMenuItems(): void {
this.items = [
{
icon: 'pi pi-pencil',
tooltipOptions: {
tooltipLabel: 'Rename',
tooltipPosition: 'top'
},
command: () => {
this.confirmationService.confirm({
message: 'Sure you want to delete: ' + this.currentEntityName() + "?",
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
});
}
});
}
});
}
},
{
icon: 'pi pi-trash',
tooltipOptions: {
@@ -189,14 +118,14 @@ export class BooksBrowserComponent implements OnInit {
},
command: () => {
this.confirmationService.confirm({
message: 'Sure you want to delete: ' + this.currentEntityName() + "?",
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({
this.libraryBookService.deleteShelf(this.currentEntityId()!).subscribe({
complete: () => {
this.router.navigate(['/']);
this.messageService.add({
@@ -219,5 +148,5 @@ export class BooksBrowserComponent implements OnInit {
}
}
];
}*/
}
}