Skip to content
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

Create deleted post functions, adjusted admin tab to call deleted posts. #77

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion frontend/src/app/admin/admin.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,16 @@
</nav>
<mat-tab-nav-panel #tabPanel>
<router-outlet></router-outlet>
</mat-tab-nav-panel>
</mat-tab-nav-panel>
<div>
<h1>
List of Deleted Posts
</h1>
<div *ngFor="let post of deletedPost$ | async">
<div class="forum-user-name"> {{ post.user.first_name}} {{ post.user.last_name}} {{ post.timestamp }}</div>
<h1 class="post-body">{{ post.title }}</h1>
<h2 class="post-body">{{ post.content }}</h2>
<div></div>

</div>
</div>
13 changes: 12 additions & 1 deletion frontend/src/app/admin/admin.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { Profile, ProfileService } from '../profile/profile.service';
import { Post, PostService } from '../post.service';

@Component({
selector: 'app-admin',
Expand All @@ -10,16 +11,26 @@ import { Profile, ProfileService } from '../profile/profile.service';
export class AdminComponent implements OnInit {

public profile$: Observable<Profile | undefined>;
public deletedPost$: Observable<Post []>;


public links = [
{ label: 'Users', path: '/admin/users' },
{ label: 'Roles', path: '/admin/roles' },
];

constructor(public profileService: ProfileService) {
constructor(
public profileService: ProfileService,
private postService: PostService,
) {
this.profile$ = profileService.profile$;
this.deletedPost$ = postService.getDeletedPosts();
}

ngOnInit(): void {}

showDeletedPosts() {
return this.deletedPost$
}

}
20 changes: 18 additions & 2 deletions frontend/src/app/post.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ export class PostService {
constructor(private http: HttpClient) {}

posts: Post[] = [];
deletedPosts: Post[] = [];

getPost() {
return this.posts;
getPost(id: number) {
for (let i = 0; i < this.posts.length; i++) {
if (this.posts[i].id == id) {
return this.posts[i];
}
}
return throwError(() => new Error("Post not found."));
}


Expand All @@ -57,6 +63,16 @@ export class PostService {
);
}

getDeletedPosts(): Observable<Post[]>{
return this.http.get<Post[]>('/api/post').pipe( // need to fix routing @ warren
map((posts: Post[]) => {
// Sort the posts in descending order based on timestamp
posts.sort((a: Post, b: Post) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime());
return posts;
})
);
}

makePost(id: number, title: string, content: string, user: Profile, votes: [], timestamp: string): Observable<Post> {

if ((title.length == 0) && (content.length == 0)) {
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/app/viewforum/viewforum.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ export class viewforumComponent {
public adminPermission$: Observable<boolean>;
public post$: Observable<Post[]>;
public displayPost$: Observable<Post[]>;
public deletedPost$: Observable<Post []>
public itemsPerPage: number = 5; // set the number of items to display per page
public currentPage: number = 1;
public numPosts: number;
public numPages: number;
public postCopy: Post; // error since not being read

public static Route = {
path: 'viewforum',
Expand All @@ -29,10 +31,12 @@ export class viewforumComponent {
constructor(
private postService: PostService,
private permission: PermissionService,
private postCopy: Post, //error since not being read
) {
this.numPosts = 0; //numPosts and numPages must be set to zero initially
this.numPages = 0;
this.post$ = postService.getPosts()
this.deletedPost$ = postService.getDeletedPosts()
this.displayPost$ = this.post$.pipe( //paginating total number of posts
map((items) => {
const startIndex = (this.currentPage - 1) * this.itemsPerPage;
Expand All @@ -51,6 +55,9 @@ export class viewforumComponent {

onDelete(id: number): void {
if(confirm("Are you sure you want to delete this post?")) {
let postCopy = this.postService.getPost(id); // make a copy of the post to be deleted
// this.deletedPost$.push(postCopy); // need to add this deleted post on the array, so we can refer to it later

this.postService
.deletePost(id)
.subscribe({ // stopping here when we use the /api/posts + id route)
Expand All @@ -62,6 +69,7 @@ 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;
Expand Down