From 5573094cf46b1bf377b03815856e064b93737248 Mon Sep 17 00:00:00 2001 From: Johannes Hoppe Date: Sun, 3 Nov 2024 11:35:20 +0100 Subject: [PATCH] simpler handleRatingChange --- src/app/books/dashboard/dashboard.component.html | 2 +- src/app/books/dashboard/dashboard.component.ts | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/app/books/dashboard/dashboard.component.html b/src/app/books/dashboard/dashboard.component.html index 63757a2..24dfa19 100644 --- a/src/app/books/dashboard/dashboard.component.html +++ b/src/app/books/dashboard/dashboard.component.html @@ -3,7 +3,7 @@ @for (b of books(); track b.isbn) { diff --git a/src/app/books/dashboard/dashboard.component.ts b/src/app/books/dashboard/dashboard.component.ts index 6155909..088a064 100644 --- a/src/app/books/dashboard/dashboard.component.ts +++ b/src/app/books/dashboard/dashboard.component.ts @@ -20,7 +20,17 @@ export class DashboardComponent { this.books.update(books => books.toReversed()); } - handleRatingChange({ isbn, newRating }: { isbn: string, newRating: number }) { - this.books.update(books => books.map(b => b.isbn === isbn ? { ...b, rating: newRating } : b)); + handleRatingChange(isbn: string, newRating: number) { + this.books.update(books => + books.map(b => { + // if this is the book we want to update, set the new rating + if (b.isbn === isbn) { + return { ...b, rating: newRating }; + } else { + // leave all other books in the list unchanged + return b; + } + }) + ); } }