{"size": 2128, "ext": "js", "max_stars_repo_path": "src/components/review/authors-controls.js", "max_stars_repo_name": "tensegrity666/x-check-app", "max_stars_repo_stars_event_min_datetime": "2020-09-03T15:06:14.000Z", "max_stars_repo_stars_event_max_datetime": "2020-09-03T15:06:14.000Z", "max_issues_repo_path": "src/components/review/authors-controls.js", "max_issues_repo_name": "tensegrity666/x-check-app", "max_issues_repo_issues_event_min_datetime": "2020-09-02T18:54:22.000Z", "max_issues_repo_issues_event_max_datetime": "2020-09-28T12:39:04.000Z", "max_forks_repo_path": "src/components/review/authors-controls.js", "max_forks_repo_name": "tensegrity666/x-check-app", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 31, "max_forks_count": null, "avg_line_length": 25.0352941176, "max_line_length": 79, "alphanum_fraction": 0.6442669173}
import React from 'react';
import { useSelector } from 'react-redux';
import PropTypes from 'prop-types';
import { Space, Button } from 'antd';

import { REVIEW_STATE } from './constants';
import { actionReviewList as modify } from '../../services/rest-api/constants';
import store from '../../redux/store';

const AuthorsControls = ({
  reviewStatus,
  createReview,
  editReview,
  toggleReviewStatus,
  isDisabled,
  isTouched,
}) => {
  const review = useSelector(({ reviewReducer }) => reviewReducer.review);

  const handlePublish = async () => {
    if (review.id) {
      await editReview();
      await toggleReviewStatus(modify.DRAFT_TO_PUBLISHED);
    } else {
      await createReview();
      const updatedId = store.getState().reviewReducer.review.id;
      await toggleReviewStatus(modify.DRAFT_TO_PUBLISHED, updatedId);
    }
  };

  const handleDraftSave = () => {
    if (review.id) {
      editReview();
    } else {
      createReview();
    }
  };

  if (reviewStatus === REVIEW_STATE.DRAFT) {
    return (
      <Space size="middle">
        <Button onClick={handleDraftSave} disabled={isDisabled}>
          Save Draft
        </Button>
        <Button onClick={handlePublish} disabled={isDisabled}>
          Publish
        </Button>
      </Space>
    );
  }

  if (reviewStatus === REVIEW_STATE.DISPUTED) {
    return (
      <Space size="middle">
        <Button onClick={editReview} disabled={isDisabled}>
          Save
        </Button>
        <Button
          onClick={() => toggleReviewStatus(modify.DISPUTED_TO_ACCEPTED)}
          disabled={isDisabled || isTouched}>
          Accept
        </Button>
      </Space>
    );
  }

  if (
    reviewStatus === REVIEW_STATE.ACCEPTED ||
    reviewStatus === REVIEW_STATE.PUBLISHED
  )
    return null;
  return null;
};

AuthorsControls.propTypes = {
  reviewStatus: PropTypes.string.isRequired,
  createReview: PropTypes.func.isRequired,
  editReview: PropTypes.func.isRequired,
  toggleReviewStatus: PropTypes.func.isRequired,
  isDisabled: PropTypes.bool.isRequired,
  isTouched: PropTypes.bool.isRequired,
};

export default AuthorsControls;
