Skip to content

Commit d8adf73

Browse files
committed
Switch to a resolver to load data.
1 parent 927196e commit d8adf73

File tree

8 files changed

+46
-38
lines changed

8 files changed

+46
-38
lines changed

ui/src/app/app.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {ApplicationConfig, provideBrowserGlobalErrorListeners} from '@angular/core';
2-
import {provideRouter} from '@angular/router';
2+
import {provideRouter, withComponentInputBinding} from '@angular/router';
33
import {routes} from './app.routes';
44
import {providePrimeNG} from 'primeng/config';
55
import Aura from '@primeuix/themes/aura';
@@ -8,7 +8,7 @@ import {provideHttpClient, withFetch} from '@angular/common/http';
88
export const appConfig: ApplicationConfig = {
99
providers: [
1010
provideBrowserGlobalErrorListeners(),
11-
provideRouter(routes),
11+
provideRouter(routes, withComponentInputBinding()),
1212
provideHttpClient(
1313
withFetch()
1414
),

ui/src/app/app.routes.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import {Routes} from '@angular/router';
22
import {LandingPage} from './pages/landing-page/landing-page';
3+
import {libraryResolver} from './resolvers/library-resolver';
34

45
export const routes: Routes = [
56
{
6-
path: '', component: LandingPage
7+
path: '',
8+
component: LandingPage,
9+
resolve: {
10+
library: libraryResolver
11+
}
712
}
813
];
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {Book} from './book';
33
/**
44
* LibraryResponse response.
55
*/
6-
export interface LibraryResponse {
6+
export interface Library {
77
message: string,
88
books: Book[]
99
}

ui/src/app/pages/landing-page/landing-page.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<div class="card w-full">
22
<p-table
33
#libraryTable
4-
(onLazyLoad)="getLibrary()"
54
[globalFilterFields]="['title', 'author', 'shelfLocation']"
6-
[lazy]="true"
7-
[value]="library?.books ?? []"
5+
[value]="library().books ?? []"
86
class="w-full"
97
dataKey="title">
108
<ng-template #caption>
Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,20 @@
1-
import {Component, DestroyRef, inject} from '@angular/core';
1+
import {Component, input} from '@angular/core';
22
import {TableModule} from 'primeng/table';
33
import {FormsModule} from '@angular/forms';
44
import {Tag} from 'primeng/tag';
55
import {InputText} from 'primeng/inputtext';
6-
import {LibraryService} from '../../services/library-service';
7-
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
8-
import {distinctUntilChanged} from 'rxjs';
9-
import {LibraryResponse} from '../../interfaces/library-response';
6+
import {Library} from '../../interfaces/library';
107

118
@Component({
129
selector: 'app-landing-page',
1310
imports: [
1411
TableModule,
1512
FormsModule,
16-
Tag,
1713
InputText,
14+
Tag,
1815
],
1916
templateUrl: './landing-page.html'
2017
})
2118
export class LandingPage {
22-
/**
23-
* The library
24-
*/
25-
public library: LibraryResponse | null = null;
26-
private readonly _library = inject(LibraryService);
27-
private destroyRef = inject(DestroyRef);
28-
29-
/**
30-
* Get the library
31-
*/
32-
getLibrary(): void {
33-
this._library.getLibrary()
34-
.pipe(takeUntilDestroyed(this.destroyRef), distinctUntilChanged())
35-
.subscribe({
36-
next: response => {
37-
console.log("Fetched", response);
38-
this.library = response;
39-
},
40-
error: err => {
41-
console.log(err);
42-
}
43-
})
44-
}
19+
library = input.required<Library>();
4520
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { TestBed } from '@angular/core/testing';
2+
import { ResolveFn } from '@angular/router';
3+
4+
import { libraryResolver } from './library-resolver';
5+
6+
describe('libraryResolver', () => {
7+
const executeResolver: ResolveFn<boolean> = (...resolverParameters) =>
8+
TestBed.runInInjectionContext(() => libraryResolver(...resolverParameters));
9+
10+
beforeEach(() => {
11+
TestBed.configureTestingModule({});
12+
});
13+
14+
it('should be created', () => {
15+
expect(executeResolver).toBeTruthy();
16+
});
17+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { ResolveFn } from '@angular/router';
2+
import {Library} from '../interfaces/library';
3+
import {inject} from '@angular/core';
4+
import {LibraryService} from '../services/library-service';
5+
import {catchError, of} from 'rxjs';
6+
7+
export const libraryResolver: ResolveFn<Library | null> = (route, state) => {
8+
const _library = inject(LibraryService);
9+
return _library.getLibrary()
10+
.pipe(catchError((error) => {
11+
return of(null)
12+
}))
13+
};

ui/src/app/services/library-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {inject, Injectable} from '@angular/core';
22
import {HttpClient} from '@angular/common/http';
3-
import {LibraryResponse} from '../interfaces/library-response';
3+
import {Library} from '../interfaces/library';
44

55
@Injectable({
66
providedIn: 'root',
@@ -15,6 +15,6 @@ export class LibraryService {
1515
* Fetch books from the library.
1616
*/
1717
public getLibrary() {
18-
return this.http.get<LibraryResponse>('/api/GetBooks');
18+
return this.http.get<Library>('/api/GetBooks');
1919
}
2020
}

0 commit comments

Comments
 (0)