Skip to content

Commit

Permalink
simpler handleRatingChange
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesHoppe committed Nov 3, 2024
1 parent 6c3a104 commit 5573094
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/app/books/dashboard/dashboard.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@for (b of books(); track b.isbn) {

<app-book
(ratingChange)="handleRatingChange($event)"
(ratingChange)="handleRatingChange($event.isbn, $event.newRating)"
[book]="b"
/>

Expand Down
14 changes: 12 additions & 2 deletions src/app/books/dashboard/dashboard.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
})
);
}
}

0 comments on commit 5573094

Please sign in to comment.