Website Summary Files Commits Branches @commit @diff @stats
path: root / src / DevHive.Angular / src / app / components / post-page / post-page.component.ts
blob: 830e33d6d14e86f3a221c90aadedcd77db9b82d5 (plain) (blame)
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
import { HttpErrorResponse } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
import { Router } from '@angular/router';
import { Guid } from 'guid-typescript';
import { PostService } from 'src/app/services/post.service';
import { TokenService } from 'src/app/services/token.service';
import { Post } from 'src/models/post';

@Component({
  selector: 'app-post-page',
  templateUrl: './post-page.component.html',
  styleUrls: ['./post-page.component.css']
})
export class PostPageComponent implements OnInit {
  public editable = false;
  public editingPost = false;
  public postId: Guid;
  public post: Post;
  public editPostFormGroup: FormGroup;

  constructor(private _router: Router, private _fb: FormBuilder, private _tokenService: TokenService, private _postService: PostService)
  { }

  ngOnInit(): void {
    this.postId = Guid.parse(this._router.url.substring(6));

    // Gets the post and the logged in user and compares them,
    // to determine if the current post is made by the user
    this._postService.getPostRequest(this.postId).subscribe(
      (result: object) => {
        this.post = result as Post;
        this.post.comments = this.post.comments.sort((x, y) => {
          return Date.parse(y.timeCreated.toString()) - Date.parse(x.timeCreated.toString());
        });
        this.editable = this.post.creatorUsername === this._tokenService.getUsernameFromSessionStorageToken();
      },
      (err: HttpErrorResponse) => {
        this._router.navigate(['/not-found']);
      }
    );

    this.editPostFormGroup = this._fb.group({
      newPostMessage: new FormControl('')
    });
  }

  backToFeed(): void {
    this._router.navigate(['/']);
  }

  editPost(): void {
    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.editingPost = !this.editingPost;
  }

  deletePost(): void {
    this._postService.deletePostWithSessionStorage(this.postId).subscribe(
      (result: object) => {
        this._router.navigate(['/profile/' + this._tokenService.getUsernameFromSessionStorageToken()]);
      }
    );
  }
}