File size: 1,842 Bytes
1e92f2d |
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 55 56 57 58 59 60 |
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.#hasNextPage() || this.#isFetchingNextPage(),
)
readonly nextButtonText = computed(() =>
this.#isFetchingNextPage()
? 'Loading more...'
: this.#hasNextPage()
? 'Load newer'
: 'Nothing more to load',
)
readonly previousButtonDisabled = computed(
() => !this.#hasPreviousPage() || this.#isFetchingNextPage(),
)
readonly previousButtonText = computed(() =>
this.#isFetchingPreviousPage()
? 'Loading more...'
: this.#hasPreviousPage()
? 'Load Older'
: 'Nothing more to load',
)
readonly #hasPreviousPage = this.query.hasPreviousPage
readonly #hasNextPage = this.query.hasNextPage
readonly #isFetchingPreviousPage = this.query.isFetchingPreviousPage
readonly #isFetchingNextPage = this.query.isFetchingNextPage
}
|