Skip to content

Commit 9fcaa03

Browse files
chore(demo): add withResource example (#250)
1 parent de76bcc commit 9fcaa03

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

apps/demo/src/app/app.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<a mat-list-item routerLink="/conditional">withConditional</a>
3232
<a mat-list-item routerLink="/mutation">withMutation</a>
3333
<a mat-list-item routerLink="/rx-mutation">rxMutation (without Store)</a>
34+
<a mat-list-item routerLink="/with-resource">withResource</a>
3435
</mat-nav-list>
3536
</mat-drawer>
3637
<mat-drawer-content>

apps/demo/src/app/lazy-routes.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,11 @@ export const lazyRoutes: Route[] = [
7575
(m) => m.CounterRxMutation,
7676
),
7777
},
78+
{
79+
path: 'with-resource',
80+
loadComponent: () =>
81+
import('./with-resource/with-resource.component').then(
82+
(m) => m.WithResourceComponent,
83+
),
84+
},
7885
];
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { withResource } from '@angular-architects/ngrx-toolkit';
2+
import { JsonPipe } from '@angular/common';
3+
import { httpResource } from '@angular/common/http';
4+
import { Component, inject } from '@angular/core';
5+
import { signalStore, withState } from '@ngrx/signals';
6+
import { Flight } from '../shared/flight';
7+
8+
const url = 'https://demo.angulararchitects.io/api/flight?from=Paris&to=';
9+
10+
export const FlightStore = signalStore(
11+
withState({ flightTo: 'New York' }),
12+
withResource(({ flightTo }) => httpResource(() => `${url}${flightTo()}`)),
13+
withResource(({ flightTo }) => ({
14+
list: httpResource<Flight[]>(() => `${url}${flightTo()}`, {
15+
defaultValue: [],
16+
}),
17+
})),
18+
);
19+
20+
@Component({
21+
selector: 'demo-with-resource',
22+
imports: [JsonPipe],
23+
template: `,
24+
<h1>withResource</h1>
25+
<a
26+
href="https://ngrx-toolkit.angulararchitects.io/docs/with-resource"
27+
target="_blank"
28+
>withResource doc page</a
29+
>
30+
31+
<h2>Single Resource</h2>
32+
<pre>value: {{ store.value() | json }}</pre>
33+
<pre>status: {{ store.status() }}</pre>
34+
<pre>error: {{ store.error() | json }}</pre>
35+
<pre>hasValue: {{ store.hasValue() }}</pre>
36+
37+
<h2>Named Resource</h2>
38+
<pre>{{ store.listValue() | json }}</pre>
39+
<pre>status: {{ store.listStatus() }}</pre>
40+
<pre>error: {{ store.listError() | json }}</pre>
41+
<pre>hasValue: {{ store.listHasValue() }}</pre> `,
42+
providers: [FlightStore],
43+
})
44+
export class WithResourceComponent {
45+
store = inject(FlightStore);
46+
}

0 commit comments

Comments
 (0)