diff --git a/frontend/src/app/viewforum/viewforum.component.css b/frontend/src/app/viewforum/viewforum.component.css index 18096a6..ccf3cfb 100644 --- a/frontend/src/app/viewforum/viewforum.component.css +++ b/frontend/src/app/viewforum/viewforum.component.css @@ -78,4 +78,20 @@ tr:nth-child(even) { flex:8; font-family:system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; font-size:xx-large; + } + + .pagination{ + display: flex; + flex-direction: row; + padding: 10px; + } + + .pagination-button{ + flex: 2; + } + + .pagination-text{ + flex: 6; + align-self: center; + text-align: center; } \ No newline at end of file diff --git a/frontend/src/app/viewforum/viewforum.component.html b/frontend/src/app/viewforum/viewforum.component.html index bfc99e9..d5bb742 100644 --- a/frontend/src/app/viewforum/viewforum.component.html +++ b/frontend/src/app/viewforum/viewforum.component.html @@ -3,19 +3,24 @@

Forum

New Post - +
-
- -
{{ post.user.first_name}} {{ post.user.last_name}} {{ post.timestamp }}
+
+ +
{{ post.user.first_name}} {{ post.user.last_name}} {{ post.timestamp }}

{{ post.title }}

{{ post.content }}

- -
+
+ + diff --git a/frontend/src/app/viewforum/viewforum.component.ts b/frontend/src/app/viewforum/viewforum.component.ts index 431ed30..cba8db8 100644 --- a/frontend/src/app/viewforum/viewforum.component.ts +++ b/frontend/src/app/viewforum/viewforum.component.ts @@ -3,6 +3,9 @@ import { Post, PostService } from '../post.service'; import { Observable } from 'rxjs'; import { PermissionService } from '../permission.service'; import { HttpErrorResponse } from '@angular/common/http'; +import { MatPaginator } from '@angular/material/paginator'; +import { map } from 'rxjs/operators'; +import { tap } from 'rxjs/operators'; @Component({ selector: 'app-getforum', @@ -12,7 +15,12 @@ import { HttpErrorResponse } from '@angular/common/http'; export class viewforumComponent { public adminPermission$: Observable; public post$: Observable; - + public displayPost$: Observable; + public itemsPerPage: number = 5; // set the number of items to display per page + public currentPage: number = 1; + public numPosts: number; + public numPages: number; + public static Route = { path: 'viewforum', component: viewforumComponent @@ -22,7 +30,19 @@ export class viewforumComponent { private postService: PostService, private permission: PermissionService, ) { + this.numPosts = 0; //numPosts and numPages must be set to zero initially + this.numPages = 0; this.post$ = postService.getPosts() + this.displayPost$ = this.post$.pipe( //paginating total number of posts + map((items) => { + const startIndex = (this.currentPage - 1) * this.itemsPerPage; + return items.slice(startIndex, startIndex + this.itemsPerPage); + })) + this.post$.subscribe((items) => { //set number of pages and number of total posts + this.numPosts = items.length; + this.numPages = Math.ceil(items.length / this.itemsPerPage) + }); + this.adminPermission$ = this.permission.check('admin.view', 'admin/') } @@ -37,6 +57,15 @@ export class viewforumComponent { private onSuccess(): void { // get new posts after deletion this.post$ = this.postService.getPosts() + this.displayPost$ = this.post$.pipe( + map((items) => { + const startIndex = (this.currentPage - 1) * this.itemsPerPage; + return items.slice(startIndex, startIndex + this.itemsPerPage); + })) + this.post$.subscribe((items) => { //update number of pages after deletion + this.numPosts = items.length; + this.numPages = Math.ceil(items.length / this.itemsPerPage) + }); } private onError(err: HttpErrorResponse) { @@ -47,4 +76,17 @@ export class viewforumComponent { } } + public getCurrentPage(pageNumber: number): Observable { + return this.displayPost$ + } + + updateItems() { //updating displayedPosts based on what page is currently selected + this.displayPost$ = this.post$.pipe( + map((items) => { + const startIndex = (this.currentPage - 1) * this.itemsPerPage; + return items.slice(startIndex, startIndex + this.itemsPerPage); + }) + ); + } + }