-
-
Notifications
You must be signed in to change notification settings - Fork 1
feat(posts, series): load more posts on scroll #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
esteecodes
merged 3 commits into
HashnodeWithAngular:develop
from
czBalazs98:pagination
Mar 20, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
24 changes: 15 additions & 9 deletions
24
angular-primeng-app/src/app/components/posts/posts.component.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,19 @@ | ||
| <div class="posts-view"> | ||
| <div class="posts-view" infiniteScroll [isActiveInfiniteScroll]="isActiveInfiniteScroll" (scrolled)="loadMorePosts()"> | ||
| <div class="cards-wrapper grid"> | ||
| @for (post of posts$ | async; track post.id) { | ||
| <a [routerLink]="['post', post.slug]"> | ||
| <p-card class="post-card" header="{{ post.title }}"> | ||
| <ng-template pTemplate="header"> | ||
| <img class="card-image" [src]="post.coverImage.url" [alt]="post.title + ' image'" /> | ||
| </ng-template> | ||
| </p-card> | ||
| </a> | ||
| @for (post of posts; track post) { | ||
| <a [routerLink]="['post', post.slug]"> | ||
| <p-card class="post-card" header="{{ post.title }}"> | ||
| <ng-template pTemplate="header"> | ||
| <img class="card-image" [src]="post.coverImage.url" [alt]="post.title + ' image'"/> | ||
| </ng-template> | ||
| </p-card> | ||
| </a> | ||
| } | ||
| </div> | ||
|
|
||
| @if (paginationInfo.hasNextPage && !isHiddenLoadMore) { | ||
| <div class="load-more-posts"> | ||
| <p-button (click)="loadMorePosts()">Load more</p-button> | ||
| </div> | ||
| } | ||
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 34 additions & 6 deletions
40
angular-primeng-app/src/app/components/posts/posts.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,53 @@ | ||
| import { Component, OnInit, inject } from '@angular/core'; | ||
| import { Component, inject, OnInit } from '@angular/core'; | ||
| import { BlogService } from '../../services/blog.service'; | ||
| import { RouterLink } from '@angular/router'; | ||
| import { Observable } from 'rxjs'; | ||
| import { AsyncPipe } from '@angular/common'; | ||
| import { CardModule } from 'primeng/card'; | ||
| import { Post } from '../../models/post'; | ||
| import { PageInfo, Post } from '../../models/post'; | ||
| import { InfiniteScrollDirective } from "../../directives/infinite-scroll.directive"; | ||
| import { ButtonModule } from "primeng/button"; | ||
|
|
||
| @Component({ | ||
| selector: 'app-posts', | ||
| standalone: true, | ||
| imports: [AsyncPipe, RouterLink, CardModule], | ||
| imports: [AsyncPipe, RouterLink, CardModule, InfiniteScrollDirective, ButtonModule], | ||
| templateUrl: './posts.component.html', | ||
| styleUrl: './posts.component.scss' | ||
| }) | ||
| export class PostsComponent implements OnInit { | ||
| blogURL!: string; | ||
| posts$!: Observable<Post[]>; | ||
| posts!: Post[]; | ||
| paginationInfo: PageInfo = { hasNextPage: true, endCursor: ''}; | ||
| isHiddenLoadMore: boolean = true; | ||
| isActiveInfiniteScroll: boolean = false; | ||
|
|
||
| private blogService = inject(BlogService); | ||
|
|
||
| ngOnInit() { | ||
| this.blogURL = this.blogService.getBlogURL(); | ||
| this.posts$ = this.blogService.getPosts(this.blogURL); | ||
| this.loadPosts(); | ||
| } | ||
|
|
||
| private loadPosts(): void { | ||
| this.blogService.getPosts(this.blogURL, this.paginationInfo.endCursor).subscribe(postsPageInfo => { | ||
| this.paginationInfo = postsPageInfo.pagination; | ||
| this.isHiddenLoadMore = !postsPageInfo.pagination.hasNextPage; | ||
| this.posts = postsPageInfo.posts; | ||
| }); | ||
| } | ||
|
|
||
| loadMorePosts(): void { | ||
| if (!this.paginationInfo.hasNextPage) { | ||
| return; | ||
| } | ||
|
|
||
| this.isHiddenLoadMore = true; | ||
|
|
||
| this.blogService.getPosts(this.blogURL, this.paginationInfo.endCursor) | ||
| .subscribe(newPosts => { | ||
| this.isActiveInfiniteScroll = true; | ||
| this.paginationInfo = newPosts.pagination; | ||
| this.posts = this.posts.concat(newPosts.posts); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,4 +13,10 @@ | |
| display: flex; | ||
| } | ||
| } | ||
|
|
||
| .load-more-posts { | ||
| display: flex; | ||
| justify-content: center; | ||
| margin-bottom: 2rem; | ||
| } | ||
| } | ||
44 changes: 33 additions & 11 deletions
44
angular-primeng-app/src/app/components/series/series.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,54 @@ | ||
| import { Component, inject } from '@angular/core'; | ||
| import { ActivatedRoute, Params, RouterLink } from '@angular/router'; | ||
| import { Observable, switchMap } from 'rxjs'; | ||
| import { Post } from '../../models/post'; | ||
| import { ActivatedRoute, RouterLink } from '@angular/router'; | ||
| import { PageInfo, Post } from '../../models/post'; | ||
| import { AsyncPipe } from "@angular/common"; | ||
| import { BlogService } from '../../services/blog.service'; | ||
| import { CardModule } from 'primeng/card'; | ||
| import { InfiniteScrollDirective } from "../../directives/infinite-scroll.directive"; | ||
| import { ButtonModule } from "primeng/button"; | ||
|
|
||
| @Component({ | ||
| selector: 'app-series', | ||
| standalone: true, | ||
| imports: [RouterLink, AsyncPipe, CardModule], | ||
| imports: [RouterLink, AsyncPipe, CardModule, InfiniteScrollDirective, ButtonModule], | ||
| templateUrl: './series.component.html', | ||
| styleUrl: './series.component.scss' | ||
| }) | ||
| export class SeriesComponent { | ||
| blogURL!: string; | ||
| slug: string = ""; | ||
| postsInSeries$!: Observable<Post[]>; | ||
| postsInSeries: Post[] = []; | ||
| paginationInfo: PageInfo = { hasNextPage: true, endCursor: '' }; | ||
| isHiddenLoadMore: boolean = true; | ||
| isActiveInfiniteScroll: boolean = false; | ||
|
|
||
| blogService: BlogService = inject(BlogService); | ||
| route: ActivatedRoute = inject(ActivatedRoute); | ||
|
|
||
| ngOnInit(): void { | ||
| this.blogURL = this.blogService.getBlogURL(); | ||
| this.postsInSeries$ = this.route.params.pipe( | ||
| switchMap((params: Params) => { | ||
| this.slug = params["slug"]; | ||
| return this.blogService.getPostsInSeries(this.blogURL, this.slug); | ||
| }) | ||
| ); | ||
| this.route.params.subscribe(params => { | ||
| this.slug = params['slug']; | ||
| this.loadPostsInSeries(); | ||
| }) | ||
| } | ||
|
|
||
| private loadPostsInSeries():void{ | ||
| this.blogService.getPostsInSeries(this.blogURL, this.slug).subscribe(seriesPageInfo => { | ||
| this.paginationInfo = seriesPageInfo.pagination; | ||
| this.isHiddenLoadMore = !seriesPageInfo.pagination.hasNextPage; | ||
| this.postsInSeries = seriesPageInfo.posts; | ||
| }) | ||
| } | ||
|
|
||
| loadMorePostsFromSeries():void { | ||
| if (!this.paginationInfo.hasNextPage) return; | ||
| this.isHiddenLoadMore = true; | ||
| this.blogService.getPostsInSeries(this.blogURL, this.slug, this.paginationInfo.endCursor).pipe( | ||
| ).subscribe(newPosts => { | ||
| this.isActiveInfiniteScroll = true; | ||
| this.paginationInfo = newPosts.pagination; | ||
| this.postsInSeries = this.postsInSeries.concat(newPosts.posts); | ||
| }); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
angular-primeng-app/src/app/directives/infinite-scroll.directive.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { InfiniteScrollDirective } from './infinite-scroll.directive'; | ||
|
|
||
| describe('InfiniteScrollDirective', () => { | ||
| it('should create an instance', () => { | ||
| const directive = new InfiniteScrollDirective(); | ||
| expect(directive).toBeTruthy(); | ||
| }); | ||
| }); |
55 changes: 55 additions & 0 deletions
55
angular-primeng-app/src/app/directives/infinite-scroll.directive.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { Directive, effect, EventEmitter, input, Input, InputSignal, OnDestroy, Output } from '@angular/core'; | ||
| import { fromEvent, Subject, takeUntil } from 'rxjs'; | ||
|
|
||
| @Directive({ | ||
| selector: '[infiniteScroll]', | ||
| standalone: true | ||
| }) | ||
| export class InfiniteScrollDirective implements OnDestroy { | ||
| isActiveInfiniteScroll: InputSignal<boolean> = input(false); | ||
| @Input() infiniteScrollDistance: number = 20; | ||
|
|
||
| @Output() scrolled = new EventEmitter<void>; | ||
|
|
||
| private unsubscribe: Subject<void> = new Subject<void>(); | ||
|
|
||
| constructor() { | ||
| this._listenSignals(); | ||
| } | ||
|
|
||
| ngOnDestroy(): void { | ||
| this.unsubscribe.next(); | ||
| this.unsubscribe.complete(); | ||
| } | ||
|
|
||
| private listenScrollWindow(): void { | ||
| let isActivePercentageBeforeEnd = false; | ||
| fromEvent(window, 'scroll').pipe( | ||
| takeUntil(this.unsubscribe)) | ||
| .subscribe(() => { | ||
| let percentageBeforeEnd = this.calculatePositionScroll(); | ||
| if (!isActivePercentageBeforeEnd && percentageBeforeEnd <= this.infiniteScrollDistance) { | ||
| isActivePercentageBeforeEnd = true; | ||
| this.scrolled.emit(); | ||
| } else if (percentageBeforeEnd >= this.infiniteScrollDistance) { | ||
| isActivePercentageBeforeEnd = false; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private calculatePositionScroll(): number { | ||
| let scrollHeight = document.documentElement.scrollHeight; | ||
| let innerHeight = window.innerHeight; | ||
| let scrollY = window.scrollY || 0; | ||
| return (scrollHeight - scrollY - innerHeight) / scrollHeight * 100; | ||
| } | ||
|
|
||
| private _listenSignals(): void { | ||
| effect(() => { | ||
| this.unsubscribe.next(); | ||
| if (this.isActiveInfiniteScroll()) { | ||
| this.listenScrollWindow(); | ||
| } | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.