Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

example.component.ts

59 lines1.8 KB

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