Skip to content

Commit 0bd4aa3

Browse files
committed
Add comments to post
1 parent 8ed3966 commit 0bd4aa3

File tree

7 files changed

+182
-12
lines changed

7 files changed

+182
-12
lines changed

src/app/admin/posts/create-post/ipost.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export interface IPost {
77
image: string;
88
public: boolean;
99
likes_count?: number;
10-
likes_users_ids: string[];
10+
likes_users_ids?: string[];
11+
comments: any[];
1112
created_at: Date;
1213
updated_at: Date;
1314
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { TestBed } from '@angular/core/testing';
2+
3+
import { CommentService } from './comment.service';
4+
5+
describe('CommentService', () => {
6+
let service: CommentService;
7+
8+
beforeEach(() => {
9+
TestBed.configureTestingModule({});
10+
service = TestBed.inject(CommentService);
11+
});
12+
13+
it('should be created', () => {
14+
expect(service).toBeTruthy();
15+
});
16+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Injectable } from '@angular/core';
2+
import { environment } from 'src/environments/environment';
3+
import { HttpClient } from '@angular/common/http';
4+
5+
@Injectable({
6+
providedIn: 'root'
7+
})
8+
export class CommentService {
9+
url = environment.apiUrl + '/';
10+
11+
constructor(private http: HttpClient) { }
12+
13+
addCommentToPost(post_id, data) {
14+
return this.http.post<any>(this.url + 'posts/' + post_id + '/add_comment', data);
15+
}
16+
17+
addAnswerComment(comment_id, data) {
18+
return this.http.post<any>(this.url + 'comments/' + comment_id + '/createAnswer', data);
19+
}
20+
}
Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div class="main_box">
22
<div class="border_image">
3-
<img src="{{ post.image ? post.image : '../../assets/images/env.jpeg' }}">
3+
<img src="{{ post?.image ? post.image : '../../assets/images/env.jpeg' }}">
44
</div>
55
<div class="title">
66
<b>{{ post?.title }}</b>
@@ -10,10 +10,51 @@
1010
</div>
1111
<div class="info">
1212
<p>*User Profile Picture Link*</p>
13-
<p>{{post?.updated_at.toString().split('T')[0]}}</p>
13+
<p>{{ post?.updated_at.toString().split('T')[0] }}</p>
1414
</div>
1515
<div *ngIf="auth" class="actions">
16-
<button class="btn" [class.liked]="liked" (click)="handleLike()"><span class="fas fa-tree"></span> Like {{ likes }}</button>
17-
<button class="btn comment" routerLink="/post/{{post.id}}" fragment="comments">Comment</button>
16+
<button class="btn" [class.liked]="liked" (click)="handleLike()"><span class="fas fa-tree"></span> Like
17+
{{ likes }}</button>
18+
<div id="comments" class="comments">
19+
<h3>Comments</h3>
20+
<form [formGroup]="commentForm" (submit)="comment()">
21+
<textarea formControlName="content"></textarea>
22+
<button class="btn btn-primary">Add Comment</button>
23+
</form>
24+
<div *ngIf="comment_id" class="answerForm">
25+
<form [formGroup]="answerForm" (submit)="answerComment()">
26+
<textarea formControlName="content"></textarea>
27+
<button class="btn btn-primary">Add Comment</button>
28+
<button type="button" class="btn btn-secondary" (click)="hideAnswer()">Cancel</button>
29+
</form>
30+
</div>
31+
<div *ngFor="let comment of post?.comments">
32+
<div class="comment">
33+
<p>{{ comment.author.username }} wrote:</p>
34+
<p class="content">{{ comment.content }}</p>
35+
<button class="btn btn-primary" (click)="showAnswer(comment.comments, comment)">Answer to comment</button>
36+
<button *ngIf="comment.author.username == user.username" class="btn btn-secondary">Edit comment</button>
37+
</div>
38+
<div *ngFor="let subComment of comment.comments">
39+
<div class="comment">
40+
<p>{{ subComment.author.username }} wrote:</p>
41+
<p class="content">{{ subComment.content }}</p>
42+
<button class="btn btn-primary" (click)="showAnswer(subComment.comments, subComment)">Answer to
43+
comment</button>
44+
<button *ngIf="subComment.author.username == user.username" class="btn btn-secondary">Edit comment</button>
45+
</div>
46+
<div class="comment" *ngFor="let subSubComment of subComment.comments">
47+
<div class="comment">
48+
<p>{{ subSubComment.author.username }} wrote:</p>
49+
<p class="content">{{ subSubComment.content }}</p>
50+
<button class="btn btn-primary" (click)="showAnswer(subComment.comments, subSubComment)">Answer to
51+
comment</button>
52+
<button *ngIf="subSubComment.author.username == user.username" class="btn btn-secondary">Edit
53+
comment</button>
54+
</div>
55+
</div>
56+
</div>
57+
</div>
58+
</div>
1859
</div>
1960
</div>

src/app/admin/posts/post/post.component.scss

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
align-items: center;
1717
}
1818

19-
.border_image{
19+
.border_image {
2020
margin-bottom: 10px;
2121
width: 100%;
2222
}
23-
.border_image img{
23+
24+
.border_image img {
2425
object-fit: cover;
2526
height: 450px;
2627
width: 100%;
@@ -62,8 +63,35 @@
6263
color: var(--color-primary) !important;
6364
}
6465

66+
.actions {
67+
width: 100%;
68+
display: flex;
69+
flex-wrap: wrap;
70+
justify-content: center;
71+
}
72+
73+
.comments {
74+
width: 100%;
75+
display: flex;
76+
flex-direction: column;
77+
align-items: center;
78+
}
79+
80+
.answerForm {
81+
z-index: 10;
82+
position: fixed;
83+
top: 0;
84+
left: 0;
85+
width: 100%;
86+
height: 100%;
87+
background-color: rgba(0,0,0,0.5);
88+
display: flex;
89+
align-items: center;
90+
justify-content: center;
91+
}
92+
6593
@media screen and (max-width: 767px) {
66-
.border_image img{
94+
.border_image img {
6795
height: auto;
6896
}
69-
}
97+
}

src/app/admin/posts/post/post.component.ts

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { Router, ActivatedRoute } from '@angular/router';
44
import { IPost } from '../create-post/ipost';
55
import { Title, Meta } from '@angular/platform-browser';
66
import { 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
}

src/app/single-post/single-post.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ <h3>{{ post.title }}</h3>
88
</a>
99
<div *ngIf="auth" class="actions">
1010
<button class="btn" [class.liked]="liked" (click)="handleLike()"><span class="fas fa-tree"></span> Like {{likes}}</button>
11-
<button class="btn comment" routerLink="/post/{{post.id}}" fragment="comments">Comment</button>
11+
<button class="btn comment" routerLink="/post/{{post.id}}" fragment="comments">Comments</button>
1212
</div>
1313
</div>
1414
</div>

0 commit comments

Comments
 (0)