<commit_before>import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { compose } from 'ramda';
import { withRouter } from 'react-router';
import { connect } from 'react-redux';
import DropdownMenu from '../../../common/dropdownMenu';
import SpreadContent from '../../../common/spreadContent';
import EditButton from '../../buttons/editCfeiButton';
import NewCfeiModalButton from '../../modals/newCfei/newCfeiModalButton';
import DeleteButton from '../../buttons/deleteCfeiButton';
import CancelButton from '../../buttons/cancelCfeiButton';
import DownloadButton from '../../buttons/downloadCfeiButton';
import Complete from '../../buttons/completeCfeiButton';
import SendDsrButton from '../../buttons/sendDsrButton';
import PublishDsrButton from '../../buttons/publishDsrButton';
import SendDsrModal from '../../modals/completeDsr/sendDsrModal';
import DeleteDsrModal from '../../modals/completeDsr/deleteDsrModal';
import CancelDsrModal from '../../modals/completeDsr/cancelDsrModal';
import EditDsrModal from '../../modals/editDsr/editDsrModal';
import PublishDsrModal from '../../modals/completeDsr/publishDsrModal';
import FinalizeDsrModal from '../../modals/completeDsr/finalizeDsrModal';
import withMultipleDialogHandling from '../../../common/hoc/withMultipleDialogHandling';
import { checkPermission, isRoleOffice, AGENCY_ROLES, AGENCY_PERMISSIONS, COMMON_PERMISSIONS } from '../../../../helpers/permissions';
import { selectCfeiStatus,
  isCfeiPublished,
  isCfeiCompleted,
  isUserAFocalPoint,
  isUserACreator,
  selectCfeiDetails,
} from '../../../../store';

const edit = 'edit';
const del = 'del';
const cancel = 'cancel';
const download = 'download';
const send = 'send';
const publish = 'publish';
const complete = 'complete';

class AgencyDirectHeaderOptions extends Component {
  constructor(props) {
    super(props);

    this.sendOptions = this.sendOptions.bind(this);
    this.publishOptions = this.publishOptions.bind(this);
    this.isActionAllowed = this.isActionAllowed.bind(this);
  }

  sendOptions() {
    const {
      params: { id },
      handleDialogOpen,
      hasEditDraftPermission,
      hasDeleteDraftPermission,
      isCreator } = this.props;

    const options = [
      {
        name: download,
        content: <DownloadButton handleClick={() => { window.open(`/api/projects/${id}/?export=pdf`, '_self'); }} />,
      },
    ];

    if (hasEditDraftPermission && isCreator) {
      options.push(
        {
          name: edit,
          content: <EditButton
            handleClick={() => handleDialogOpen(edit)}
          />,
        });
    }

    if (hasDeleteDraftPermission && isCreator) {
      options.push(
        {
          name: del,
          content: <DeleteButton handleClick={() => handleDialogOpen(del)} />,
        });
    }

    return options;
  }

  isActionAllowed(hasActionPermission) {
    const {
      isAdvEd,
      isMFT,
      isPAM,
      isBasEd,
      isCreator,
      isFocalPoint } = this.props;

    return ((hasActionPermission && isAdvEd && (isCreator || isFocalPoint))
    || (hasActionPermission && isMFT && isFocalPoint)
    || (hasActionPermission && isBasEd && isCreator)
    || (hasActionPermission && isPAM && isCreator));
  }

  publishOptions() {
    const {
      handleDialogOpen,
      hasEditSentPermission,
      hasEditPublishedPermission,
      hasCancelPermission,
      isPublished,
      status,
      isCompleted,
      isAdvEd,
      isMFT,
      isFocalPoint } = this.props;

    const options = [
      {
        name: download,
        content: <DownloadButton handleClick={() => {}} />,
      },
    ];

    if ((!isCompleted && isPublished && this.isActionAllowed(hasEditPublishedPermission))
    || (!isPublished && status === 'Sen' && ((hasEditSentPermission && isAdvEd && isFocalPoint)
    || (!isCompleted && hasEditPublishedPermission && isMFT && isFocalPoint)))) {
      options.push(
        {
          name: edit,
          content: <EditButton handleClick={() => handleDialogOpen(edit)} />,
        });
    }

    if (!isCompleted && this.isActionAllowed(hasCancelPermission)) {
      options.push(
        {
          name: del,
          content: <CancelButton handleClick={() => handleDialogOpen(cancel)} />,
        });
    }

    return options;
  }

  isPartnerVerified() {
    const { cfei } = this.props;

    if (cfei.direct_selected_partners && cfei.direct_selected_partners.length > 0) {
      return cfei.direct_selected_partners[0].partner_is_verified;
    }

    return false;
  }

