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 readonly projectsService = inject(ProjectsService)
20β
21 readonly 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 readonly nextButtonDisabled = computed(
33 () => !this.#hasNextPage() || this.#isFetchingNextPage(),
34 )
35β
36 readonly nextButtonText = computed(() =>
37 this.#isFetchingNextPage()
38 ? 'Loading more...'
39 : this.#hasNextPage()
40 ? 'Load newer'
41 : 'Nothing more to load',
42 )
43β
44 readonly previousButtonDisabled = computed(
45 () => !this.#hasPreviousPage() || this.#isFetchingNextPage(),
46 )
47 readonly previousButtonText = computed(() =>
48 this.#isFetchingPreviousPage()
49 ? 'Loading more...'
50 : this.#hasPreviousPage()
51 ? 'Load Older'
52 : 'Nothing more to load',
53 )
54β
55 readonly #hasPreviousPage = this.query.hasPreviousPage
56 readonly #hasNextPage = this.query.hasNextPage
57 readonly #isFetchingPreviousPage = this.query.isFetchingPreviousPage
58 readonly #isFetchingNextPage = this.query.isFetchingNextPage
59}