forked from tomalaforge/angular-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage-2.ts
54 lines (50 loc) · 1.33 KB
/
page-2.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { httpResource } from '@angular/common/http';
import {
ChangeDetectionStrategy,
Component,
ResourceStatus,
signal,
} from '@angular/core';
import { ExpandableCard } from './expandable-card';
interface Post {
id: number;
title: string;
body: string;
userId: number;
}
@Component({
selector: 'app-page-2',
template: `
page2
<app-expandable-card (expanded)="onExpansion($event)">
<div title>Load Post</div>
<div>
@if (postRessource.isLoading()) {
Loading...
} @else if (postRessource.status() === ResourceStatus.Error) {
Error...
} @else {
@for (post of postRessource.value(); track post.id) {
<div>{{ post.title }}</div>
}
}
</div>
</app-expandable-card>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [ExpandableCard],
})
export class Page2 {
// Set this signal to true to start fetching the posts
private readonly $loader = signal(false);
// Fetch posts from the API
public postRessource = httpResource<Post[]>(() =>
this.$loader() ? 'https://jsonplaceholder.typicode.com/posts' : undefined,
);
protected readonly ResourceStatus = ResourceStatus;
onExpansion(isExpanded: boolean) {
if (isExpanded && !this.postRessource.hasValue()) {
this.$loader.set(true);
}
}
}