Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show new comment in UI #29

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 75 additions & 2 deletions src/Comment/CommentAdd/index.js
Original file line number Diff line number Diff line change
@@ -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: '',
Expand All @@ -23,13 +73,36 @@ class CommentAdd extends Component {
};

render() {
const { issueId } = this.props;
const { issue, repositoryOwner, repositoryName } = this.props;
const { value } = this.state;

return (
<Mutation
mutation={ADD_COMMENT}
variables={{ body: value, subjectId: issueId }}
variables={{ body: value, subjectId: issue.id }}
optimisticResponse={{
addComment: {
__typename: 'Mutation',
commentEdge: {
__typename: 'IssueCommentEdge',
node: {
__typename: 'IssueComment',
id: new Date().getTime() + '',
databaseId: new Date().getTime(),
author: {
__typename: 'User',
login: 'me'
},
bodyHTML: value
}
}
}
}}
update={(client, data) => updateComments({
repositoryOwner,
repositoryName,
issue
}, client, data)}
>
{(addComment, { data, loading, error }) => (
<div>
Expand Down
5 changes: 4 additions & 1 deletion src/Comment/CommentAdd/mutations.js
Original file line number Diff line number Diff line change
@@ -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}
`;
25 changes: 18 additions & 7 deletions src/Comment/CommentList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
};
};

Expand Down Expand Up @@ -68,7 +75,11 @@ const Comments = ({ repositoryOwner, repositoryName, issue }) => (
fetchMore={fetchMore}
/>

<CommentAdd issueId={repository.issue.id} />
<CommentAdd
issue={issue}
repositoryOwner={repositoryOwner}
repositoryName={repositoryName}
/>
</Fragment>
);
}}
Expand Down
9 changes: 4 additions & 5 deletions src/Comment/CommentList/queries.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import gql from 'graphql-tag';

import ISSUECOMMENT_FRAGMENT from '../fragment';

export const GET_COMMENTS_OF_ISSUE = gql`
query(
$repositoryOwner: String!
Expand All @@ -13,11 +15,7 @@ export const GET_COMMENTS_OF_ISSUE = gql`
comments(first: 1, after: $cursor) {
edges {
node {
id
bodyHTML
author {
login
}
...issueComment
}
}
pageInfo {
Expand All @@ -28,4 +26,5 @@ export const GET_COMMENTS_OF_ISSUE = gql`
}
}
}
${ISSUECOMMENT_FRAGMENT}
`;
12 changes: 12 additions & 0 deletions src/Comment/fragment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import gql from 'graphql-tag';

export default gql`
fragment issueComment on IssueComment {
id
databaseId
bodyHTML
author {
login
}
}
`;