Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { memo, useState, useEffect } from 'react';
  2. import { useQuery, useMutation } from '@apollo/react-hooks';
  3. import PropTypes from 'prop-types';
  4.  
  5. import { CREATE_COMMENT, GET_IMAGE, GET_ANNOTATION } from '../resolvers';
  6. import CloseBtn from './CloseBtn';
  7. import Comments from './Comments';
  8. import AddComment from './AddComment';
  9.  
  10. import { EditorArea } from '../AnnotationStyle';
  11.  
  12. function ViewSingleAnnotation(props) {
  13.   const [comment, setComment] = useState('');
  14.   const [commentReply, setReplyOnComment] = useState([]);
  15.   const { data } = props;
  16.   const {
  17.     x,
  18.     y,
  19.     id,
  20.     text,
  21.     imageId,
  22.     author: { name },
  23.     closeAnnotation,
  24.   } = data;
  25.  
  26.   const {
  27.     deleteAnnotation,
  28.     currentUser,
  29.     commentId,
  30.     updateComment,
  31.     annotations,
  32.     commentField,
  33.   } = props;
  34.  
  35.   const { replyOnComment } = data;
  36.   useEffect(() => {
  37.     setReplyOnComment(replyOnComment);
  38.   }, []);
  39.  
  40.   const addComment = async (e, createCommentMutation) => {
  41.     const {
  42.       data: { text },
  43.     } = props;
  44.     const { submitAnnotation, createAnnotation } = props;
  45.     e.preventDefault();
  46.     if (!text) {
  47.       await submitAnnotation(comment, createAnnotation, createCommentMutation);
  48.     } else {
  49.       await createCommentMutation();
  50.     }
  51.     setComment('');
  52.   };
  53.  
  54.   const onSubmit = async (cache, { data }) => {
  55.     const { updateComments } = props;
  56.     const { replyOnComment } = data.createComment;
  57.     await updateComments(replyOnComment);
  58.   };
  59.  
  60.   const update = async (cache, { data }) => {
  61.     const commentId = data.deleteComment.id;
  62.     const newComments = commentReply.filter(
  63.       (comment) => comment.id !== commentId,
  64.     );
  65.     const { updateComments } = props;
  66.     updateComments(newComments);
  67.   };
  68.  
  69.   const onChange = async (e) => {
  70.     const { updateComment } = props;
  71.     setReplyOnComment(e.target.value);
  72.     await updateComment(e.target.value);
  73.   };
  74.  
  75.   const [createComment] = useMutation(CREATE_COMMENT, {
  76.     variables: { annotationId: id, text: comment },
  77.     refetchQueries: [{ query: GET_IMAGE, variables: { imageId } }],
  78.     update: onSubmit,
  79.   });
  80.  
  81.   return (
  82.     <EditorArea x={x} y={y}>
  83.       <form
  84.         className={x < 50 ? 'show-right' : 'show-left'}
  85.         key={`${id} + 1`}
  86.         onSubmit={async (e) => {
  87.           e.preventDefault();
  88.           addComment(e, createComment, data, id);
  89.           updateComment('');
  90.         }}
  91.       >
  92.         <CloseBtn
  93.           text={text}
  94.           deleteAnnotation={deleteAnnotation}
  95.           closeAnnotation={closeAnnotation}
  96.         />
  97.  
  98.         <Comments
  99.           text={text}
  100.           replyOnComment={replyOnComment}
  101.           update={update}
  102.           imageId={imageId}
  103.           name={name}
  104.         />
  105.  
  106.         <AddComment
  107.           id={id}
  108.           currentUser={currentUser}
  109.           commentField={commentField}
  110.           onChange={onChange}
  111.         />
  112.       </form>
  113.     </EditorArea>
  114.   );
  115. }
  116.  
  117. ViewSingleAnnotation.propTypes = {
  118.   deleteAnnotation: PropTypes.func.isRequired,
  119.   data: PropTypes.object.isRequired,
  120.   updateComments: PropTypes.func.isRequired,
  121.   currentUser: PropTypes.string.isRequired,
  122.   submitAnnotation: PropTypes.func.isRequired,
  123.   createAnnotation: PropTypes.func.isRequired,
  124. };
  125.  
  126. export default memo(ViewSingleAnnotation);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement