Repository navigation
Expand file tree
/
Copy patharticle.component.ts
More file actions
116 lines (99 loc) · 2.63 KB
/
Copy patharticle.component.ts
File metadata and controls
116 lines (99 loc) · 2.63 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import {
Article,
ArticlesService,
Comment,
CommentsService,
User,
UserService
} from '../core';
@Component({
selector: 'app-article-page',
templateUrl: './article.component.html'
})
export class ArticleComponent implements OnInit {
article: Article;
currentUser: User;
canModify: boolean;
comments: Comment[];
commentControl = new FormControl();
commentFormErrors = {};
isSubmitting = false;
isDeleting = false;
constructor(
private route: ActivatedRoute,
private articlesService: ArticlesService,
private commentsService: CommentsService,
private router: Router,
private userService: UserService,
) { }
ngOnInit() {
// Retreive the prefetched article
this.route.data.subscribe(
(data: { article: Article }) => {
this.article = data.article;
// Load the comments on this article
this.populateComments();
}
);
// Load the current user's data
this.userService.currentUser.subscribe(
(userData: User) => {
this.currentUser = userData;
this.canModify = (this.currentUser.username === this.article.author.username);
}
);
}
onToggleFavorite(favorited: boolean) {
this.article.favorited = favorited;
if (favorited) {
this.article.favoritesCount++;
} else {
this.article.favoritesCount--;
}
}
onToggleFollowing(following: boolean) {
this.article.author.following = following;
}
deleteArticle() {
this.isDeleting = true;
this.articlesService.destroy(this.article.slug)
.subscribe(
success => {
this.router.navigateByUrl('/');
}
);
}
populateComments() {
this.commentsService.getAll(this.article.slug)
.subscribe(comments => this.comments = comments);
}
addComment() {
this.isSubmitting = true;
this.commentFormErrors = {};
const commentBody = this.commentControl.value;
this.commentsService
.add(this.article.slug, commentBody)
.subscribe(
comment => {
this.comments.unshift(comment);
this.commentControl.reset('');
this.isSubmitting = false;
},
errors => {
this.isSubmitting = false;
this.commentFormErrors = errors;
}
);
}
onDeleteComment(comment) {
this.commentsService.destroy(comment.id, this.article.slug)
.subscribe(
success => {
this.comments = this.comments.filter((item) => item !== comment);
}
);
}
}