diff options
| author | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-02-01 14:48:29 +0200 |
|---|---|---|
| committer | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-02-01 14:48:29 +0200 |
| commit | 4c32c5cd4236bbe783e3bdf0e6bd406fb69d298c (patch) | |
| tree | 45dc28858064e1aa4f37fd58f5c3227c3401a0d0 /src | |
| parent | 2b97669cacf222d03697607984b34f7b85ddc363 (diff) | |
| download | DevHive-4c32c5cd4236bbe783e3bdf0e6bd406fb69d298c.tar DevHive-4c32c5cd4236bbe783e3bdf0e6bd406fb69d298c.tar.gz DevHive-4c32c5cd4236bbe783e3bdf0e6bd406fb69d298c.zip | |
Implemented creation of comments in post page
Diffstat (limited to 'src')
| -rw-r--r-- | src/DevHive.Angular/src/app/components/post-page/post-page.component.html | 8 | ||||
| -rw-r--r-- | src/DevHive.Angular/src/app/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 @@ <button class="submit-btn" (click)="editPost()">Edit post</button> <button class="submit-btn delete-btn" (click)="deletePost()">Delete post</button> </div> - <form *ngIf="editingPost" [formGroup]="editPostFormGroup" (ngSubmit)="editPost()"> - <input type="text" placeholder="New post message" class="input-field" formControlName="newPostMessage"> + <form [formGroup]="editPostFormGroup" (ngSubmit)="editPost()"> + <input type="text" *ngIf="editingPost" placeholder="New post message" class="input-field" formControlName="newPostMessage"> + <input type="submit" style="display: none" /> + </form> + <form [formGroup]="addCommentFormGroup" (ngSubmit)="addComment()"> + <input type="text" placeholder="Add comment" class="input-field" formControlName="newComment"> <input type="submit" style="display: none" /> </form> <div> 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]); + } } |
