@@ -4,6 +4,8 @@ import { Router, ActivatedRoute } from '@angular/router';
44import { IPost } from '../create-post/ipost' ;
55import { Title , Meta } from '@angular/platform-browser' ;
66import { AuthService } from 'src/app/_services/auth.service' ;
7+ import { FormBuilder , Validators } from '@angular/forms' ;
8+ import { CommentService } from './comment.service' ;
79
810@Component ( {
911 selector : 'app-post' ,
@@ -15,20 +17,32 @@ export class PostComponent implements OnInit {
1517 liked = false ;
1618 likes ;
1719 auth = false ;
20+ comment_id = null ;
21+ user ;
22+ parentComms ;
23+ commentForm = this . fb . group ( {
24+ content : [ '' , Validators . required ]
25+ } ) ;
26+
27+ answerForm = this . fb . group ( {
28+ content : [ '' , Validators . required ]
29+ } ) ;
1830
1931 constructor ( private postSrv : UserPostsService , private title : Title , private meta : Meta , private router : Router ,
20- private actRoute : ActivatedRoute , private authSrv : AuthService ) { }
32+ private actRoute : ActivatedRoute , private authSrv : AuthService , private fb : FormBuilder , private commentSrv : CommentService ) { }
2133
2234 ngOnInit ( ) : void {
2335 const id = this . actRoute . snapshot . paramMap . get ( 'id' ) ;
2436 this . meta . updateTag ( { name : 'description' , content : 'Post Content' } ) ;
2537 this . postSrv . getPost ( id ) . subscribe (
2638 res => {
2739 this . post = res ;
40+ console . log ( this . post ) ;
2841 document . title = this . post . title ;
2942 if ( this . authSrv . isAuth ( ) ) {
3043 this . auth = true ;
31- if ( this . post . likes_users_ids . includes ( this . authSrv . getUser ( ) . id ) ) {
44+ this . user = this . authSrv . getUser ( ) ;
45+ if ( this . post . likes_users_ids . includes ( this . user . id ) ) {
3246 this . liked = true ;
3347 }
3448 }
@@ -52,4 +66,54 @@ export class PostComponent implements OnInit {
5266 ) ;
5367 }
5468
69+ comment ( ) {
70+ if ( this . commentForm . valid ) {
71+ this . post . comments . unshift ( {
72+ author : {
73+ username : this . authSrv . getUser ( ) . username ,
74+ } ,
75+ content : this . commentForm . controls . content . value
76+ } ) ;
77+ this . commentSrv . addCommentToPost ( this . post . id , this . commentForm . value )
78+ . subscribe (
79+ res => {
80+ console . log ( res ) ;
81+ } , err => {
82+ console . log ( err ) ;
83+ }
84+ ) ;
85+ }
86+ }
87+
88+ answerComment ( ) {
89+ const comment = ( {
90+ author : {
91+ username : this . authSrv . getUser ( ) . username ,
92+ } ,
93+ content : this . answerForm . controls . content . value
94+ } ) ;
95+ this . parentComms . unshift ( comment ) ;
96+ console . log ( this . comment_id ) ;
97+ if ( this . answerForm . valid ) {
98+ this . commentSrv . addAnswerComment ( this . comment_id , this . answerForm . value )
99+ . subscribe (
100+ res => {
101+ console . log ( res ) ;
102+ } , err => {
103+ console . log ( err ) ;
104+ }
105+ ) ;
106+ this . hideAnswer ( ) ;
107+ }
108+ }
109+
110+ showAnswer ( comments , comment ) {
111+ console . log ( comment ) ;
112+ this . parentComms = comments ;
113+ this . comment_id = comment . id ;
114+ }
115+
116+ hideAnswer ( ) {
117+ this . comment_id = null ;
118+ }
55119}
0 commit comments