-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathcomment-thread.js
More file actions
120 lines (103 loc) · 3.18 KB
/
comment-thread.js
File metadata and controls
120 lines (103 loc) · 3.18 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
117
118
119
120
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
import { task } from 'ember-concurrency';
import getWithDefault from '@fleetbase/ember-core/utils/get-with-default';
import getModelName from '@fleetbase/ember-core/utils/get-model-name';
/**
* Component to handle a thread of comments.
*/
export default class CommentThreadComponent extends Component {
/**
* Service to handle data store operations.
* @service
*/
@service store;
/**
* Service for handling notifications.
* @service
*/
@service notifications;
/**
* Service for internationalization.
* @service
*/
@service intl;
/**
* The subject related to the comments.
* @tracked
*/
@tracked subject;
/**
* Array of comments related to the subject.
* @tracked
*/
@tracked comments = [];
/**
* The text input for publishing a new comment.
* @tracked
*/
@tracked input = '';
/**
* Context object containing utility functions.
*/
context = {
isCommentInvalid: this.isCommentInvalid.bind(this),
reloadComments: () => {
return this.reloadComments.perform();
},
};
/**
* Constructor for the comment thread component.
* @param owner - The owner of the component.
* @param subject - The subject of the comment thread.
* @param subjectType - The type of the subject.
*/
constructor(owner, { subject, subjectType }) {
super(...arguments);
this.subject = subject;
this.comments = getWithDefault(subject, 'comments', []);
this.subjectType = subjectType ? subjectType : getModelName(subject);
}
/**
* Asynchronous task to publish a new comment.
* @task
*/
@task *publishComment() {
if (this.isCommentInvalid(this.input)) {
return;
}
let comment = this.store.createRecord('comment', {
content: this.input,
subject_uuid: this.subject.id,
subject_type: this.subjectType,
});
yield comment.save();
yield this.reloadComments.perform();
this.input = '';
}
/**
* Asynchronous task to reload the comments related to the subject.
* @task
*/
@task *reloadComments() {
this.comments = yield this.store.query('comment', { subject_uuid: this.subject.id, withoutParent: 1, sort: '-created_at' });
}
/**
* Checks if a comment is invalid.
* @param {string} comment - The comment to validate.
* @returns {boolean} True if the comment is invalid, false otherwise.
*/
isCommentInvalid(comment) {
if (!comment) {
this.notifications.warning(this.intl.t('component.comment-thread.comment-input-empty-notification'));
return true;
}
// make sure comment is at least 2 characters
if (typeof comment === 'string' && comment.length <= 1) {
this.notifications.warning(this.intl.t('component.comment-thread.comment-min-length-notification'));
return true;
}
return false;
}
}