Open History to see the last commit
1import {
2 ChangeDetectionStrategy,
3 Component,
4 computed,
5 inject,
6} from '@angular/core'
7import { injectInfiniteQuery } from '@tanstack/angular-query-experimental'
8import { lastValueFrom } from 'rxjs'
9import { ProjectStyleDirective } from '../directives/project-style.directive'
10import { ProjectsService } from '../services/projects.service'
11β
12@Component({
13 changeDetection: ChangeDetectionStrategy.OnPush,
14 selector: 'example',
15 templateUrl: './example.component.html',
16 imports: [ProjectStyleDirective],
17})
18export class ExampleComponent {
19 projectsService = inject(ProjectsService)
20β
21 query = injectInfiniteQuery(() => ({
22 queryKey: ['projects'],
23 queryFn: ({ pageParam }) => {
24 return lastValueFrom(this.projectsService.getProjects(pageParam))
25 },
26 initialPageParam: 0,
27 getPreviousPageParam: (firstPage) => firstPage.previousId ?? undefined,
28 getNextPageParam: (lastPage) => lastPage.nextId ?? undefined,
29 maxPages: 3,
30 }))
31β
32 nextButtonDisabled = computed(
33 () => !this.#hasNextPage() || this.#isFetchingNextPage(),
34 )
35 nextButtonText = computed(() =>
36 this.#isFetchingNextPage()
37 ? 'Loading more...'
38 : this.#hasNextPage()
39 ? 'Load newer'
40 : 'Nothing more to load',
41 )
42 previousButtonDisabled = computed(
43 () => !this.#hasPreviousPage() || this.#isFetchingNextPage(),
44 )
45 previousButtonText = computed(() =>
46 this.#isFetchingPreviousPage()
47 ? 'Loading more...'
48 : this.#hasPreviousPage()
49 ? 'Load Older'
50 : 'Nothing more to load',
51 )
52β
53 #hasPreviousPage = this.query.hasPreviousPage
54 #hasNextPage = this.query.hasNextPage
55 #isFetchingPreviousPage = this.query.isFetchingPreviousPage
56 #isFetchingNextPage = this.query.isFetchingNextPage
57}