Instead of Entity[] use EntityState

This commit is contained in:
aditya.chandel
2024-12-28 23:06:54 -07:00
parent da153d6c04
commit a5e321319a
17 changed files with 398 additions and 251 deletions
@@ -1,12 +1,11 @@
<div class="dialog-container">
<div class="checkbox-list">
<div *ngFor="let shelf of shelves$ | async" class="flex items-center">
<p-checkbox [inputId]="shelf.name" name="group" [value]="shelf" [(ngModel)]="selectedShelves"/>
<div *ngFor="let shelf of (shelfState$ | async)?.shelves" class="flex items-center">
<p-checkbox [inputId]="shelf.name" name="group" [value]="shelf" [(ngModel)]="selectedShelves"></p-checkbox>
<label [for]="shelf.id" class="ml-2">{{ shelf.name }}</label>
</div>
</div>
<!-- Divider and Bottom Section -->
<div class="bottom-section">
<p-divider></p-divider>
<div class="bottom-container">
@@ -17,12 +16,13 @@
</div>
</div>
</div>
<app-icon-picker (iconSelected)="onIconSelected($event)"></app-icon-picker>
<p-dialog [(visible)]="displayShelfDialog" header="Create Shelf" [modal]="true" [draggable]="false" [closable]="true" (onHide)="closeShelfDialog()" [style]="{ width: '400px', height: 'auto' }">
<div class="p-fluid">
<div class="field">
<label for="shelfName">Shelf Name</label>
<input id="shelfName" type="text" pInputText [(ngModel)]="shelfName"/>
<input id="shelfName" type="text" pInputText [(ngModel)]="shelfName" />
</div>
<div class="field">
<label>Shelf Icon</label>
@@ -31,7 +31,7 @@
</div>
<div *ngIf="selectedIcon" class="mt-2 flex align-items-center">
<i [class]="selectedIcon" class="mr-2"></i>
<p-button label="Remove" icon="pi pi-times" [text]="true" [outlined]="true" severity="warning" (onClick)="clearSelectedIcon()"></p-button>
<p-button label="Remove" icon="pi pi-times" text="true" outlined="true" severity="warning" (onClick)="clearSelectedIcon()"></p-button>
</div>
</div>
</div>
@@ -1,13 +1,14 @@
import {Component, OnInit, ViewChild} from '@angular/core';
import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
import {Book} 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';
import {map, tap} from 'rxjs/operators';
import {Shelf} from '../../model/shelf.model';
import {IconPickerComponent} from '../icon-picker/icon-picker.component';
import { Component, OnInit, ViewChild } from '@angular/core';
import { DynamicDialogConfig, DynamicDialogRef } from 'primeng/dynamicdialog';
import { Book } 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';
import { map, tap } from 'rxjs/operators';
import { Shelf } from '../../model/shelf.model';
import { IconPickerComponent } from '../icon-picker/icon-picker.component';
import { ShelfState } from '../../model/state/shelf-state.model';
@Component({
selector: 'app-shelf-assigner',
@@ -16,7 +17,7 @@ import {IconPickerComponent} from '../icon-picker/icon-picker.component';
styleUrls: ['./shelf-assigner.component.scss']
})
export class ShelfAssignerComponent implements OnInit {
shelves$: Observable<Shelf[]>;
shelfState$: Observable<ShelfState>;
book: Book;
selectedShelves: Shelf[] = [];
displayShelfDialog: boolean = false;
@@ -24,6 +25,7 @@ export class ShelfAssignerComponent implements OnInit {
bookIds: Set<number> = new Set();
isMultiBooks: boolean = false;
selectedIcon: string | null = null;
@ViewChild(IconPickerComponent) iconPicker: IconPickerComponent | undefined;
constructor(
@@ -33,33 +35,37 @@ export class ShelfAssignerComponent implements OnInit {
private messageService: MessageService,
private bookService: BookService
) {
this.shelves$ = this.shelfService.shelves$;
this.shelfState$ = this.shelfService.shelfState$;
this.book = this.dynamicDialogConfig.data.book;
this.bookIds = this.dynamicDialogConfig.data.bookIds;
this.isMultiBooks = this.dynamicDialogConfig.data.isMultiBooks;
}
ngOnInit(): void {
if (!this.isMultiBooks) {
this.shelves$.pipe(
map(shelves => shelves.filter(shelf => this.book.shelves?.some(bShelf => bShelf.id === shelf.id))),
tap(selectedShelves => this.selectedShelves = selectedShelves)
if (!this.isMultiBooks && this.book.shelves) {
this.shelfState$.pipe(
map(state => state.shelves || []),
tap(shelves => {
this.selectedShelves = shelves.filter(shelf =>
this.book.shelves?.some(bShelf => bShelf.id === shelf.id)
);
})
).subscribe();
}
}
saveNewShelf(): void {
const newShelf = {
const newShelf: Partial<Shelf> = {
name: this.shelfName,
icon: this.selectedIcon ? this.selectedIcon.replace('pi pi-', '') : 'heart'
};
this.shelfService.createShelf(newShelf).subscribe(
this.shelfService.createShelf(newShelf as Shelf).subscribe(
() => {
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Shelf created: ' + this.shelfName});
this.messageService.add({ severity: 'info', summary: 'Success', detail: `Shelf created: ${this.shelfName}` });
this.displayShelfDialog = false;
},
error => {
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to create shelf'});
this.messageService.add({ severity: 'error', summary: 'Error', detail: 'Failed to create shelf' });
console.error('Error creating shelf:', error);
}
);
@@ -75,11 +81,11 @@ export class ShelfAssignerComponent implements OnInit {
private updateBookShelves(bookIds: Set<number>, idsToAssign: Set<number | undefined>, idsToUnassign: Set<number>): void {
this.bookService.updateBookShelves(bookIds, idsToAssign, idsToUnassign).subscribe(
() => {
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Book shelves updated'});
this.messageService.add({ severity: 'info', summary: 'Success', detail: 'Book shelves updated' });
this.dynamicDialogRef.close();
},
error => {
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to update book shelves'});
this.messageService.add({ severity: 'error', summary: 'Error', detail: 'Failed to update book shelves' });
this.dynamicDialogRef.close();
}
);
@@ -88,7 +94,7 @@ export class ShelfAssignerComponent implements OnInit {
private getIdsToUnAssign(book: Book, idsToAssign: Set<number | undefined>): Set<number> {
const idsToUnassign = new Set<number>();
book.shelves?.forEach(shelf => {
if (!idsToAssign.has(shelf.id!)) {
if (!idsToAssign.has(shelf.id)) {
idsToUnassign.add(shelf.id!);
}
});
@@ -120,5 +126,4 @@ export class ShelfAssignerComponent implements OnInit {
onIconSelected(icon: string) {
this.selectedIcon = icon;
}
}