<commit_before>import React from 'react'
import { connect } from 'react-redux'
import { Form, Button, Radio, Popconfirm } from 'antd'
import { Form as FinalForm, Field } from 'react-final-form'
import arrayMutators from 'final-form-arrays'
import { withTranslation, useTranslation } from 'react-i18next'

import FinalField from '../../../utils/final-field'
import Condition from '../../../utils/condition'
import { doesFieldExist, getValidationSets } from '../../../utils/validation-utils'
import ItemArray, { PanelHeaderMore } from '../../../utils/item-array'
import getSymbolFromCurrency from '../../../utils/get-symbol-from-currency'
import InputLabel from '../../../utils/input-label'
import './styles.scss'
import OrganizationSelect from '../../../utils/organization-select'
import { removeSetItem } from '../actions'
import SectionContext from '../section-context'
import { useFetch } from '../../../utils/hooks'
import validationDefs from './partners/validations'
import { shouldUpdateSectionRoot } from '../../../utils/misc'

const { Item } = Form

const Partners = ({ removeSetItem, fields, headerMore, currency, headerMoreField, primaryOrganisation, validations, showRequired, errors }) => { // eslint-disable-line
  const [{ results }, loading] = useFetch('/typeaheads/organisations')
  const { t } = useTranslation()
  const validationSets = getValidationSets(validations, validationDefs)
  const fieldExists = doesFieldExist(validationSets)
  const removeItem = (index, fields) => { // eslint-disable-line
    removeSetItem(3, 'partners', index)
    fields.remove(index)
  }
  const currencySymbol = getSymbolFromCurrency(currency)
  const currencyRegExp = new RegExp(`\\${currencySymbol}\\s?|(,*)`, 'g')
  const ROLE_OPTIONS = [
    { value: 2, label: t('Accountable partner') },
    { value: 3, label: t('Extending partner') },
    { value: 1, label: t('Funding partner') },
    { value: 4, label: t('Implementing partner') },
    { value: 101, label: t('Reporting organisation') },
    { value: 100, label: t('Sponsor partner') },
  ]
  return (
    <div className="partners view">
      <div className="min-required-wrapper">
        <h3>{t('Project partners')}</h3>
        {showRequired && errors.findIndex(it => it.type === 'min' && it.path === 'partners') !== -1 && (
          <span className="min-required">{t('Minimum one required')}</span>
        )}
      </div>
      <SectionContext.Provider value="section3">
        <Form layout="vertical">
          <FinalForm
            onSubmit={() => { }}
            initialValues={fields}
            subscription={{}}
            mutators={{ ...arrayMutators }}
            render={({
              form: {
                mutators: { push }
              }
            }) => (
                <ItemArray
                  setName="partners"
                  sectionIndex={3}
                  header={(index, role) => {
                    return (
                      <Field name={`partners[${index}].organisationName`} subscription={{ value: true }}>
                        {({ input }) => {
                          return (
                            <span>{role ? `${ROLE_OPTIONS.find(it => it.value === role).label}: ${input.value}` : `Partner ${index + 1}`}</span>
                          )
                        }}
                      </Field>
                    )
                  }}
                  renderExtra={(name, index, fields) => { // eslint-disable-line
                    return (
                      <Field
                        name={`${name}.iatiOrganisationRole`}
                        render={(roleProps) => {
                          const disabled = roleProps.input.value === 101 && primaryOrganisation === 3394
                          if (!disabled) {
                            return (
                              <span onClick={event => event.stopPropagation()}>{/* eslint-disable-line */}
                                {headerMore && <PanelHeaderMore render={headerMore} field={headerMoreField} name={name} index={index} />}
                                <Popconfirm
                                  title={t('Are you sure to delete this?')}
                                  onConfirm={() => removeItem(index, fields)}
                                  okText={t('Yes')}
                                  cancelText={t('No')}
                                >
                                  <Button size="small" icon="delete" className="delete-panel" />
                                </Popconfirm>
                              </span>
                            )
                          }
                          return null
                        }}
                      />
                    )
                  }}
                  headerField="iatiOrganisationRole"
                  formPush={push}
                  panel={name => (
                    <div>
                      <Field
                        name={`${name}.iatiOrganisationRole`}
                        render={roleProps => {
                          const disabled = roleProps.input.value === 101 && primaryOrganisation === 3394
                          return (
                            <div>
                              <FinalField
                                name={`${name}.iatiOrganisationRole`}
                                control="select"
                                options={ROLE_OPTIONS}
                                disabled={disabled}
                                withLabel
                              />
                              <OrganizationSelect
                                name={name}
                                orgs={results}
                                loading={loading}
                                disabled={disabled}
                                dict={{
                                  label: t('project partner'),
                                  tooltip: t('Select an organisation that is playing a role in the project. If an organisation is not currently featured in RSR, please contact <a href=\'mailto: <EMAIL>\'><EMAIL></a> to request to add it to the database.')
                                }}
                              />
                              <Condition when={`${name}.iatiOrganisationRole`} is={101}>
                                <Item label={<InputLabel tooltip={t('section3::secondaryReporter::tooltip')} optional>{t('section3::secondaryReporter::label')}</InputLabel>}>
                                  <FinalField
                                    name={`${name}.isSecondaryReporter`}
                                    render={({ input }) => (
                                      <Radio.Group {...input} disabled={disabled}>
                                        <Radio.Button value>{t('Yes')}</Radio.Button>
                                        <Radio.Button value={false}>{t('No')}</Radio.Button>
                                      </Radio.Group>
                                    )}
                                  />
                                </Item>
                              </Condition>
                              <Condition when={`${name}.iatiOrganisationRole`} is={1}>
                                <FinalField
                                  name={`${name}.fundingAmount`}
                                  control="input-number"
                                  formatter={value => `${currencySymbol} ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
                                  parser={value => value.replace(currencyRegExp, '')}
                                  style={{ width: 200 }}
                                  withLabel
                                />
                              </Condition>
                              <FinalField
                                name={`${name}.iatiActivityId`}
                                disabled={disabled}
                                withLabel
                                optional
                                fieldExists={fieldExists}
                                control="input"
                              />
                            </div>
                          )
                        }}
                      />
                    </div>
                  )}
                  addButton={({ onClick }) => (
                    <Button className="bottom-btn" icon="plus" type="dashed" block onClick={onClick}>{t('Add another participating organisation')}</Button>
                  )}
                />
              )}
          />
        </Form>
      </SectionContext.Provider>
    </div>
  )
}

export default connect(
  ({ editorRdr: { validations, showRequired, section3: { fields, errors }, section1: { fields: { currency, primaryOrganisation } } } }) => ({ validations, currency, fields, primaryOrganisation, showRequired, errors }),
  { removeSetItem }
)(withTranslation()(React.memo(Partners, shouldUpdateSectionRoot)))<commit_msg>prevent deletion of reporting organization (thus losing access)<commit_after>import React from 'react'
import { connect } from 'react-redux'
import { Form, Button, Radio, Popconfirm } from 'antd'
import { Form as FinalForm, Field } from 'react-final-form'
import arrayMutators from 'final-form-arrays'
import { withTranslation, useTranslation } from 'react-i18next'

import FinalField from '../../../utils/final-field'
import Condition from '../../../utils/condition'
import { doesFieldExist, getValidationSets } from '../../../utils/validation-utils'
import ItemArray, { PanelHeaderMore } from '../../../utils/item-array'
import getSymbolFromCurrency from '../../../utils/get-symbol-from-currency'
import InputLabel from '../../../utils/input-label'
import './styles.scss'
import OrganizationSelect from '../../../utils/organization-select'
import { removeSetItem } from '../actions'
import SectionContext from '../section-context'
import { useFetch } from '../../../utils/hooks'
import validationDefs from './partners/validations'
import { shouldUpdateSectionRoot } from '../../../utils/misc'

const { Item } = Form

const Partners = ({ removeSetItem, fields, headerMore, currency, headerMoreField, primaryOrganisation, validations, showRequired, errors }) => { // eslint-disable-line
  const [{ results }, loading] = useFetch('/typeaheads/organisations')
  const { t } = useTranslation()
  const validationSets = getValidationSets(validations, validationDefs)
  const fieldExists = doesFieldExist(validationSets)
  const removeItem = (index, fields) => { // eslint-disable-line
    removeSetItem(3, 'partners', index)
    fields.remove(index)
  }
  const currencySymbol = getSymbolFromCurrency(currency)
  const currencyRegExp = new RegExp(`\\${currencySymbol}\\s?|(,*)`, 'g')
  const ROLE_OPTIONS = [
    { value: 2, label: t('Accountable partner') },
    { value: 3, label: t('Extending partner') },
    { value: 1, label: t('Funding partner') },
    { value: 4, label: t('Implementing partner') },
    { value: 101, label: t('Reporting organisation') },
    { value: 100, label: t('Sponsor partner') },
  ]
  return (
    <div className="partners view">
      <div className="min-required-wrapper">
        <h3>{t('Project partners')}</h3>
        {showRequired && errors.findIndex(it => it.type === 'min' && it.path === 'partners') !== -1 && (
          <span className="min-required">{t('Minimum one required')}</span>
        )}
      </div>
      <SectionContext.Provider value="section3">
        <Form layout="vertical">
          <FinalForm
            onSubmit={() => { }}
            initialValues={fields}
            subscription={{}}
            mutators={{ ...arrayMutators }}
            render={(renderProps) => {
              const {
                form: {
                  mutators: { push }
                }
              } = renderProps
              const { values } = renderProps.form.getState()
              const reportingOrgs = values.partners.filter(it => it.iatiOrganisationRole === 101)
              const reportingOrgIndex = values.partners.findIndex(it => it.iatiOrganisationRole === 101)
              return (
                <ItemArray
                  setName="partners"
                  sectionIndex={3}
                  header={(index, role) => {
                    return (
                      <Field name={`partners[${index}].organisationName`} subscription={{ value: true }}>
                        {({ input }) => {
                          return (
                            <span>{role ? `${ROLE_OPTIONS.find(it => it.value === role).label}: ${input.value}` : `Partner ${index + 1}`}</span>
                          )
                        }}
                      </Field>
                    )
                  }}
                  renderExtra={(name, index, fields) => { // eslint-disable-line
                    return (
                      <Field
                        name={`${name}.iatiOrganisationRole`}
                        render={(roleProps) => {
                          const disabled = (roleProps.input.value === 101 && primaryOrganisation === 3394) || (reportingOrgs.length === 1 && index === reportingOrgIndex)
                          if (!disabled) {
                            return (
                              <span onClick={event => event.stopPropagation()}>{/* eslint-disable-line */}
                                {headerMore && <PanelHeaderMore render={headerMore} field={headerMoreField} name={name} index={index} />}
                                <Popconfirm
                                  title={t('Are you sure to delete this?')}
                                  onConfirm={() => removeItem(index, fields)}
                                  okText={t('Yes')}
                                  cancelText={t('No')}
                                >
                                  <Button size="small" icon="delete" className="delete-panel" />
                                </Popconfirm>
                              </span>
                            )
                          }
                          return null
                        }}
                      />
                    )
                  }}
                  headerField="iatiOrganisationRole"
                  formPush={push}
                  panel={(name, index) => (
                    <div>
                      <Field
                        name={`${name}.iatiOrganisationRole`}
                        render={roleProps => {
                          const disabled = roleProps.input.value === 101 && primaryOrganisation === 3394
                          return (
                            <div>
                              <FinalField
                                name={`${name}.iatiOrganisationRole`}
                                control="select"
                                options={ROLE_OPTIONS}
                                disabled={disabled || (reportingOrgs.length === 1 && index === reportingOrgIndex)}
                                withLabel
                              />
                              <OrganizationSelect
                                name={name}
                                orgs={results}
                                loading={loading}
                                disabled={disabled}
                                dict={{
                                  label: t('project partner'),
                                  tooltip: t('Select an organisation that is playing a role in the project. If an organisation is not currently featured in RSR, please contact <a href=\'mailto: <EMAIL>\'><EMAIL></a> to request to add it to the database.')
                                }}
                              />
                              <Condition when={`${name}.iatiOrganisationRole`} is={101}>
                                <Item label={<InputLabel tooltip={t('section3::secondaryReporter::tooltip')} optional>{t('section3::secondaryReporter::label')}</InputLabel>}>
                                  <FinalField
                                    name={`${name}.isSecondaryReporter`}
                                    render={({ input }) => (
                                      <Radio.Group {...input} disabled={disabled}>
                                        <Radio.Button value>{t('Yes')}</Radio.Button>
                                        <Radio.Button value={false}>{t('No')}</Radio.Button>
                                      </Radio.Group>
                                    )}
                                  />
                                </Item>
                              </Condition>
                              <Condition when={`${name}.iatiOrganisationRole`} is={1}>
                                <FinalField
                                  name={`${name}.fundingAmount`}
                                  control="input-number"
                                  formatter={value => `${currencySymbol} ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
                                  parser={value => value.replace(currencyRegExp, '')}
                                  style={{ width: 200 }}
                                  withLabel
                                />
                              </Condition>
                              <FinalField
                                name={`${name}.iatiActivityId`}
                                disabled={disabled}
                                withLabel
                                optional
                                fieldExists={fieldExists}
                                control="input"
                              />
                            </div>
                          )
                        }}
                      />
                    </div>
                  )}
                  addButton={({ onClick }) => (
                    <Button className="bottom-btn" icon="plus" type="dashed" block onClick={onClick}>{t('Add another participating organisation')}</Button>
                  )}
                />
              )
            }}
          />
        </Form>
      </SectionContext.Provider>
    </div>
  )
}

export default connect(
  ({ editorRdr: { validations, showRequired, section3: { fields, errors }, section1: { fields: { currency, primaryOrganisation } } } }) => ({ validations, currency, fields, primaryOrganisation, showRequired, errors }),
  { removeSetItem }
)(withTranslation()(React.memo(Partners, shouldUpdateSectionRoot)))
{'fraction_non_alphanumeric': 0.12705667276051189, 'fraction_numerical': 0.005269383804710184, 'mean_word_length': 1.2136396096167579, 'pattern_counts': {'":': 0, '<': 113, '<?xml version=': 0, '>': 153, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 44, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3516943', 'n_tokens_mistral': 4356, 'n_tokens_neox': 4144, 'n_words': 851}