|
@@ -1,4 +1,14 @@
|
|
| 1 |
-
@if (book()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
<article>
|
| 3 |
<header>
|
| 4 |
<h1>{{ b.title }}</h1>
|
|
@@ -28,6 +38,7 @@
|
|
| 28 |
<img [src]="b.imageUrl" alt="Cover" />
|
| 29 |
<footer>
|
| 30 |
<a routerLink="/books">Back to list</a>
|
|
|
|
| 31 |
<button type="button" (click)="removeBook(b.isbn)">
|
| 32 |
Delete book
|
| 33 |
</button>
|
|
|
|
| 1 |
+
@if (book.error()) {
|
| 2 |
+
<p role="alert">Book could not be found.</p>
|
| 3 |
+
<a routerLink="/books">Back to list</a>
|
| 4 |
+
}
|
| 5 |
+
|
| 6 |
+
@if (book.isLoading()) {
|
| 7 |
+
<p aria-busy="true">Loading ...</p>
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
@if (book.hasValue()) {
|
| 11 |
+
@let b = book.value();
|
| 12 |
<article>
|
| 13 |
<header>
|
| 14 |
<h1>{{ b.title }}</h1>
|
|
|
|
| 38 |
<img [src]="b.imageUrl" alt="Cover" />
|
| 39 |
<footer>
|
| 40 |
<a routerLink="/books">Back to list</a>
|
| 41 |
+
<a routerLink="/books/details/9783864909467">Angular Book</a>
|
| 42 |
<button type="button" (click)="removeBook(b.isbn)">
|
| 43 |
Delete book
|
| 44 |
</button>
|
|
@@ -1,7 +1,6 @@
|
|
| 1 |
-
import { Component,
|
| 2 |
import { Router, RouterLink } from '@angular/router';
|
| 3 |
|
| 4 |
-
import { Book } from '../../shared/book';
|
| 5 |
import { BookStore } from '../../shared/book-store';
|
| 6 |
|
| 7 |
@Component({
|
|
@@ -15,15 +14,7 @@ export class BookDetailsPage {
|
|
| 15 |
#router = inject(Router);
|
| 16 |
|
| 17 |
readonly isbn = input.required<string>();
|
| 18 |
-
protected book =
|
| 19 |
-
|
| 20 |
-
constructor() {
|
| 21 |
-
effect(() => {
|
| 22 |
-
this.#bookStore.getSingle(this.isbn()).subscribe(book => {
|
| 23 |
-
this.book.set(book);
|
| 24 |
-
});
|
| 25 |
-
});
|
| 26 |
-
}
|
| 27 |
|
| 28 |
removeBook(isbn: string) {
|
| 29 |
if (window.confirm('Delete book?')) {
|
|
|
|
| 1 |
+
import { Component, inject, input } from '@angular/core';
|
| 2 |
import { Router, RouterLink } from '@angular/router';
|
| 3 |
|
|
|
|
| 4 |
import { BookStore } from '../../shared/book-store';
|
| 5 |
|
| 6 |
@Component({
|
|
|
|
| 14 |
#router = inject(Router);
|
| 15 |
|
| 16 |
readonly isbn = input.required<string>();
|
| 17 |
+
protected book = this.#bookStore.getSingle(this.isbn);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
removeBook(isbn: string) {
|
| 20 |
if (window.confirm('Delete book?')) {
|
|
@@ -12,6 +12,10 @@
|
|
| 12 |
|
| 13 |
<section>
|
| 14 |
<h1>Books</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
<div>
|
| 16 |
<input
|
| 17 |
type="search"
|
|
|
|
| 12 |
|
| 13 |
<section>
|
| 14 |
<h1>Books</h1>
|
| 15 |
+
<button type="button" (click)="books.reload()" [aria-busy]="books.isLoading()">
|
| 16 |
+
Reload
|
| 17 |
+
</button>
|
| 18 |
+
|
| 19 |
<div>
|
| 20 |
<input
|
| 21 |
type="search"
|
|
@@ -15,24 +15,22 @@ export class BooksOverviewPage {
|
|
| 15 |
|
| 16 |
protected searchTerm = signal('');
|
| 17 |
|
| 18 |
-
protected books =
|
| 19 |
protected likedBooks = signal<Book[]>([]);
|
| 20 |
|
| 21 |
protected filteredBooks = computed(() => {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
if (!this.searchTerm()) {
|
| 23 |
-
return this.books();
|
| 24 |
}
|
| 25 |
|
| 26 |
const term = this.searchTerm().toLowerCase();
|
| 27 |
-
return this.books().filter((b) => b.title.toLowerCase().includes(term));
|
| 28 |
});
|
| 29 |
|
| 30 |
-
constructor() {
|
| 31 |
-
this.#bookStore.getAll().subscribe(books => {
|
| 32 |
-
this.books.set(books);
|
| 33 |
-
});
|
| 34 |
-
}
|
| 35 |
-
|
| 36 |
addLikedBook(newLikedBook: Book) {
|
| 37 |
const foundBook = this.likedBooks().find(
|
| 38 |
(b) => b.isbn === newLikedBook.isbn
|
|
|
|
| 15 |
|
| 16 |
protected searchTerm = signal('');
|
| 17 |
|
| 18 |
+
protected books = this.#bookStore.getAll();
|
| 19 |
protected likedBooks = signal<Book[]>([]);
|
| 20 |
|
| 21 |
protected filteredBooks = computed(() => {
|
| 22 |
+
if (!this.books.hasValue()) {
|
| 23 |
+
return [];
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
if (!this.searchTerm()) {
|
| 27 |
+
return this.books.value();
|
| 28 |
}
|
| 29 |
|
| 30 |
const term = this.searchTerm().toLowerCase();
|
| 31 |
+
return this.books.value().filter((b) => b.title.toLowerCase().includes(term));
|
| 32 |
});
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
addLikedBook(newLikedBook: Book) {
|
| 35 |
const foundBook = this.likedBooks().find(
|
| 36 |
(b) => b.isbn === newLikedBook.isbn
|
|
@@ -1,5 +1,5 @@
|
|
| 1 |
-
import { inject, Injectable } from '@angular/core';
|
| 2 |
-
import { HttpClient } from '@angular/common/http';
|
| 3 |
import { Observable } from 'rxjs';
|
| 4 |
|
| 5 |
import { Book } from './book';
|
|
@@ -11,12 +11,17 @@ export class BookStore {
|
|
| 11 |
#http = inject(HttpClient);
|
| 12 |
#apiUrl = 'https://api6.angular-buch.com';
|
| 13 |
|
| 14 |
-
getAll():
|
| 15 |
-
return
|
|
|
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
-
getSingle(isbn: string):
|
| 19 |
-
return
|
|
|
|
|
|
|
| 20 |
}
|
| 21 |
|
| 22 |
remove(isbn: string): Observable<void> {
|
|
|
|
| 1 |
+
import { inject, Injectable, Signal } from '@angular/core';
|
| 2 |
+
import { HttpClient, httpResource, HttpResourceRef } from '@angular/common/http';
|
| 3 |
import { Observable } from 'rxjs';
|
| 4 |
|
| 5 |
import { Book } from './book';
|
|
|
|
| 11 |
#http = inject(HttpClient);
|
| 12 |
#apiUrl = 'https://api6.angular-buch.com';
|
| 13 |
|
| 14 |
+
getAll(): HttpResourceRef<Book[]> {
|
| 15 |
+
return httpResource<Book[]>(
|
| 16 |
+
() => `${this.#apiUrl}/books`,
|
| 17 |
+
{ defaultValue: [] }
|
| 18 |
+
);
|
| 19 |
}
|
| 20 |
|
| 21 |
+
getSingle(isbn: Signal<string>): HttpResourceRef<Book | undefined> {
|
| 22 |
+
return httpResource<Book>(
|
| 23 |
+
() => `${this.#apiUrl}/books/${isbn()}`
|
| 24 |
+
);
|
| 25 |
}
|
| 26 |
|
| 27 |
remove(isbn: string): Observable<void> {
|