1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
import { Title } from '@angular/platform-browser';
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 { Comment } from 'src/models/comment.model';
@Component({
selector: 'app-comment-page',
templateUrl: './comment-page.component.html',
styleUrls: ['./comment-page.component.css']
})
export class CommentPageComponent implements OnInit {
private _title = 'Comment';
public loaded = false;
public loggedIn = false;
public editable = false;
public editingComment = false;
public commentId: Guid;
public comment: Comment;
public editCommentFormGroup: FormGroup;
constructor(private _titleService: Title, private _router: Router, private _fb: FormBuilder, private _tokenService: TokenService, private _commentService: CommentService){
this._titleService.setTitle(this._title);
}
ngOnInit(): void {
this.loggedIn = this._tokenService.getTokenFromSessionStorage() !== '';
this.commentId = Guid.parse(this._router.url.substring(9));
// Gets the post and the logged in user and compares them,
// to determine if the current post is made by the user
this._commentService.getCommentRequest(this.commentId).subscribe({
next: (result: object) => {
this.comment = result as Comment;
if (this.loggedIn) {
this.editable = this.comment.issuerUsername === this._tokenService.getUsernameFromSessionStorageToken();
this.editCommentFormGroup.get('newCommentMessage')?.setValue(this.comment.message);
}
this.loaded = true;
},
error: () => {
this._router.navigate(['/not-found']);
}
});
this.editCommentFormGroup = this._fb.group({
newCommentMessage: new FormControl('')
});
}
toPost(): void {
this._router.navigate(['/post/' + this.comment.postId]);
}
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) {
console.log(this.commentId);
this._commentService.putCommentWithSessionStorageRequest(this.commentId, this.comment.postId, newMessage).subscribe({
next: () => {
this.reloadPage();
}
});
}
}
this.editingComment = !this.editingComment;
}
deleteComment(): void {
this._commentService.deleteCommentWithSessionStorage(this.commentId).subscribe({
next: () => {
this.toPost();
}
});
}
private reloadPage(): void {
this._router.routeReuseStrategy.shouldReuseRoute = () => false;
this._router.onSameUrlNavigation = 'reload';
this._router.navigate([this._router.url]);
}
}
|