aboutsummaryrefslogtreecommitdiff
path: root/src/app/components/post/post.component.ts
diff options
context:
space:
mode:
authorSyndamia <kamen.d.mladenov@protonmail.com>2021-03-21 17:56:27 +0200
committerSyndamia <kamen.d.mladenov@protonmail.com>2021-03-21 17:56:27 +0200
commitbed88a28aa555e5ed3e8c08422935eb11b882fcf (patch)
treea8069859ad95e2ac0809c57f9014ea05a5d72b35 /src/app/components/post/post.component.ts
parent0f945d891a565ec1ed4aac8e4feb0b0b41746567 (diff)
downloadDevHive-Angular-bed88a28aa555e5ed3e8c08422935eb11b882fcf.tar
DevHive-Angular-bed88a28aa555e5ed3e8c08422935eb11b882fcf.tar.gz
DevHive-Angular-bed88a28aa555e5ed3e8c08422935eb11b882fcf.zip
Fixed rating highlighting not being properly executed upon loaded post component HTML
Diffstat (limited to 'src/app/components/post/post.component.ts')
-rw-r--r--src/app/components/post/post.component.ts39
1 files changed, 17 insertions, 22 deletions
diff --git a/src/app/components/post/post.component.ts b/src/app/components/post/post.component.ts
index 5c79658..000fde0 100644
--- a/src/app/components/post/post.component.ts
+++ b/src/app/components/post/post.component.ts
@@ -1,4 +1,4 @@
-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';
@@ -15,7 +15,7 @@ import { TokenService } from '../../services/token.service';
templateUrl: './post.component.html',
styleUrls: ['./post.component.css'],
})
-export class PostComponent implements OnInit {
+export class PostComponent implements OnInit, AfterViewInit {
public loaded = false;
public user: User;
public post: Post;
@@ -23,15 +23,15 @@ export class PostComponent implements OnInit {
public timeCreated: string;
@Input() paramId: string;
@Input() index: number;
+ @ViewChild('upvote') upvoteBtn: ElementRef;
+ @ViewChild('downvote') downvoteBtn: ElementRef;
public loggedIn = false;
public loggedInAuthor = false;
public editingPost = false;
public files: File[];
public editPostFormGroup: FormGroup;
- private upvoteBtns: HTMLCollectionOf<HTMLElement>;
- private downvoteBtns: HTMLCollectionOf<HTMLElement>;
- constructor(private _postService: PostService, private _ratingServe: RatingService, private _userService: UserService, private _router: Router, private _tokenService: TokenService, private _cloudinaryService: CloudinaryService, private _fb: FormBuilder)
+ constructor(private _postService: PostService, private _ratingServe: RatingService, private _userService: UserService, private _router: Router, private _tokenService: TokenService, private _cloudinaryService: CloudinaryService, private _fb: FormBuilder, private _elem: ElementRef, private _renderer: Renderer2)
{ }
ngOnInit(): void {
@@ -48,9 +48,6 @@ export class PostComponent implements OnInit {
this.post.fileURLs = Object.values(result)[7];
this.votesNumber = this.post.currentRating;
- this.upvoteBtns = document.getElementsByClassName('upvote') as HTMLCollectionOf<HTMLElement>;
- this.downvoteBtns = document.getElementsByClassName('downvote') as HTMLCollectionOf<HTMLElement>;
-
this.timeCreated = new Date(this.post.timeCreated).toLocaleString('en-GB');
this.loadUser();
@@ -69,8 +66,6 @@ export class PostComponent implements OnInit {
Object.assign(this.user, result);
if (this.loggedIn) {
- this.highlightButtonsOnInit();
-
this.loggedInAuthor = this._tokenService.getUsernameFromSessionStorageToken() === this.post.creatorUsername;
this.editPostFormGroup.get('newPostMessage')?.setValue(this.post.message);
@@ -105,6 +100,16 @@ export class PostComponent implements OnInit {
}
}
+ ngAfterViewInit(): void {
+ this._ratingServe.getRatingByUserAndPostWithSessionStorageRequest(Guid.parse(this.paramId)).subscribe({
+ next: (x: object) => {
+ const isLike: boolean = Object.values(x)[3];
+
+ this.changeColorOfVoteButton(isLike, !isLike);
+ }
+ });
+ }
+
goToAuthorProfile(): void {
this._router.navigate(['/profile/' + this.user.userName]);
}
@@ -206,17 +211,7 @@ export class PostComponent implements OnInit {
}
private changeColorOfVoteButton(isUpvoted: boolean, isDownvoted: boolean): void {
- this.upvoteBtns.item(this.index)!.style.backgroundColor = (isUpvoted) ? 'var(--upvote-highlight)' : 'inherit';
- this.downvoteBtns.item(this.index)!.style.backgroundColor = (isDownvoted) ? 'var(--downvote-highlight)' : 'inherit';
- }
-
- private highlightButtonsOnInit(): void {
- this._ratingServe.getRatingByUserAndPostWithSessionStorageRequest(Guid.parse(this.paramId)).subscribe(
- (x: object) => {
- const isLike: boolean = Object.values(x)[3];
-
- this.changeColorOfVoteButton(isLike, !isLike);
- }
- );
+ this._renderer.setStyle(this.upvoteBtn.nativeElement, 'backgroundColor', (isUpvoted) ? 'var(--upvote-highlight)' : 'inherit');
+ this._renderer.setStyle(this.downvoteBtn.nativeElement, 'backgroundColor', (isDownvoted) ? 'var(--downvote-highlight)' : 'inherit');
}
}