Skip to content

Commit ec6d2ce

Browse files
committed
Merge new comment into comments list
1 parent f408638 commit ec6d2ce

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/Comment/CommentAdd/index.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,62 @@
11
import React, { Component } from 'react';
22
import { Mutation } from 'react-apollo';
33

4+
import { GET_COMMENTS_OF_ISSUE } from '../CommentList/queries';
45
import { ADD_COMMENT } from './mutations';
56

67
import TextArea from '../../TextArea';
78
import Button from '../../Button';
89
import ErrorMessage from '../../Error';
910

11+
const updateComments = ({
12+
repositoryOwner,
13+
repositoryName,
14+
issue
15+
},
16+
client,
17+
{
18+
data: {
19+
addComment: {
20+
commentEdge
21+
}
22+
}
23+
}
24+
) => {
25+
const data = client.readQuery({
26+
query: GET_COMMENTS_OF_ISSUE,
27+
variables: {
28+
repositoryOwner,
29+
repositoryName,
30+
number: issue.number
31+
}
32+
});
33+
34+
client.writeQuery({
35+
query: GET_COMMENTS_OF_ISSUE,
36+
variables: {
37+
repositoryOwner,
38+
repositoryName,
39+
number: issue.number
40+
},
41+
data: {
42+
...data,
43+
repository: {
44+
...data.repository,
45+
issue: {
46+
...data.repository.issue,
47+
comments: {
48+
...data.repository.issue.comments,
49+
edges: [
50+
...data.repository.issue.comments.edges,
51+
commentEdge
52+
]
53+
}
54+
}
55+
}
56+
}
57+
})
58+
};
59+
1060
class CommentAdd extends Component {
1161
state = {
1262
value: '',
@@ -23,13 +73,18 @@ class CommentAdd extends Component {
2373
};
2474

2575
render() {
26-
const { issue } = this.props;
76+
const { issue, repositoryOwner, repositoryName } = this.props;
2777
const { value } = this.state;
2878

2979
return (
3080
<Mutation
3181
mutation={ADD_COMMENT}
3282
variables={{ body: value, subjectId: issue.id }}
83+
update={(client, data) => updateComments({
84+
repositoryOwner,
85+
repositoryName,
86+
issue
87+
}, client, data)}
3388
>
3489
{(addComment, { data, loading, error }) => (
3590
<div>

src/Comment/CommentList/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ const Comments = ({ repositoryOwner, repositoryName, issue }) => (
7070

7171
<CommentAdd
7272
issue={issue}
73+
repositoryOwner={repositoryOwner}
74+
repositoryName={repositoryName}
7375
/>
7476
</Fragment>
7577
);

0 commit comments

Comments
 (0)