Skip to content

Commit

Permalink
rxjs: final example
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesHoppe committed Jan 29, 2025
1 parent 29ecac6 commit 49b87ba
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
14 changes: 10 additions & 4 deletions book-rating/src/app/books/book-details/book-details.component.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<!-- <h2>{{ isbn() }}</h2> -->
@if (book(); as b) {

<pre>
{{ book() | json }}
</pre>
<h2>{{ b.title }}</h2>
<p>
{{ b.description }}
</p>

}
@else {
Waiting for the first book!
}
<hr>

<a routerLink="/books/9783864909467">Go to Angular book</a><br>
<a routerLink="/books/9783864905520">Go to React book</a><br>
<a routerLink="/books/404">Trigger an Error</a><br>
23 changes: 16 additions & 7 deletions book-rating/src/app/books/book-details/book-details.component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { JsonPipe } from '@angular/common';
import { Component, inject, signal } from '@angular/core';
import { HttpErrorResponse } from '@angular/common/http';
import { Component, inject } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { ActivatedRoute, RouterLink } from '@angular/router';
import { concatMap, map, mergeMap, switchMap } from 'rxjs';
import { catchError, map, of, retry, switchMap } from 'rxjs';

import { BookStoreService } from '../shared/book-store.service';

@Component({
selector: 'app-book-details',
imports: [
RouterLink,
JsonPipe
RouterLink
],
templateUrl: './book-details.component.html',
styleUrl: './book-details.component.scss'
Expand All @@ -20,7 +20,16 @@ export class BookDetailsComponent {

book = toSignal(inject(ActivatedRoute).paramMap.pipe(
map(paramMap => paramMap.get('isbn') || ''),
switchMap(isbn => this.bs.getSingle(isbn))
))
switchMap(isbn => this.bs.getSingle(isbn).pipe(
retry({
count: 3,
delay: 1000
}),
catchError((err: HttpErrorResponse) => of({
title: 'ERROR',
description: err.message
}))
))
));

}
2 changes: 1 addition & 1 deletion book-rating/src/app/books/shared/book-store.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export class BookStoreService {
}

getSingle(isbn: string): Observable<Book> {
return this.http.get<Book>(`https://api.angular.schule/books/${isbn}/slow`)
return this.http.get<Book>(`https://api.angular.schule/books/${isbn}`)
}
}

0 comments on commit 49b87ba

Please sign in to comment.