Skip to content

Commit 5b6b267

Browse files
committed
feat(challenge 59): added lazy loading for posts
1 parent 99a3647 commit 5b6b267

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

apps/angular/59-content-projection-defer/src/app/expandable-card.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
1+
import {
2+
ChangeDetectionStrategy,
3+
Component,
4+
output,
5+
signal,
6+
} from '@angular/core';
27

38
@Component({
49
selector: 'app-expandable-card',
510
template: `
611
<button
712
class="text-fg-subtle hover:bg-button-secondary-bg-hover active:bg-button-secondary-bg-active focus:outline-button-border-highlight flex w-fit items-center gap-1 py-2 focus:outline focus:outline-2 focus:outline-offset-1"
8-
(click)="isExpanded.set(!isExpanded())"
13+
(click)="expandedChange()"
914
data-cy="expandable-panel-toggle">
1015
@if (isExpanded()) {
1116
<svg
@@ -51,4 +56,10 @@ import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
5156
})
5257
export class ExpandableCard {
5358
public isExpanded = signal(false);
59+
expanded = output<boolean>();
60+
61+
expandedChange = () => {
62+
this.isExpanded.set(!this.isExpanded());
63+
this.expanded.emit(this.isExpanded());
64+
};
5465
}

apps/angular/59-content-projection-defer/src/app/page-2.ts

+16-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
ChangeDetectionStrategy,
44
Component,
55
ResourceStatus,
6+
signal,
67
} from '@angular/core';
78
import { ExpandableCard } from './expandable-card';
89

@@ -17,16 +18,18 @@ interface Post {
1718
selector: 'app-page-2',
1819
template: `
1920
page2
20-
<app-expandable-card>
21+
<app-expandable-card (expanded)="triggerFetch.set($event)">
2122
<div title>Load Post</div>
2223
<div>
23-
@if (postRessource.isLoading()) {
24-
Loading...
25-
} @else if (postRessource.status() === ResourceStatus.Error) {
26-
Error...
27-
} @else {
28-
@for (post of postRessource.value(); track post.id) {
29-
<div>{{ post.title }}</div>
24+
@if (postRessource) {
25+
@if (postRessource.isLoading()) {
26+
Loading...
27+
} @else if (postRessource.status() === ResourceStatus.Error) {
28+
Error...
29+
} @else {
30+
@for (post of postRessource.value(); track post.id) {
31+
<div>{{ post.title }}</div>
32+
}
3033
}
3134
}
3235
</div>
@@ -36,8 +39,11 @@ interface Post {
3639
imports: [ExpandableCard],
3740
})
3841
export class Page2 {
39-
public postRessource = httpResource<Post[]>(
40-
'https://jsonplaceholder.typicode.com/posts',
42+
protected triggerFetch = signal(false);
43+
public postRessource = httpResource<Post[]>(() =>
44+
!this.triggerFetch()
45+
? undefined
46+
: 'https://jsonplaceholder.typicode.com/posts',
4147
);
4248
protected readonly ResourceStatus = ResourceStatus;
4349
}

0 commit comments

Comments
 (0)