All files / submitComment index.js

32% Statements 8/25
0% Branches 0/16
0% Functions 0/7
32% Lines 8/25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140        1x               1x             1x             1x                                           1x           1x         1x                                                                                                                                         1x                      
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import './styles.css';
 
const FormTitle = props => {
  const { type, title, userName, userEmail } = props;
 
  return type === 'Edit'
    ? <h2>{title}: <span>{`${userName} - ${userEmail}`}</span></h2>
    : <h2>{title}</h2>;
};
 
FormTitle.propTypes = {
  type: PropTypes.string,
  title: PropTypes.string,
  userName: PropTypes.string,
  userEmail: PropTypes.string
};
 
FormTitle.defaultProps = {
  type: '',
  title: '',
  userName: '',
  userEmail: ''
};
 
const UpdateOrCancelButtons = props => {
  const { _id, handleSubmit, handleCancel } = props;
 
  return (
    <div>
      <button
        key={'save'}
        type="submit"
        id={_id}
        style={{ marginRight: 10 }}
        onClick={handleSubmit}
        title="Update"
      >
        Update
      </button>
      <button key={'cancel'} id={_id} onClick={handleCancel} title="Cancel">
        Cancel
      </button>
    </div>
  );
};
 
UpdateOrCancelButtons.propTypes = {
  _id: PropTypes.string,
  handleSubmit: PropTypes.func,
  handleCancel: PropTypes.func
};
 
const SubmitButton = props =>
  <button type="submit" onClick={props.handleSubmit} title="Submit">
    Submit
  </button>;
 
SubmitButton.propTypes = {
  handleSubmit: PropTypes.func
};
 
class SubmitCommentForm extends Component {
  constructor(props) {
    super(props);
 
    this.state = {
      hasErrors: false,
      userComment: ''
    };
  }
 
  handleChange = event => {
    this.setState({
      hasErrors: false,
      userComment: event.target.value
    });
    this.props.handleChange(event.target.value);
  };
 
  handleSubmit = event => {
    event.preventDefault();
    event.stopPropagation();
 
    if (!this.state.userComment || this.state.userComment.length < 1) {
      this.setState({ hasErrors: true });
    } else {
      this.props.handleSubmit(event.target.id);
    }
  };
 
  render() {
    const {
      userComment,
      title = 'Add a comment:',
      type = 'Add',
      onCommentCancel,
      comment = {}
    } = this.props;
 
    const { _id = null, userEmail = '', userName = '' } = comment;
 
    return (
      <section className="blabbr-submitComment">
        <form>
          <FormTitle
            type={type}
            title={title}
            userName={userName}
            userEmail={userEmail}
          />
          <textarea value={userComment} onChange={this.handleChange} />
          {this.state.hasErrors &&
            <div className="error">You need to enter a comment.</div>}
          {type === 'Edit'
            ? <UpdateOrCancelButtons
                _id={_id}
                handleSubmit={this.handleSubmit}
                handleCancel={onCommentCancel}
              />
            : <SubmitButton _id={_id} handleSubmit={this.handleSubmit} />}
        </form>
      </section>
    );
  }
}
 
SubmitCommentForm.propTypes = {
  handleChange: PropTypes.func.isRequired,
  handleSubmit: PropTypes.func.isRequired,
  userComment: PropTypes.string,
  type: PropTypes.string,
  comment: PropTypes.object,
  onCommentCancel: PropTypes.func,
  title: PropTypes.string
};
 
export default SubmitCommentForm;