From f9436e7780b50ac66c3bc484021fdeeb5afcc7cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Victor?= Date: Mon, 6 Nov 2023 10:45:20 -0300 Subject: [PATCH] Adiciona comentarios na tela do video --- .../video-comment.component.html | 45 ++++++++--- .../video-comment/video-comment.component.ts | 75 ++++++++++++++++--- src/app/services/video-comment.service.ts | 16 +++- 3 files changed, 112 insertions(+), 24 deletions(-) diff --git a/src/app/pages/video-viewer/video-comment/video-comment.component.html b/src/app/pages/video-viewer/video-comment/video-comment.component.html index 804c4815..52c9059a 100644 --- a/src/app/pages/video-viewer/video-comment/video-comment.component.html +++ b/src/app/pages/video-viewer/video-comment/video-comment.component.html @@ -1,11 +1,36 @@
- -
- - - - - -
- -
\ No newline at end of file +
+
+ + +
+
+
+

Faça login para comentar

+
+
+
+
+ {{ comment.user_name }} +
+
+

{{ comment.content }}

+
+
+

{{ comment.created_at }}

+
+
+
+ diff --git a/src/app/pages/video-viewer/video-comment/video-comment.component.ts b/src/app/pages/video-viewer/video-comment/video-comment.component.ts index b3738a68..e91d7238 100644 --- a/src/app/pages/video-viewer/video-comment/video-comment.component.ts +++ b/src/app/pages/video-viewer/video-comment/video-comment.component.ts @@ -1,8 +1,11 @@ import { Component, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; -import { VideoCommentService }from '../../../services/video-comment.service'; +import { VideoCommentService } from '../../../services/video-comment.service'; import { ActivatedRoute } from '@angular/router'; -import { HttpResponse } from '@angular/common/http'; +import jwt_decode from 'jwt-decode'; +import { AuthService } from '../../../services/auth.service'; +import { Comment } from '../../../../shared/model/comment.model'; +import { UserService } from 'src/app/services/user.service'; @Component({ selector: 'app-video-comment', @@ -13,39 +16,89 @@ export class VideoCommentComponent implements OnInit { commentForm!: FormGroup; comment: string = ''; video_id: number = 0; + userId: any; + comments: Comment[] = []; + userName: string = ''; + constructor( private fb: FormBuilder, private vcs: VideoCommentService, - private route: ActivatedRoute - ) {} + private route: ActivatedRoute, + private authService: AuthService, + private userService: UserService + ) { } ngOnInit(): void { this.commentForm = this.fb.group({ - comment: ['', [Validators.required,Validators.maxLength(300)]], - }, + comment: ['', [Validators.required, Validators.maxLength(300)]], + }, ); - this.route.params.subscribe((params)=> { + this.route.params.subscribe((params) => { this.video_id = params['idVideo']; }) + if (this.authService.isAuthenticated()) { + this.setUserIdFromToken(localStorage.getItem('token') as string); + } else { + this.userId = 0; + } + this.vcs.getComments(this.video_id).subscribe({ + next: (data) => { + this.comments = data.body as Comment[]; + this.comments.forEach((comment) => { + this.vcs.getUser(comment.user_id as number).subscribe({ + next: (data) => { + console.log(data); + comment.user_name = data.body.name; + }, + error: (error) => { + console.log(error) + } + }); + }); + console.log(this.comments); + }, + error: (error) => { + console.log(error) + } + }); + } + + setUserIdFromToken(token: string) { + const decodedToken: any = jwt_decode(token); + this.userId = decodedToken.id; + // console.log(this.userId); + } + + getUserName(userId: number) { + this.userService.getUser(userId).subscribe({ + next: (data) => { + return data.name; + }, + error: (error) => { + console.log(error) + } + }); + } + addComment() { if (this.commentForm.valid) { this.comment = this.commentForm.value.comment; this.vcs.postComment({ - user_id: 123, + user_id: this.userId, video_id: this.video_id, content: this.comment }).subscribe({ - next: (data)=> { + next: (data) => { console.log(data); }, - error: (error)=> { + error: (error) => { console.log(error) } }); } else { - alert("Comentário não pode ser vazio"); + alert("Comentário não pode ser vazio"); } } } diff --git a/src/app/services/video-comment.service.ts b/src/app/services/video-comment.service.ts index d73b1f76..d915d0d0 100644 --- a/src/app/services/video-comment.service.ts +++ b/src/app/services/video-comment.service.ts @@ -1,7 +1,10 @@ import { Injectable } from '@angular/core'; import { environment } from '../environment/environment'; -import { HttpClient } from '@angular/common/http'; +import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http'; import { Observable } from 'rxjs'; +import { IComment } from '../../shared/model/comment.model'; + +type CommentResponseType = HttpResponse; @Injectable({ providedIn: 'root' @@ -9,11 +12,18 @@ import { Observable } from 'rxjs'; export class VideoCommentService { private apiURLVideo = environment.apiURLVideo; + private apiURLUsers = environment.apiURL; constructor(private http: HttpClient) { } - getComments(video_id: any): Observable { - return this.http.get(`${this.apiURLVideo}/comments/${video_id}`); + getUser(id: any): Observable { + const token = localStorage.getItem('token'); + let headers = new HttpHeaders({ Authorization: `Bearer ${token}` }); + return this.http.get(`${this.apiURLUsers}/users/${id}`, { headers: headers, observe: 'response' }); + } + + getComments(video_id: any): Observable { + return this.http.get(`${this.apiURLVideo}/comments/${video_id}`, { observe: 'response' }); } postComment(body: any): Observable {