  render() {
    const { params: { id },
      isFocalPoint,
      isCreator,
      isPublished,
      isCompleted,
      status,
      dialogOpen,
      hasSendPermission,
      hasPublishPermission,
      hasFinalizePermission,
      isMFT,
      isAdvEd,
      handleDialogClose,
      handleDialogOpen } = this.props;

    return (
      <SpreadContent>
        {!isCompleted && isPublished && this.isActionAllowed(hasFinalizePermission)
          && <Complete handleClick={() => handleDialogOpen(complete)} />}

        {!isCompleted && status === 'Dra' && isCreator && hasSendPermission
         && <SendDsrButton handleClick={() => handleDialogOpen(send)} />}

        {!isPublished && !isCompleted && hasPublishPermission &&
            (((isFocalPoint || isCreator) && isAdvEd) || (isFocalPoint && isMFT))
         && <PublishDsrButton disabled={!this.isPartnerVerified()} handleClick={() => handleDialogOpen(publish)} />}

        <DropdownMenu
          options={status === 'Dra' ? this.sendOptions() : this.publishOptions()}
        />

        {dialogOpen[cancel] && <CancelDsrModal
          id={id}
          dialogOpen={dialogOpen[cancel]}
          handleDialogClose={handleDialogClose}
        />}
        {dialogOpen[del] && <DeleteDsrModal
          id={id}
          dialogOpen={dialogOpen[del]}
          handleDialogClose={handleDialogClose}
        />}
        {dialogOpen[edit] && <EditDsrModal
          id={id}
          type="direct"
          open={dialogOpen[edit]}
          handleDialogClose={handleDialogClose}
        />}
        {dialogOpen[send] && <SendDsrModal
          id={id}
          type="direct"
          dialogOpen={dialogOpen[send]}
          handleDialogClose={handleDialogClose}
        />}
        {dialogOpen[publish] && <PublishDsrModal
          id={id}
          type="direct"
          dialogOpen={dialogOpen[publish]}
          handleDialogClose={handleDialogClose}
        />}
        {dialogOpen[complete] && <FinalizeDsrModal
          id={id}
          dialogOpen={dialogOpen[complete]}<commit_msg>added second dsr edit modal for published status<commit_after>import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { compose } from 'ramda';
import { withRouter } from 'react-router';
import { connect } from 'react-redux';
import DropdownMenu from '../../../common/dropdownMenu';
import SpreadContent from '../../../common/spreadContent';
import EditButton from '../../buttons/editCfeiButton';
import DeleteButton from '../../buttons/deleteCfeiButton';
import CancelButton from '../../buttons/cancelCfeiButton';
import DownloadButton from '../../buttons/downloadCfeiButton';
import Complete from '../../buttons/completeCfeiButton';
import SendDsrButton from '../../buttons/sendDsrButton';
import PublishDsrButton from '../../buttons/publishDsrButton';
import SendDsrModal from '../../modals/completeDsr/sendDsrModal';
import DeleteDsrModal from '../../modals/completeDsr/deleteDsrModal';
import CancelDsrModal from '../../modals/completeDsr/cancelDsrModal';
import EditCfeiModal from '../../modals/editCfei/editCfeiModal';
import EditDsrModal from '../../modals/editDsr/editDsrModal';
import PublishDsrModal from '../../modals/completeDsr/publishDsrModal';
import FinalizeDsrModal from '../../modals/completeDsr/finalizeDsrModal';
import withMultipleDialogHandling from '../../../common/hoc/withMultipleDialogHandling';
import { checkPermission, isRoleOffice, AGENCY_ROLES, AGENCY_PERMISSIONS, COMMON_PERMISSIONS } from '../../../../helpers/permissions';
import { selectCfeiStatus,
  isCfeiPublished,
  isCfeiCompleted,
  isUserAFocalPoint,
  isUserACreator,
  selectCfeiDetails,
} from '../../../../store';

const edit = 'edit';
const del = 'del';
const cancel = 'cancel';
const download = 'download';
const send = 'send';
const publish = 'publish';
const complete = 'complete';
const editPublished = 'editPublished';

class AgencyDirectHeaderOptions extends Component {
  constructor(props) {
    super(props);

    this.sendOptions = this.sendOptions.bind(this);
    this.publishOptions = this.publishOptions.bind(this);
    this.isActionAllowed = this.isActionAllowed.bind(this);
  }

  sendOptions() {
    const {
      params: { id },
      handleDialogOpen,
      hasEditDraftPermission,
      hasDeleteDraftPermission,
      isCreator } = this.props;

    const options = [
      {
        name: download,
        content: <DownloadButton handleClick={() => { window.open(`/api/projects/${id}/?export=pdf`, '_self'); }} />,
      },
    ];

    if (hasEditDraftPermission && isCreator) {
      options.push(
        {
          name: edit,
          content: <EditButton
            handleClick={() => handleDialogOpen(edit)}
          />,
        });
    }

    if (hasDeleteDraftPermission && isCreator) {
      options.push(
        {
          name: del,
          content: <DeleteButton handleClick={() => handleDialogOpen(del)} />,
        });
    }

    return options;
  }

  isActionAllowed(hasActionPermission) {
    const {
      isAdvEd,
      isMFT,
      isPAM,
      isBasEd,
      isCreator,
      isFocalPoint } = this.props;

    return ((hasActionPermission && isAdvEd && (isCreator || isFocalPoint))
    || (hasActionPermission && isMFT && isFocalPoint)
    || (hasActionPermission && isBasEd && isCreator)
    || (hasActionPermission && isPAM && isCreator));
  }

  publishOptions() {
    const {
      handleDialogOpen,
      hasEditSentPermission,
      hasEditPublishedPermission,
      hasCancelPermission,
      isPublished,
      status,
      isCompleted,
      isAdvEd,
      isMFT,
      isFocalPoint } = this.props;

    const options = [
      {
        name: download,
        content: <DownloadButton handleClick={() => {}} />,
      },
    ];

    console.log(isCompleted, isPublished, hasEditPublishedPermission);
    if ((!isCompleted && isPublished && this.isActionAllowed(hasEditPublishedPermission))
    || (!isPublished && status === 'Sen' && ((hasEditSentPermission && isAdvEd && isFocalPoint)
    || (!isCompleted && hasEditPublishedPermission && isMFT && isFocalPoint)))) {
      options.push(
        {
          name: edit,
          content: <EditButton handleClick={() => handleDialogOpen(editPublished)} />,
        });
    }

    if (!isCompleted && this.isActionAllowed(hasCancelPermission)) {
      options.push(
        {
          name: del,
          content: <CancelButton handleClick={() => handleDialogOpen(cancel)} />,
        });
    }

    return options;
  }

  isPartnerVerified() {
    const { cfei } = this.props;

    if (cfei.direct_selected_partners && cfei.direct_selected_partners.length > 0) {
      return cfei.direct_selected_partners[0].partner_is_verified;
    }

    return false;
  }

  render() {
    const { params: { id },
      isFocalPoint,
      isCreator,
      isPublished,
      isCompleted,
      status,
      dialogOpen,
      hasSendPermission,
      hasPublishPermission,
      hasFinalizePermission,
      isMFT,
      isAdvEd,
      handleDialogClose,
      handleDialogOpen } = this.props;

    return (
      <SpreadContent>
        {!isCompleted && isPublished && this.isActionAllowed(hasFinalizePermission)
          && <Complete handleClick={() => handleDialogOpen(complete)} />}

        {!isCompleted && status === 'Dra' && isCreator && hasSendPermission
         && <SendDsrButton handleClick={() => handleDialogOpen(send)} />}

        {!isPublished && !isCompleted && hasPublishPermission &&
            (((isFocalPoint || isCreator) && isAdvEd) || (isFocalPoint && isMFT))
         && <PublishDsrButton disabled={!this.isPartnerVerified()} handleClick={() => handleDialogOpen(publish)} />}

        <DropdownMenu
          options={status === 'Dra' ? this.sendOptions() : this.publishOptions()}
        />

        {dialogOpen[cancel] && <CancelDsrModal
          id={id}
          dialogOpen={dialogOpen[cancel]}
          handleDialogClose={handleDialogClose}
        />}
        {dialogOpen[del] && <DeleteDsrModal
          id={id}
          dialogOpen={dialogOpen[del]}
          handleDialogClose={handleDialogClose}
        />}
        {dialogOpen[edit] && <EditDsrModal
          id={id}
          type="direct"
          open={dialogOpen[edit]}
          handleDialogClose={handleDialogClose}
        />}
        {dialogOpen[editPublished] && <EditCfeiModal
          id={id}
          type="direct"
          dialogOpen={dialogOpen[editPublished]}
          handleDialogClose={handleDialogClose}
        />}
        {dialogOpen[send] && <SendDsrModal
          id={id}
          type="direct"
          dialogOpen={dialogOpen[send]}
          handleDialogClose={handleDialogClose}
        />}
        {dialogOpen[publish] && <PublishDsrModal
          id={id}
          type="direct"
          dialogOpen={dialogOpen[publish]}
          handleDialogClose={handleDialogClose}
        />}
        {dialogOpen[complete] && <FinalizeDsrModal
          id={id}
          dialogOpen={dialogOpen[complete]}
{'fraction_non_alphanumeric': 0.1381574094563694, 'fraction_numerical': 0.00029505052740281775, 'mean_word_length': 2.7755499860763018, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4673139', 'n_tokens_mistral': 3918, 'n_tokens_neox': 3912, 'n_words': 718}