Implement refresh library

This commit is contained in:
aditya.chandel
2024-12-26 14:36:55 -07:00
parent f3bf5e9c0b
commit bc1aa39b34
18 changed files with 214 additions and 32 deletions
+7 -3
View File
@@ -1,8 +1,8 @@
import {Component, OnInit} from '@angular/core';
import {RxStompService} from './book/service/rx-stomp.service';
import {Message} from '@stomp/stompjs';
import {Book} from './book/model/book.model';
import {BookService} from './book/service/book.service';
import {Action, parseBookNotification} from './book/model/book-notification.model';
@Component({
selector: 'app-root',
@@ -17,8 +17,12 @@ export class AppComponent implements OnInit {
ngOnInit(): void {
this.rxStompService.watch('/topic/books').subscribe((message: Message) => {
const book: Book = JSON.parse(message.body);
this.bookService.handleNewlyCreatedBook(book);
const notification = parseBookNotification(message.body);
if (notification.action === Action.BOOK_ADDED) {
this.bookService.handleNewlyCreatedBook(notification.addedBook!);
} else if (notification.action === Action.BOOKS_REMOVED) {
this.bookService.handleRemovedBookIds(notification.removedBookIds!);
}
});
}
@@ -37,13 +37,13 @@
}
.virtual-scroller-item {
height: 243px;
width: 150px;
height: 210px;
width: 130px;
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
gap: 1.3rem;
padding: 1rem;
width: 100%;
@@ -225,7 +225,7 @@ export class BooksBrowserComponent implements OnInit {
},
command: () => {
this.confirmationService.confirm({
message: 'Sure you want to delete ' + this.entity?.name + "?",
message: 'Sure you want to delete library: ' + this.entity?.name + "?",
header: 'Confirmation',
icon: 'pi pi-exclamation-triangle',
acceptIcon: 'none',
@@ -249,6 +249,38 @@ export class BooksBrowserComponent implements OnInit {
}
});
}
},
{
icon: 'pi pi-refresh',
tooltipOptions: {
tooltipLabel: 'Refresh',
tooltipPosition: 'top'
},
command: () => {
this.confirmationService.confirm({
message: 'Sure you want to refresh library: ' + this.entity?.name + "?",
header: 'Confirmation',
icon: 'pi pi-exclamation-triangle',
acceptIcon: 'none',
rejectIcon: 'none',
rejectButtonStyleClass: 'p-button-text',
accept: () => {
this.libraryService.refreshLibrary(this.entity?.id!).subscribe({
complete: () => {
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Library refresh scheduled'});
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Failed',
detail: 'Failed to refresh library',
life: 3000
});
}
});
}
});
}
}
];
}
@@ -0,0 +1,21 @@
import { Book } from './book.model';
export interface BookNotification {
action: Action;
addedBook?: Book;
removedBookIds?: Set<number>;
}
export enum Action {
BOOK_ADDED = 'BOOK_ADDED',
BOOKS_REMOVED = 'BOOKS_REMOVED',
}
export function parseBookNotification(messageBody: string): BookNotification {
const raw = JSON.parse(messageBody);
return {
action: raw.action,
addedBook: raw.addedBook,
removedBookIds: raw.removedBookIds ? new Set<number>(raw.removedBookIds) : undefined,
};
}
@@ -158,4 +158,17 @@ export class BookService {
this.books.next(updatedBooks);
}
handleRemovedBookIds(removedBookIds: Set<number>) {
console.log(removedBookIds);
if (!(removedBookIds instanceof Set)) {
console.error("removedBookIds should be a Set");
return; // Or throw an error if preferred
}
const currentBooks = this.books.getValue();
let filteredBooks = currentBooks.filter(currentBook => !removedBookIds.has(currentBook.id));
this.books.next(filteredBooks);
}
}
@@ -52,6 +52,10 @@ export class LibraryService {
);
}
refreshLibrary(libraryId: number): Observable<void> {
return this.http.put<void>(`${this.url}/${libraryId}/refresh`, {});
}
updateSort(libraryId: number, sort: SortOption): Observable<Library> {
return this.http.put<Library>(`${this.url}/${libraryId}/sort`, sort).pipe(
map(updatedLibrary => {