Added shelf functionality

This commit is contained in:
aditya.chandel
2024-12-21 16:39:30 -07:00
parent 1abe138dd1
commit e68117c1bc
12 changed files with 216 additions and 30 deletions
+4
View File
@@ -28,6 +28,8 @@ import {LazyLoadImageModule} from 'ng-lazyload-image';
import {ConfirmDialogModule} from 'primeng/confirmdialog';
import { ShelfAssignerComponent } from './shelf-assigner/shelf-assigner.component';
import {CheckboxModule} from 'primeng/checkbox';
import {DividerModule} from 'primeng/divider';
import {DialogModule} from 'primeng/dialog';
@NgModule({
declarations: [
@@ -57,6 +59,8 @@ import {CheckboxModule} from 'primeng/checkbox';
LazyLoadImageModule,
ConfirmDialogModule,
CheckboxModule,
DividerModule,
DialogModule,
],
providers: [
DialogService,
@@ -26,10 +26,8 @@ export class BookCardComponent implements OnInit {
constructor(
private libraryBookService: LibraryAndBookService, private router: Router,
private dialogService: DialogService) {
}
coverImageSrc(book: Book): string {
return this.libraryBookService.getBookCoverUrl(book.id);
}
@@ -61,10 +59,10 @@ export class BookCardComponent implements OnInit {
private openShelfDialog(book: Book) {
this.dialogService.open(ShelfAssignerComponent, {
header: 'Edit Shelves for Book: ' + book.metadata.title,
header: 'Update Shelves: ' + book.metadata.title,
modal: true,
width: '50%',
height: '75%',
width: '30%',
height: '70%',
contentStyle: {overflow: 'auto'},
baseZIndex: 10,
data: {
@@ -18,7 +18,7 @@ export class LibraryCreatorComponent {
ref: DynamicDialogRef | undefined;
constructor(
public dialogService: DialogService,
private dialogService: DialogService,
private dynamicDialogRef: DynamicDialogRef,
private libraryAndBookService: LibraryAndBookService,
private router: Router,
+1 -1
View File
@@ -31,7 +31,7 @@ export interface Category {
}
export interface Shelf {
id: number;
id?: number;
name: string;
}
@@ -41,17 +41,54 @@ export class LibraryAndBookService {
this.http.get<Shelf[]>(this.shelfUrl).pipe(
map(response => response),
catchError(error => {
console.error('Error loading libraries:', error);
console.error('Error loading shelves:', error);
return of([]);
})
).subscribe(
(shelves) => {
this.#shelves.set(shelves);
console.log("Library Initialized")
console.log("Shelves Initialized")
}
);
}
createShelf(name: string): Observable<Shelf> {
const newShelf: Shelf = {name};
return this.http.post<Shelf>(this.shelfUrl, newShelf).pipe(
tap((createdShelf) => {
this.#shelves.set([...this.#shelves(), createdShelf]);
console.log("New Shelf Created", createdShelf);
}),
catchError((error) => {
console.error('Error creating shelf:', error);
throw error;
})
);
}
assignShelvesToBook(book: Book, shelfIds: number[]): Observable<Book> {
const requestPayload = {
bookId: book.id,
shelfIds: shelfIds
};
return this.http.post<Book>(`${this.shelfUrl}/assign-shelves`, requestPayload).pipe(
tap((updatedBook) => {
const bookLibraryId = book.libraryId;
const updatedMap = new Map(this.libraryBooksMap());
const libraryBooks = updatedMap.get(bookLibraryId) || [];
const updatedLibraryBooks = libraryBooks.map(b =>
b.id === book.id ? { ...b, shelves: updatedBook.shelves } : b
);
updatedMap.set(bookLibraryId, updatedLibraryBooks);
this.libraryBooksMap.set(updatedMap);
}),
catchError((error) => {
console.error('Error assigning shelves to book:', error);
throw error;
})
);
}
/*---------- Library Methods go below ----------*/
@@ -266,4 +303,6 @@ export class LibraryAndBookService {
const requestBody = {googleBookId: googleBookId};
return this.http.put<void>(`${this.bookUrl}/${bookId}/set-metadata`, requestBody);
}
}
@@ -1,4 +1,33 @@
<div *ngFor="let shelf of shelves()" 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 class="dialog-container">
<div class="checkbox-list">
<div *ngFor="let shelf of shelves()" 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>
</div>
<!-- Divider and Bottom Section -->
<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>
<p-dialog [(visible)]="displayShelfDialog" header="Create Shelf" [modal]="true" [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>
<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 */
}
@@ -1,7 +1,8 @@
import {Component, computed, OnInit} from '@angular/core';
import {LibraryAndBookService} from '../book/service/library-and-book.service';
import {DynamicDialogConfig} from 'primeng/dynamicdialog';
import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
import {Book} from '../book/model/book.model';
import {MessageService} from 'primeng/api';
@Component({
selector: 'app-shelf-assigner',
@@ -18,8 +19,12 @@ export class ShelfAssignerComponent implements OnInit {
book: Book;
selectedShelves: any[] = [];
displayShelfDialog: boolean = false;
shelfName: string = '';
constructor(private libraryBookService: LibraryAndBookService, private dynamicDialogConfig: DynamicDialogConfig) {
constructor(
private libraryBookService: LibraryAndBookService, private dynamicDialogConfig: DynamicDialogConfig,
private dynamicDialogRef: DynamicDialogRef, private messageService: MessageService) {
this.book = this.dynamicDialogConfig.data.book;
}
@@ -30,4 +35,42 @@ export class ShelfAssignerComponent implements OnInit {
}
}
saveNewShelf() {
this.libraryBookService.createShelf(this.shelfName).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() {
const shelfIds = this.selectedShelves.map((shelf) => shelf.id);
this.libraryBookService.assignShelvesToBook(this.book, shelfIds).subscribe(
() => {
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Book\'s shelves updated'});
this.dynamicDialogRef.close();
},
(error) => {
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to update book\'s shelves'});
this.dynamicDialogRef.close();
}
);
}
createShelfDialog(): void {
this.displayShelfDialog = true;
}
closeShelfDialog(): void {
this.displayShelfDialog = false;
}
closeDialog() {
this.dynamicDialogRef.close();
}
}