From 4c32c5cd4236bbe783e3bdf0e6bd406fb69d298c Mon Sep 17 00:00:00 2001 From: Syndamia Date: Mon, 1 Feb 2021 14:48:29 +0200 Subject: Implemented creation of comments in post page --- .../components/post-page/post-page.component.html | 8 +++-- .../components/post-page/post-page.component.ts | 37 +++++++++++++++++++--- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/DevHive.Angular/src/app/components/post-page/post-page.component.html b/src/DevHive.Angular/src/app/components/post-page/post-page.component.html index 7069eae..32326b9 100644 --- a/src/DevHive.Angular/src/app/components/post-page/post-page.component.html +++ b/src/DevHive.Angular/src/app/components/post-page/post-page.component.html @@ -7,8 +7,12 @@ -
- + + + +
+
+
diff --git a/src/DevHive.Angular/src/app/components/post-page/post-page.component.ts b/src/DevHive.Angular/src/app/components/post-page/post-page.component.ts index 830e33d..983e550 100644 --- a/src/DevHive.Angular/src/app/components/post-page/post-page.component.ts +++ b/src/DevHive.Angular/src/app/components/post-page/post-page.component.ts @@ -3,6 +3,7 @@ import { Component, OnInit } from '@angular/core'; import { FormBuilder, FormControl, FormGroup } from '@angular/forms'; import { Router } from '@angular/router'; import { Guid } from 'guid-typescript'; +import {CommentService} from 'src/app/services/comment.service'; import { PostService } from 'src/app/services/post.service'; import { TokenService } from 'src/app/services/token.service'; import { Post } from 'src/models/post'; @@ -18,8 +19,9 @@ export class PostPageComponent implements OnInit { public postId: Guid; public post: Post; public editPostFormGroup: FormGroup; + public addCommentFormGroup: FormGroup; - constructor(private _router: Router, private _fb: FormBuilder, private _tokenService: TokenService, private _postService: PostService) + constructor(private _router: Router, private _fb: FormBuilder, private _tokenService: TokenService, private _postService: PostService, private _commentService: CommentService) { } ngOnInit(): void { @@ -43,6 +45,10 @@ export class PostPageComponent implements OnInit { this.editPostFormGroup = this._fb.group({ newPostMessage: new FormControl('') }); + + this.addCommentFormGroup = this._fb.group({ + newComment: new FormControl('') + }); } backToFeed(): void { @@ -50,15 +56,17 @@ export class PostPageComponent implements OnInit { } editPost(): void { + if (this._tokenService.getTokenFromSessionStorage() === '') { + this._router.navigate(['/login']); + return; + } + if (this.editingPost) { const newMessage = this.editPostFormGroup.get('newPostMessage')?.value; if (newMessage !== '') { this._postService.putPostWithSessionStorageRequest(this.postId, newMessage).subscribe( (result: object) => { - // Reload the page - this._router.routeReuseStrategy.shouldReuseRoute = () => false; - this._router.onSameUrlNavigation = 'reload'; - this._router.navigate([this._router.url]); + this.reloadPage(); } ); } @@ -66,6 +74,19 @@ export class PostPageComponent implements OnInit { this.editingPost = !this.editingPost; } + addComment(): void { + const newComment = this.addCommentFormGroup.get('newComment')?.value; + if (newComment !== '' && newComment !== null) { + console.log(newComment); + this._commentService.createCommentWithSessionStorageRequest(this.postId, newComment).subscribe( + (result: object) => { + this.editPostFormGroup.reset(); + this.reloadPage(); + } + ); + } + } + deletePost(): void { this._postService.deletePostWithSessionStorage(this.postId).subscribe( (result: object) => { @@ -73,4 +94,10 @@ export class PostPageComponent implements OnInit { } ); } + + private reloadPage(): void { + this._router.routeReuseStrategy.shouldReuseRoute = () => false; + this._router.onSameUrlNavigation = 'reload'; + this._router.navigate([this._router.url]); + } } -- cgit v1.2.3