|
| 1 | +import React, { Fragment } from 'react'; |
| 2 | +import { Query } from 'react-apollo'; |
| 3 | + |
| 4 | +import { GET_COMMENTS_OF_ISSUE } from './queries'; |
| 5 | +import CommentItem from '../CommentItem'; |
| 6 | +import CommentAdd from '../CommentAdd'; |
| 7 | + |
| 8 | +import Loading from '../../Loading'; |
| 9 | +import ErrorMessage from '../../Error'; |
| 10 | +import FetchMore from '../../FetchMore'; |
| 11 | + |
| 12 | +import './style.css'; |
| 13 | + |
| 14 | +const updateQuery = (previousResult, { fetchMoreResult }) => { |
| 15 | + if (!fetchMoreResult) { |
| 16 | + return previousResult; |
| 17 | + } |
| 18 | + |
| 19 | + return { |
| 20 | + ...previousResult, |
| 21 | + repository: { |
| 22 | + ...previousResult.repository, |
| 23 | + issue: { |
| 24 | + ...previousResult.repository.issue, |
| 25 | + ...fetchMoreResult.repository.issue, |
| 26 | + comments: { |
| 27 | + ...previousResult.repository.issue.comments, |
| 28 | + ...fetchMoreResult.repository.issue.comments, |
| 29 | + edges: [ |
| 30 | + ...previousResult.repository.issue.comments.edges, |
| 31 | + ...fetchMoreResult.repository.issue.comments.edges, |
| 32 | + ], |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + }; |
| 37 | +}; |
| 38 | + |
| 39 | +const Comments = ({ repositoryOwner, repositoryName, issue }) => ( |
| 40 | + <Query |
| 41 | + query={GET_COMMENTS_OF_ISSUE} |
| 42 | + variables={{ |
| 43 | + repositoryOwner, |
| 44 | + repositoryName, |
| 45 | + number: issue.number, |
| 46 | + }} |
| 47 | + notifyOnNetworkStatusChange={true} |
| 48 | + > |
| 49 | + {({ data, loading, error, fetchMore }) => { |
| 50 | + if (error) { |
| 51 | + return <ErrorMessage error={error} />; |
| 52 | + } |
| 53 | + |
| 54 | + const { repository } = data; |
| 55 | + |
| 56 | + if (loading && !repository) { |
| 57 | + return <Loading />; |
| 58 | + } |
| 59 | + |
| 60 | + return ( |
| 61 | + <Fragment> |
| 62 | + <CommentList |
| 63 | + comments={repository.issue.comments} |
| 64 | + loading={loading} |
| 65 | + number={issue.number} |
| 66 | + repositoryOwner={repositoryOwner} |
| 67 | + repositoryName={repositoryName} |
| 68 | + fetchMore={fetchMore} |
| 69 | + /> |
| 70 | + |
| 71 | + <CommentAdd issueId={repository.issue.id} /> |
| 72 | + </Fragment> |
| 73 | + ); |
| 74 | + }} |
| 75 | + </Query> |
| 76 | +); |
| 77 | + |
| 78 | +const CommentList = ({ |
| 79 | + comments, |
| 80 | + loading, |
| 81 | + repositoryOwner, |
| 82 | + repositoryName, |
| 83 | + number, |
| 84 | + fetchMore, |
| 85 | +}) => ( |
| 86 | + <div className="CommentList"> |
| 87 | + {comments.edges.map(({ node }) => ( |
| 88 | + <CommentItem key={node.id} comment={node} /> |
| 89 | + ))} |
| 90 | + |
| 91 | + <FetchMore |
| 92 | + loading={loading} |
| 93 | + hasNextPage={comments.pageInfo.hasNextPage} |
| 94 | + variables={{ |
| 95 | + cursor: comments.pageInfo.endCursor, |
| 96 | + repositoryOwner, |
| 97 | + repositoryName, |
| 98 | + number, |
| 99 | + }} |
| 100 | + updateQuery={updateQuery} |
| 101 | + fetchMore={fetchMore} |
| 102 | + > |
| 103 | + Comments |
| 104 | + </FetchMore> |
| 105 | + </div> |
| 106 | +); |
| 107 | + |
| 108 | +export default Comments; |
0 commit comments