-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathexample.component.ts
More file actions
54 lines (49 loc) · 1.61 KB
/
example.component.ts
File metadata and controls
54 lines (49 loc) · 1.61 KB
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 {
ChangeDetectionStrategy,
Component,
computed,
inject,
} from '@angular/core'
import { injectInfiniteQuery } from '@tanstack/angular-query-experimental'
import { lastValueFrom } from 'rxjs'
import { ProjectStyleDirective } from '../directives/project-style.directive'
import { ProjectsService } from '../services/projects.service'
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'example',
templateUrl: './example.component.html',
imports: [ProjectStyleDirective],
})
export class ExampleComponent {
readonly projectsService = inject(ProjectsService)
readonly query = injectInfiniteQuery(() => ({
queryKey: ['projects'],
queryFn: ({ pageParam }) => {
return lastValueFrom(this.projectsService.getProjects(pageParam))
},
initialPageParam: 0,
getPreviousPageParam: (firstPage) => firstPage.previousId ?? undefined,
getNextPageParam: (lastPage) => lastPage.nextId ?? undefined,
maxPages: 3,
}))
readonly nextButtonDisabled = computed(
() => !this.query.hasNextPage() || this.query.isFetchingNextPage(),
)
readonly nextButtonText = computed(() =>
this.query.isFetchingNextPage()
? 'Loading more...'
: this.query.hasNextPage()
? 'Load newer'
: 'Nothing more to load',
)
readonly previousButtonDisabled = computed(
() => !this.query.hasPreviousPage() || this.query.isFetchingPreviousPage(),
)
readonly previousButtonText = computed(() =>
this.query.isFetchingPreviousPage()
? 'Loading more...'
: this.query.hasPreviousPage()
? 'Load Older'
: 'Nothing more to load',
)
}