Skip to content
Snippets Groups Projects
Comment.js 2.41 KiB
Newer Older
chris's avatar
chris committed
import React, {
  Fragment,
  useState,
  useEffect,
  useContext,
  useRef
} from "react";
chris's avatar
chris committed
import { v4 as uuidv4 } from "uuid";
chris's avatar
chris committed

import styled from "styled-components";
import { WaxContext } from "wax-prosemirror-core";
chris's avatar
chris committed
import { DocumentHelpers } from "wax-prosemirror-utilities";
chris's avatar
chris committed

chris's avatar
chris committed
const SinlgeCommentRow = styled.div`
  padding: 4px;
  border-bottom: 1px solid #ffab20;
`;

chris's avatar
chris committed
export default ({ comment, activeView, user }) => {
  const commentInput = useRef(null);
  const [currentUser, setCurrentuser] = useState(user);
  const [commentAnnotation, setCommentAnnotation] = useState(comment);
chris's avatar
chris committed
  const [commentInputValue, setcommentInputValue] = useState("");
chris's avatar
chris committed
  const { state, dispatch } = activeView;
  const { attrs: { conversation } } = comment;

  const handleKeyDown = event => {
    if (event.key === "Enter" || event.which === 13) {
      saveComment();
    }
  };

  const saveComment = () => {
    const { current: { value } } = commentInput;
    const { tr, doc } = state;
    const commentMark = state.schema.marks.comment;

    const obj = { [user.username]: value };
    commentAnnotation.attrs.conversation.push(obj);
chris's avatar
chris committed
    const actualComment = DocumentHelpers.findMark(state, commentMark);
chris's avatar
chris committed

    dispatch(
      tr.addMark(
chris's avatar
chris committed
        actualComment.from,
        actualComment.to,
chris's avatar
chris committed
        commentMark.create({
          ...((commentAnnotation && commentAnnotation.attrs) || {}),
          conversation: commentAnnotation.attrs.conversation
        })
      )
    );
chris's avatar
chris committed
    setcommentInputValue("");
  };

  const updateCommentInputValue = () => {
    const { current: { value } } = commentInput;
    setcommentInputValue(value);
  };

  const commentInputReply = () => {
    return (
      <Fragment>
        <input
          type="text"
          ref={commentInput}
          placeholder="add a new comment"
          onChange={updateCommentInputValue}
          onKeyPress={handleKeyDown}
          autoFocus
          value={commentInputValue}
        />
        <button onClick={saveComment}>Post</button>
        <button>Cancel</button>
      </Fragment>
    );
chris's avatar
chris committed
  };

chris's avatar
chris committed
  return conversation.length === 0 ? (
    <Fragment>{commentInputReply()}</Fragment>
  ) : (
chris's avatar
chris committed
    <Fragment>
chris's avatar
chris committed
      {conversation.map((singleComment, index) => {
        return (
          <SinlgeCommentRow key={uuidv4()}>{`${user.username} : ${
            singleComment[user.username]
          }`}</SinlgeCommentRow>
        );
      })}
      {commentInputReply()}
chris's avatar
chris committed
    </Fragment>
  );
};