diff --git a/src/Comment/CommentAdd/index.js b/src/Comment/CommentAdd/index.js index bf6f92b..c258c98 100755 --- a/src/Comment/CommentAdd/index.js +++ b/src/Comment/CommentAdd/index.js @@ -1,12 +1,62 @@ import React, { Component } from 'react'; import { Mutation } from 'react-apollo'; +import { GET_COMMENTS_OF_ISSUE } from '../CommentList/queries'; import { ADD_COMMENT } from './mutations'; import TextArea from '../../TextArea'; import Button from '../../Button'; import ErrorMessage from '../../Error'; +const updateComments = ({ + repositoryOwner, + repositoryName, + issue +}, +client, +{ + data: { + addComment: { + commentEdge + } + } +} +) => { + const data = client.readQuery({ + query: GET_COMMENTS_OF_ISSUE, + variables: { + repositoryOwner, + repositoryName, + number: issue.number + } + }); + + client.writeQuery({ + query: GET_COMMENTS_OF_ISSUE, + variables: { + repositoryOwner, + repositoryName, + number: issue.number + }, + data: { + ...data, + repository: { + ...data.repository, + issue: { + ...data.repository.issue, + comments: { + ...data.repository.issue.comments, + edges: [ + ...data.repository.issue.comments.edges, + commentEdge + ] + } + } + } + } + }) +}; + class CommentAdd extends Component { state = { value: '', @@ -23,13 +73,36 @@ class CommentAdd extends Component { }; render() { - const { issueId } = this.props; + const { issue, repositoryOwner, repositoryName } = this.props; const { value } = this.state; return ( updateComments({ + repositoryOwner, + repositoryName, + issue + }, client, data)} > {(addComment, { data, loading, error }) => (
diff --git a/src/Comment/CommentAdd/mutations.js b/src/Comment/CommentAdd/mutations.js index 8e7dbea..01ae960 100755 --- a/src/Comment/CommentAdd/mutations.js +++ b/src/Comment/CommentAdd/mutations.js @@ -1,13 +1,16 @@ import gql from 'graphql-tag'; +import ISSUECOMMENT_FRAGMENT from '../fragment'; + export const ADD_COMMENT = gql` mutation($subjectId: ID!, $body: String!) { addComment(input: { subjectId: $subjectId, body: $body }) { commentEdge { node { - body + ...issueComment } } } } + ${ISSUECOMMENT_FRAGMENT} `; diff --git a/src/Comment/CommentList/index.js b/src/Comment/CommentList/index.js index 41b5384..b156fdf 100755 --- a/src/Comment/CommentList/index.js +++ b/src/Comment/CommentList/index.js @@ -27,12 +27,19 @@ const updateQuery = (previousResult, { fetchMoreResult }) => { ...previousResult.repository.issue.comments, ...fetchMoreResult.repository.issue.comments, edges: [ - ...previousResult.repository.issue.comments.edges, - ...fetchMoreResult.repository.issue.comments.edges, - ], - }, - }, - }, + ...previousResult + .repository + .issue + .comments + .edges + .filter( + previousEdge => !fetchMoreResult.repository.issue.comments.edges.some( + fetchMoreEdge => fetchMoreEdge.node.id === previousEdge.node.id)), + ...fetchMoreResult.repository.issue.comments.edges + ].sort((a, b) => a.node.databaseId - b.node.databaseId) + } + } + } }; }; @@ -68,7 +75,11 @@ const Comments = ({ repositoryOwner, repositoryName, issue }) => ( fetchMore={fetchMore} /> - + ); }} diff --git a/src/Comment/CommentList/queries.js b/src/Comment/CommentList/queries.js index 47b6cde..8967a8c 100755 --- a/src/Comment/CommentList/queries.js +++ b/src/Comment/CommentList/queries.js @@ -1,5 +1,7 @@ import gql from 'graphql-tag'; +import ISSUECOMMENT_FRAGMENT from '../fragment'; + export const GET_COMMENTS_OF_ISSUE = gql` query( $repositoryOwner: String! @@ -13,11 +15,7 @@ export const GET_COMMENTS_OF_ISSUE = gql` comments(first: 1, after: $cursor) { edges { node { - id - bodyHTML - author { - login - } + ...issueComment } } pageInfo { @@ -28,4 +26,5 @@ export const GET_COMMENTS_OF_ISSUE = gql` } } } + ${ISSUECOMMENT_FRAGMENT} `; diff --git a/src/Comment/fragment.js b/src/Comment/fragment.js new file mode 100644 index 0000000..b784b78 --- /dev/null +++ b/src/Comment/fragment.js @@ -0,0 +1,12 @@ +import gql from 'graphql-tag'; + +export default gql` + fragment issueComment on IssueComment { + id + databaseId + bodyHTML + author { + login + } + } +`; \ No newline at end of file