{"size": 1249, "ext": "js", "max_stars_repo_path": "17/src/components/PostEditor/PostEditor.js", "max_stars_repo_name": "radomely/React-practice", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "17/src/components/PostEditor/PostEditor.js", "max_issues_repo_name": "radomely/React-practice", "max_issues_repo_issues_event_min_datetime": "2020-09-06T13:16:17.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T15:26:49.000Z", "max_forks_repo_path": "17/src/components/PostEditor/PostEditor.js", "max_forks_repo_name": "radomely/React-practice", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 13, "max_forks_count": null, "avg_line_length": 20.4754098361, "max_line_length": 69, "alphanum_fraction": 0.5612489992}
import React, { Component } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import * as postsOperations from '../../redux/posts/postsOperations';

class PostEditor extends Component {
  static propTypes = {
    onSubmit: PropTypes.func.isRequired,
  };

  constructor(props) {
    super(props);
    this.state = { text: '', tag: '' };
  }

  handleChange = e => {
    this.setState({
      [e.target.name]: e.target.value,
    });
  };

  handleSubmit = e => {
    const { onSubmit } = this.props;
    e.preventDefault();
    onSubmit({ ...this.state });
    this.setState({ text: '', tag: '' });
  };

  render() {
    const { text, tag } = this.state;

    return (
      <form onSubmit={this.handleSubmit}>
        <textarea
          name="text"
          cols="30"
          rows="10"
          value={text}
          onChange={this.handleChange}
        />
        <input
          type="text"
          name="tag"
          value={tag}
          onChange={this.handleChange}
        />
        <button type="submit">Add Post</button>
      </form>
    );
  }
}

const mapDispatchToProps = {
  onSubmit: postsOperations.addPost,
};

export default connect(
  null,
  mapDispatchToProps,
)(PostEditor);
