Skip to content

Instantly share code, notes, and snippets.

@touhidrahman
Created August 24, 2022 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save touhidrahman/ede43a2efe7d54beb2f58e9769baeb9c to your computer and use it in GitHub Desktop.
Save touhidrahman/ede43a2efe7d54beb2f58e9769baeb9c to your computer and use it in GitHub Desktop.
<main>
<app-movie-card
*ngFor="let movie of movies$ | async"
[movie]="movie]
></app-movie-card>
</main>
<app-pagination
[currentPage]="page$ | async"
[totalPages]="totalPages$ | async"
[size]="size$ | async"
[routerLinkBase]="['/movies']"
></app-pagination>
export class MovieListPageComponent {
page$: Observable<number> = this.activatedRoute.queryParams.pipe(map(({page}) => page ? +page : 1))
size$: Observable<number> = this.activatedRoute.queryParams.pipe(map(({size}) => size ? +size : 24))
movies$: Observable<Movie[]>
totalPages$: Observable<number>
constructor(
private movieService: MovieService,
private activatedRoute: ActivatedRoute
) {
this.movies$ = combineLatest({
page: this.page$,
size: this.size$,
}).pipe(
debounceTime(200),
switchMap(({ page, size }) => {
return this.movieService.find({ page, size})
})
)
this.totalPages$ = combineLatest({
total: this.movieService.count(),
size: this.size$,
}).pipe(
switchMap(({ total, size }) => {
return Math.ceil(total / size)
})
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment