-
Notifications
You must be signed in to change notification settings - Fork 18
/
CreateComment.js
75 lines (68 loc) · 1.43 KB
/
CreateComment.js
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
import Relay from 'react-relay';
import showdown from 'showdown';
import moment from 'moment';
const converter = new showdown.Converter();
/* global App */
export default class extends Relay.Mutation {
getMutation() {
return Relay.QL`mutation{CreateComment}`;
}
getFatQuery() {
return Relay.QL`
fragment on CreateCommentPayload {
commentEdge,
post {
comments_count
}
}
`;
}
getConfigs() {
return [
{
type: 'FIELDS_CHANGE',
fieldIDs: {
post: this.props.post.id,
},
},
{
type: 'RANGE_ADD',
parentName: 'post',
parentID: this.props.post.id,
connectionName: 'comments',
edgeName: 'commentEdge',
rangeBehaviors: {
'': 'append',
'order(-id)': 'prepend',
},
},
];
}
getVariables() {
return {
post_id: this.props.post.id,
body: this.props.body,
};
}
getOptimisticResponse() {
const { post, body } = this.props;
return {
post: {
id: post.id,
comments_count: post.comments_count + 1,
},
commentEdge: {
node: {
body: converter.makeHtml(body),
is_owner: true,
voted: false,
votes_count: 0,
created_at: moment().utc().valueOf(),
user: {
name: App.currentUser().name,
},
},
},
};
}
}