diff options
| author | Kamen Mladenov <kamen.d.mladenov@protonmail.com> | 2021-03-22 20:52:19 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-22 20:52:19 +0200 |
| commit | 0cbe73007b9112bf7aa76e2584cb1fafc272dd5b (patch) | |
| tree | 69b9916c17b6675a1e33b1a180d5eb0615e4fb87 /src/app/components/comment/comment.component.ts | |
| parent | 084238dfa39f31b3661362cbe8cdea44e8f05992 (diff) | |
| parent | cad20fc8c7d58860d7bf9c803da3a8fcea43396a (diff) | |
| download | DevHive-Angular-0cbe73007b9112bf7aa76e2584cb1fafc272dd5b.tar DevHive-Angular-0cbe73007b9112bf7aa76e2584cb1fafc272dd5b.tar.gz DevHive-Angular-0cbe73007b9112bf7aa76e2584cb1fafc272dd5b.zip | |
Merge pull request #3 from Team-Kaleidoscope/major-redesign
Major redesign
Diffstat (limited to 'src/app/components/comment/comment.component.ts')
| -rw-r--r-- | src/app/components/comment/comment.component.ts | 82 |
1 files changed, 77 insertions, 5 deletions
diff --git a/src/app/components/comment/comment.component.ts b/src/app/components/comment/comment.component.ts index 7da896d..2a54f92 100644 --- a/src/app/components/comment/comment.component.ts +++ b/src/app/components/comment/comment.component.ts @@ -1,7 +1,9 @@ -import { Component, Input, OnInit } from '@angular/core'; +import { AfterViewInit, Component, ElementRef, Input, OnInit, Renderer2, ViewChild } 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 { TokenService } from 'src/app/services/token.service'; import { UserService } from 'src/app/services/user.service'; import { Comment } from 'src/models/comment.model'; import { User } from 'src/models/identity/user.model'; @@ -11,14 +13,20 @@ import { User } from 'src/models/identity/user.model'; templateUrl: './comment.component.html', styleUrls: ['./comment.component.css'] }) -export class CommentComponent implements OnInit { +export class CommentComponent implements OnInit, AfterViewInit { public loaded = false; + public loggedInAuthor = false; + public editingComment = false; public user: User; public comment: Comment; public timeCreated: string; @Input() paramId: string; + public editCommentFormGroup: FormGroup; + @ViewChild('share') shareBtn: ElementRef; + private _defaultShareBtnInnerHTML: string; + private _linkShared = false; - constructor(private _router: Router, private _commentService: CommentService, private _userService: UserService) + constructor(private _router: Router, private _commentService: CommentService, private _userService: UserService, private _tokenService: TokenService, private _fb: FormBuilder, private _elem: ElementRef, private _renderer: Renderer2) { } ngOnInit(): void { @@ -33,22 +41,86 @@ export class CommentComponent implements OnInit { this.loadUser(); } }); + + this.editCommentFormGroup = this._fb.group({ + newCommentMessage: new FormControl('') + }); } private loadUser(): void { this._userService.getUserByUsernameRequest(this.comment.issuerUsername).subscribe({ next: (result: object) => { Object.assign(this.user, result); + + if (this._tokenService.getTokenFromSessionStorage() !== '') { + this.loggedInAuthor = this._tokenService.getUsernameFromSessionStorageToken() === this.comment.issuerUsername; + this.editCommentFormGroup.get('newCommentMessage')?.setValue(this.comment.message); + } + this.loaded = true; } }); } + ngAfterViewInit(): void { + this._defaultShareBtnInnerHTML = this.shareBtn.nativeElement.innerHTML; + } + goToAuthorProfile(): void { this._router.navigate(['/profile/' + this.comment.issuerUsername]); } - goToCommentPage(): void { - this._router.navigate(['/comment/' + this.comment.commentId]); + toggleEditing(): void { + this.editingComment = !this.editingComment; + } + + editComment(): void { + if (this._tokenService.getTokenFromSessionStorage() === '') { + this._router.navigate(['/login']); + return; + } + + if (this.editingComment) { + const newMessage = this.editCommentFormGroup.get('newCommentMessage')?.value; + + if (newMessage !== '' && newMessage !== this.comment.message) { + this._commentService.putCommentWithSessionStorageRequest(Guid.parse(this.paramId), this.comment.postId, newMessage).subscribe({ + next: () => { + this.reloadPage(); + } + }); + } + } + this.editingComment = !this.editingComment; + } + + deleteComment(): void { + this._commentService.deleteCommentWithSessionStorage(Guid.parse(this.paramId)).subscribe({ + next: () => { + this.reloadPage(); + } + }); + } + + private reloadPage(): void { + this._router.routeReuseStrategy.shouldReuseRoute = () => false; + this._router.onSameUrlNavigation = 'reload'; + this._router.navigate([this._router.url]); + } + + resetShareBtn(): void { + if (this._linkShared) { + this._renderer.setProperty(this.shareBtn.nativeElement, 'innerHTML', this._defaultShareBtnInnerHTML); + this._linkShared = false; + } + } + + showCopiedMessage(): void { + this._renderer.setProperty(this.shareBtn.nativeElement, 'innerHTML', 'Link copied to clipboard!'); + this._linkShared = true; + } + + getPostLink(): string { + return location.origin + '/comment/' + this.paramId; } } |
