Checkpoint/WIP: Completely refactor state management
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user