|
| 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