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

example.component.ts

46 lines1.4 KB

1import { Component, computed, inject } from '@angular/core'
2import { injectInfiniteQuery } from '@tanstack/angular-query'
3import { ProjectStyleDirective } from '../directives/project-style.directive'
4import { ProjectsQueries } from '../services/projects/queries'
5​
6@Component({
7 selector: 'example',
8 templateUrl: './example.component.html',
9 imports: [ProjectStyleDirective],
10})
11export class ExampleComponent {
12 readonly #projectsQueries = inject(ProjectsQueries)
13​
14 readonly query = injectInfiniteQuery(() => ({
15 ...this.#projectsQueries.list(),
16 maxPages: 3,
17 }))
18​
19 readonly nextButtonDisabled = computed(
20 () => !this.#hasNextPage() || this.#isFetchingNextPage(),
21 )
22​
23 readonly nextButtonText = computed(() =>
24 this.#isFetchingNextPage()
25 ? 'Loading more...'
26 : this.#hasNextPage()
27 ? 'Load newer'
28 : 'Nothing more to load',
29 )
30​
31 readonly previousButtonDisabled = computed(
32 () => !this.#hasPreviousPage() || this.#isFetchingPreviousPage(),
33 )
34 readonly previousButtonText = computed(() =>
35 this.#isFetchingPreviousPage()
36 ? 'Loading more...'
37 : this.#hasPreviousPage()
38 ? 'Load Older'
39 : 'Nothing more to load',
40 )
41​
42 readonly #hasPreviousPage = this.query.hasPreviousPage
43 readonly #hasNextPage = this.query.hasNextPage
44 readonly #isFetchingPreviousPage = this.query.isFetchingPreviousPage
45 readonly #isFetchingNextPage = this.query.isFetchingNextPage