WIP: Rearrange code

This commit is contained in:
aditya.chandel
2025-01-12 13:38:57 -07:00
parent 7df0052036
commit ef3556cb0c
77 changed files with 101 additions and 115 deletions
@@ -0,0 +1,43 @@
<div class="dialog-container">
<div class="checkbox-list">
<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>
<div class="bottom-section">
<p-divider></p-divider>
<div class="bottom-container">
<p-button outlined="true" label="Create Shelf" (onClick)="createShelfDialog()"></p-button>
<div>
<p-button class="spacing-right" severity="secondary" label="Cancel" (onClick)="closeDialog()"></p-button>
<p-button label="Save" (onClick)="updateBooksShelves()"></p-button>
</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" />
</div>
<div class="field">
<label>Shelf Icon</label>
<div *ngIf="!selectedIcon">
<p-button label="Select Icon" icon="pi pi-search" (onClick)="openIconPicker()"></p-button>
</div>
<div *ngIf="selectedIcon" class="mt-2 flex items-center">
<i [class]="selectedIcon" class="mr-2"></i>
<p-button label="Remove" icon="pi pi-times" text="true" outlined="true" severity="warn" (onClick)="clearSelectedIcon()"></p-button>
</div>
</div>
</div>
<ng-template pTemplate="footer">
<p-button label="Cancel" severity="secondary" (onClick)="closeShelfDialog()" class="p-button-text"></p-button>
<p-button label="Save" (onClick)="saveNewShelf()"></p-button>
</ng-template>
</p-dialog>
</div>
@@ -0,0 +1,49 @@
.dialog-container {
display: flex;
flex-direction: column;
justify-content: space-between;
height: 100%;
}
.bottom-section {
margin-top: auto;
}
.bottom-container {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 1rem;
}
p-divider {
margin: 1rem 0;
border-color: var(--text-color-secondary);
}
.spacing-right {
margin-right: 1rem;
}
p-dialog .p-dialog-content {
display: flex;
flex-direction: column;
justify-content: space-between;
gap: 1rem; /* Adds space between elements inside the dialog */
}
p-dialog .p-dialog-footer {
display: flex;
justify-content: flex-end;
}
.dialog-container .checkbox-list {
display: flex;
flex-direction: column;
gap: 1rem; /* Adds gap between each checkbox row */
padding: 1rem;
}
.checkbox-list .flex {
margin-bottom: 0.5rem; /* Optional: adds a smaller gap between each checkbox item */
}
@@ -0,0 +1,129 @@
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 '../../../utilities/icon-picker/icon-picker.component';
import { ShelfState } from '../../model/state/shelf-state.model';
@Component({
selector: 'app-shelf-assigner',
standalone: false,
templateUrl: './shelf-assigner.component.html',
styleUrls: ['./shelf-assigner.component.scss']
})
export class ShelfAssignerComponent implements OnInit {
shelfState$: Observable<ShelfState>;
book: Book;
selectedShelves: Shelf[] = [];
displayShelfDialog: boolean = false;
shelfName: string = '';
bookIds: Set<number> = new Set();
isMultiBooks: boolean = false;
selectedIcon: string | null = null;
@ViewChild(IconPickerComponent) iconPicker: IconPickerComponent | undefined;
constructor(
private shelfService: ShelfService,
private dynamicDialogConfig: DynamicDialogConfig,
private dynamicDialogRef: DynamicDialogRef,
private messageService: MessageService,
private bookService: BookService
) {
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.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: Partial<Shelf> = {
name: this.shelfName,
icon: this.selectedIcon ? this.selectedIcon.replace('pi pi-', '') : 'heart'
};
this.shelfService.createShelf(newShelf as Shelf).subscribe(
() => {
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' });
console.error('Error creating shelf:', error);
}
);
}
updateBooksShelves(): void {
const idsToAssign: Set<number | undefined> = new Set(this.selectedShelves.map(shelf => shelf.id));
const idsToUnassign: Set<number> = this.isMultiBooks ? new Set() : this.getIdsToUnAssign(this.book, idsToAssign);
const bookIds = this.isMultiBooks ? this.bookIds : new Set([this.book.id]);
this.updateBookShelves(bookIds, idsToAssign, idsToUnassign);
}
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.dynamicDialogRef.close();
},
error => {
this.messageService.add({ severity: 'error', summary: 'Error', detail: 'Failed to update book shelves' });
this.dynamicDialogRef.close();
}
);
}
private getIdsToUnAssign(book: Book, idsToAssign: Set<number | undefined>): Set<number> {
const idsToUnassign = new Set<number>();
book.shelves?.forEach(shelf => {
if (!idsToAssign.has(shelf.id)) {
idsToUnassign.add(shelf.id!);
}
});
return idsToUnassign;
}
createShelfDialog(): void {
this.displayShelfDialog = true;
}
closeShelfDialog(): void {
this.displayShelfDialog = false;
}
closeDialog(): void {
this.dynamicDialogRef.close();
}
openIconPicker() {
if (this.iconPicker) {
this.iconPicker.open();
}
}
clearSelectedIcon() {
this.selectedIcon = null;
}
onIconSelected(icon: string) {
this.selectedIcon = icon;
}
}