feat: add support for personal book ratings
- Introduced 'personalRating' field in book metadata - Added UI for 10-star rating input - Persist personal ratings via updateBookMetadata API - Updated filters and sorting to include personal rating range
This commit is contained in:
committed by
Aditya Chandel
parent
ce96fcff6d
commit
cd4e14c8ad
+31
-13
@@ -3,10 +3,24 @@ import {ActivatedRoute} from '@angular/router';
|
||||
import {UserService} from '../../../settings/user-management/user.service';
|
||||
import {Book, BookRecommendation} from '../../model/book.model';
|
||||
import {Observable, Subscription} from 'rxjs';
|
||||
import {distinctUntilChanged, filter, map, shareReplay, switchMap, take, tap} from 'rxjs/operators';
|
||||
import {
|
||||
distinctUntilChanged,
|
||||
filter,
|
||||
map,
|
||||
shareReplay,
|
||||
switchMap,
|
||||
take,
|
||||
tap
|
||||
} from 'rxjs/operators';
|
||||
import {BookService} from '../../service/book.service';
|
||||
import {AppSettingsService} from '../../../core/service/app-settings.service';
|
||||
import {Tab, TabList, TabPanel, TabPanels, Tabs} from 'primeng/tabs';
|
||||
import {
|
||||
Tab,
|
||||
TabList,
|
||||
TabPanel,
|
||||
TabPanels,
|
||||
Tabs
|
||||
} from 'primeng/tabs';
|
||||
import {MetadataViewerComponent} from './metadata-viewer/metadata-viewer.component';
|
||||
import {MetadataEditorComponent} from './metadata-editor/metadata-editor.component';
|
||||
import {MetadataSearcherComponent} from './metadata-searcher/metadata-searcher.component';
|
||||
@@ -40,34 +54,36 @@ export class BookMetadataCenterComponent implements OnInit, OnDestroy {
|
||||
admin: boolean = false;
|
||||
|
||||
private userSubscription: Subscription = Subscription.EMPTY;
|
||||
private routeSubscription: Subscription = Subscription.EMPTY;
|
||||
private tabSubscription: Subscription = Subscription.EMPTY;
|
||||
private recommendationSubscription: Subscription = Subscription.EMPTY;
|
||||
private appSettings$ = this.appSettingsService.appSettings$;
|
||||
|
||||
ngOnInit(): void {
|
||||
this.bookService.loadBooks();
|
||||
|
||||
this.book$ = this.route.paramMap.pipe(
|
||||
const bookId$ = this.route.paramMap.pipe(
|
||||
map(params => Number(params.get('bookId'))),
|
||||
filter(bookId => !isNaN(bookId)),
|
||||
distinctUntilChanged(),
|
||||
shareReplay(1)
|
||||
);
|
||||
|
||||
this.book$ = bookId$.pipe(
|
||||
switchMap(bookId =>
|
||||
this.bookService.bookState$.pipe(
|
||||
map(state => state.books?.find(b => b.id === bookId)),
|
||||
filter((book): book is Book => !!book && !!book.metadata),
|
||||
distinctUntilChanged((a, b) => a.id === b.id && a.metadata === b.metadata),
|
||||
switchMap(book =>
|
||||
this.bookService.getBookByIdFromAPI(book.id, true).pipe(
|
||||
tap(bookWithDescription => {
|
||||
this.fetchBookRecommendationsIfNeeded(bookWithDescription.metadata!.bookId);
|
||||
})
|
||||
)
|
||||
)
|
||||
switchMap(book => this.bookService.getBookByIdFromAPI(book.id, true))
|
||||
)
|
||||
),
|
||||
shareReplay(1)
|
||||
);
|
||||
|
||||
this.recommendationSubscription = bookId$.pipe(
|
||||
tap(bookId => this.fetchBookRecommendationsIfNeeded(bookId))
|
||||
).subscribe();
|
||||
|
||||
this.tabSubscription = this.route.queryParamMap.pipe(
|
||||
map(queryParams => queryParams.get('tab') ?? 'view'),
|
||||
distinctUntilChanged()
|
||||
@@ -85,8 +101,8 @@ export class BookMetadataCenterComponent implements OnInit, OnDestroy {
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.userSubscription.unsubscribe();
|
||||
this.routeSubscription.unsubscribe();
|
||||
this.tabSubscription.unsubscribe();
|
||||
this.recommendationSubscription.unsubscribe();
|
||||
}
|
||||
|
||||
private fetchBookRecommendationsIfNeeded(bookId: number): void {
|
||||
@@ -98,7 +114,9 @@ export class BookMetadataCenterComponent implements OnInit, OnDestroy {
|
||||
.subscribe(settings => {
|
||||
if (settings!.similarBookRecommendation ?? false) {
|
||||
this.bookService.getBookRecommendations(bookId).subscribe(recommendations => {
|
||||
this.recommendedBooks = recommendations;
|
||||
this.recommendedBooks = recommendations.sort(
|
||||
(a, b) => (b.similarityScore ?? 0) - (a.similarityScore ?? 0)
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
+17
-14
@@ -63,10 +63,24 @@
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-2 pt-2">
|
||||
<div class="flex items-center gap-2">
|
||||
<i class="pi pi-thumbs-up-fill"
|
||||
pTooltip="Personal Rating"
|
||||
tooltipPosition="top">
|
||||
</i>
|
||||
<p-rating
|
||||
[ngModel]="book?.metadata!.personalRating"
|
||||
(onRate)="onPersonalRatingChange(book, $event)"
|
||||
stars="10"
|
||||
[style.--p-rating-icon-active-color]="getStarColorScaled(book?.metadata!.personalRating, 10)">
|
||||
</p-rating>
|
||||
</div>
|
||||
@if (book?.metadata!.amazonRating || book?.metadata!.goodreadsRating || book?.metadata!.hardcoverRating) {
|
||||
@if (book?.metadata!.amazonRating) {
|
||||
@if (book?.metadata!.asin) {
|
||||
<span>|</span>
|
||||
<a class="flex items-center gap-1" [href]="'https://www.amazon.com/dp/' + book?.metadata!.asin" target="_blank" rel="noopener noreferrer">
|
||||
<img src="https://www.amazon.com/favicon.ico" alt="Amazon"
|
||||
class="w-5 h-5 object-contain transform transition duration-200 hover:scale-125 hover:drop-shadow-lg"/>
|
||||
@@ -79,7 +93,7 @@
|
||||
</span>
|
||||
}
|
||||
<p-rating [ngModel]="book?.metadata!.amazonRating" readonly stars="5"
|
||||
[style.--p-rating-icon-active-color]="getStarColor(book?.metadata!.amazonRating)">
|
||||
[style.--p-rating-icon-active-color]="getStarColorScaled(book?.metadata!.amazonRating)">
|
||||
</p-rating>
|
||||
<p class="font-semibold">({{ book?.metadata!.amazonReviewCount | number:'1.0-0' }})</p>
|
||||
@if (book?.metadata!.goodreadsRating || book?.metadata!.hardcoverRating) {
|
||||
@@ -100,7 +114,7 @@
|
||||
</span>
|
||||
}
|
||||
<p-rating [ngModel]="book?.metadata!.goodreadsRating" readonly stars="5"
|
||||
[style.--p-rating-icon-active-color]="getStarColor(book?.metadata!.goodreadsRating)">
|
||||
[style.--p-rating-icon-active-color]="getStarColorScaled(book?.metadata!.goodreadsRating)">
|
||||
</p-rating>
|
||||
<p class="font-semibold">({{ book?.metadata!.goodreadsReviewCount | number:'1.0-0' }})</p>
|
||||
@if (book?.metadata!.hardcoverRating) {
|
||||
@@ -121,7 +135,7 @@
|
||||
</span>
|
||||
}
|
||||
<p-rating [ngModel]="book?.metadata!.hardcoverRating" readonly stars="5"
|
||||
[style.--p-rating-icon-active-color]="getStarColor(book?.metadata!.hardcoverRating)">
|
||||
[style.--p-rating-icon-active-color]="getStarColorScaled(book?.metadata!.hardcoverRating)">
|
||||
</p-rating>
|
||||
<p class="font-semibold">({{ book?.metadata!.hardcoverReviewCount | number:'1.0-0' }})</p>
|
||||
@if ((book?.metadata!.amazonRating || book?.metadata!.goodreadsRating || book?.metadata!.hardcoverRating) && book?.metadata!.googleId) {
|
||||
@@ -134,17 +148,6 @@
|
||||
class="w-5 h-5 object-contain transform transition duration-200 hover:scale-125 hover:drop-shadow-lg"/>
|
||||
</a>
|
||||
}
|
||||
|
||||
} @else {
|
||||
<p-rating
|
||||
[(ngModel)]="book?.metadata!.rating"
|
||||
readonly
|
||||
stars="5"
|
||||
[style.--p-rating-icon-active-color]="getStarColor(book?.metadata!.rating)">
|
||||
</p-rating>
|
||||
<p class="font-semibold">
|
||||
{{ book.metadata?.reviewCount ? '(' + (book.metadata?.reviewCount | number:'1.0-0') + ' Ratings)' : 'No Ratings' }}
|
||||
</p>
|
||||
}
|
||||
</div>
|
||||
<div class="flex gap-2 pt-4 overflow-x-auto scrollbar-hide">
|
||||
|
||||
+23
-6
@@ -3,7 +3,7 @@ import {Button, ButtonDirective} from 'primeng/button';
|
||||
import {AsyncPipe, DecimalPipe, NgClass} from '@angular/common';
|
||||
import {Observable} from 'rxjs';
|
||||
import {BookService} from '../../../service/book.service';
|
||||
import {Rating} from 'primeng/rating';
|
||||
import {Rating, RatingRateEvent} from 'primeng/rating';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {Tag} from 'primeng/tag';
|
||||
import {Book, BookMetadata, BookRecommendation} from '../../../model/book.model';
|
||||
@@ -278,16 +278,18 @@ export class MetadataViewerComponent implements OnInit, OnChanges {
|
||||
}
|
||||
}
|
||||
|
||||
getStarColor(rating?: number | null): string {
|
||||
getStarColorScaled(rating?: number | null, maxScale: number = 5): string {
|
||||
if (rating == null) {
|
||||
return 'rgb(203, 213, 225)';
|
||||
} else if (rating >= 4.5) {
|
||||
}
|
||||
const normalized = rating / maxScale;
|
||||
if (normalized >= 0.9) {
|
||||
return 'rgb(34, 197, 94)';
|
||||
} else if (rating >= 4) {
|
||||
} else if (normalized >= 0.75) {
|
||||
return 'rgb(52, 211, 153)';
|
||||
} else if (rating >= 3.5) {
|
||||
} else if (normalized >= 0.6) {
|
||||
return 'rgb(234, 179, 8)';
|
||||
} else if (rating >= 2.5) {
|
||||
} else if (normalized >= 0.4) {
|
||||
return 'rgb(249, 115, 22)';
|
||||
} else {
|
||||
return 'rgb(239, 68, 68)';
|
||||
@@ -310,4 +312,19 @@ export class MetadataViewerComponent implements OnInit, OnChanges {
|
||||
if (progress == null) return 'bg-gray-600';
|
||||
return 'bg-blue-500';
|
||||
}
|
||||
|
||||
onPersonalRatingChange(book: Book, event: RatingRateEvent): void {
|
||||
const rating = event.value;
|
||||
if (!book || !book.metadata) return;
|
||||
const updatedMetadata = {
|
||||
...book.metadata,
|
||||
personalRating: rating
|
||||
};
|
||||
this.bookService.updateBookMetadata(book.id, updatedMetadata, false).subscribe({
|
||||
next: () => {
|
||||
},
|
||||
error: (err) => {
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user