Skip to content
Snippets Groups Projects
CommentBox.js 2.89 KiB
Newer Older
import React from 'react';
import PropTypes from 'prop-types';
import styled, { css } from 'styled-components';
Yannis Barlas's avatar
Yannis Barlas committed

// import { th } from '../_helpers'
import CommentItemList from './CommentItemList';
import CommentReply from './CommentReply';
Yannis Barlas's avatar
Yannis Barlas committed

const inactive = css`
  background: #e2e2e2;
  cursor: pointer;
  transition: box-shadow 0.2s;
  /* transition: background-color 0.2s; */

  &:hover {
    /* background: white; */
    box-shadow: 0 0 1px 2px gray;
  }
Yannis Barlas's avatar
Yannis Barlas committed

const Wrapper = styled.div`
  background: white;
  border: 1px solid gray;
  border-radius: 3px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  /* padding: 8px 0; */

  ${props => !props.active && inactive}
Yannis Barlas's avatar
Yannis Barlas committed

const Head = styled.div`
  display: flex;
  justify-content: flex-end;
  padding: 8px 16px 0;
Yannis Barlas's avatar
Yannis Barlas committed

const Resolve = styled.button`
  align-self: flex-end;
  cursor: pointer;
  margin-bottom: 12px;

  &:hover {
    background: blue;
  }
Yannis Barlas's avatar
Yannis Barlas committed

const StyledReply = styled(CommentReply)`
  border-top: ${props => !props.isNewComment && '1px solid gray'};
Yannis Barlas's avatar
Yannis Barlas committed

const CommentBox = props => {
  const {
    active,
    className,
    commentId,
    commentData,
    onClickBox,
Yannis Barlas's avatar
Yannis Barlas committed
    onClickResolve,
Yannis Barlas's avatar
Yannis Barlas committed

  // send signal to make this comment active
  const onClickWrapper = () => {
    if (active) return;
    onClickBox(commentId);
  };
Yannis Barlas's avatar
Yannis Barlas committed

  if (!active && (!commentData || commentData.length === 0)) return null;

Yannis Barlas's avatar
Yannis Barlas committed
  return (
    <Wrapper active={active} className={className} onClick={onClickWrapper}>
      {active && commentData.length > 0 && (
Yannis Barlas's avatar
Yannis Barlas committed
        <Head>
          <Resolve onClick={() => onClickResolve(commentId)}>Resolve</Resolve>
        </Head>
      )}

      <CommentItemList active={active} data={commentData} />

      {active && (
        <StyledReply
          isNewComment={commentData.length === 0}
          onClickPost={onClickPost}
        />
      )}
Yannis Barlas's avatar
Yannis Barlas committed
    </Wrapper>
Yannis Barlas's avatar
Yannis Barlas committed

CommentBox.propTypes = {
  /** Whether this is the current active comment */
  active: PropTypes.bool,
  /** List of objects containing data for comment items */
  commentData: PropTypes.arrayOf(
    PropTypes.shape({
      content: PropTypes.string.isRequired,
      displayName: PropTypes.string.isRequired,
      timestamp: PropTypes.string.isRequired,
    }),
  ),
  /** This comment's id in the document */
  commentId: PropTypes.string.isRequired,
  /** Function to run when box is clicked on.
   *  Use this to make comment active on click (it passes on the comment id).
   *  eg. `onClickBox = commentId => doSth(commentId)`
   *  Will only run if comment is not active already.
   */
  onClickBox: PropTypes.func.isRequired,
  /** Function to run when "post" button is clicked to send reply */
  onClickPost: PropTypes.func.isRequired,
Yannis Barlas's avatar
Yannis Barlas committed
  /** Function to run when "resolve" button is clicked */
  onClickResolve: PropTypes.func.isRequired,
Yannis Barlas's avatar
Yannis Barlas committed

CommentBox.defaultProps = {
  active: false,
  commentData: [],
Yannis Barlas's avatar
Yannis Barlas committed

export default CommentBox;