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,6 +1,6 @@
<div class="dialog-container">
<div class="checkbox-list">
<div *ngFor="let shelf of shelves()" class="flex items-center">
<div *ngFor="let shelf of shelves$ | async" class="flex items-center">
<p-checkbox [inputId]="shelf.name" name="group" [value]="shelf" [(ngModel)]="selectedShelves" />
<label [for]="shelf.id" class="ml-2">{{ shelf.name }}</label>
</div>
@@ -1,8 +1,10 @@
import {Component, computed, OnInit} from '@angular/core';
import {LibraryAndBookService} from '../../service/library-and-book.service';
import {Component, OnInit} from '@angular/core';
import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
import {Book} from '../../model/book.model';
import {Book, Shelf} from '../../model/book.model';
import {MessageService} from 'primeng/api';
import {ShelfService} from '../../service/shelf.service';
import {Observable} from 'rxjs';
import {BookService} from '../../service/book.service';
@Component({
selector: 'app-shelf-assigner',
@@ -13,29 +15,31 @@ import {MessageService} from 'primeng/api';
})
export class ShelfAssignerComponent implements OnInit {
shelves;
shelves$: Observable<Shelf[]>;
book: Book;
selectedShelves: any[] = [];
displayShelfDialog: boolean = false;
shelfName: string = '';
constructor(
private libraryBookService: LibraryAndBookService, private dynamicDialogConfig: DynamicDialogConfig,
private dynamicDialogRef: DynamicDialogRef, private messageService: MessageService) {
this.shelves = this.libraryBookService.getShelves();
private shelfService: ShelfService, private dynamicDialogConfig: DynamicDialogConfig,
private dynamicDialogRef: DynamicDialogRef, private messageService: MessageService,
private bookService: BookService) {
this.shelves$ = this.shelfService.shelves$;
this.book = this.dynamicDialogConfig.data.book;
}
ngOnInit(): void {
if (this.book.shelves) {
const bookShelfIds = this.book.shelves.map(shelf => shelf.id);
this.selectedShelves = this.shelves().filter(shelf => bookShelfIds.includes(shelf.id));
}
this.shelves$.subscribe(shelves => {
if (this.book.shelves) {
const bookShelfIds = this.book.shelves.map(shelf => shelf.id);
this.selectedShelves = shelves.filter(shelf => bookShelfIds.includes(shelf.id));
}
});
}
saveNewShelf() {
this.libraryBookService.createShelf(this.shelfName).subscribe(
this.shelfService.createShelf(this.shelfName).subscribe(
() => {
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Shelf created: ' + this.shelfName});
this.displayShelfDialog = false;
@@ -49,7 +53,7 @@ export class ShelfAssignerComponent implements OnInit {
updateBooksShelves() {
const shelfIds = this.selectedShelves.map((shelf) => shelf.id);
this.libraryBookService.assignShelvesToBook(this.book, shelfIds).subscribe(
this.bookService.assignShelvesToBook(this.book, shelfIds).subscribe(
() => {
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Book\'s shelves updated'});
this.dynamicDialogRef.close();