<commit_before>import API from "api"
import { gql } from "apollo-boost"
import { PersonSimpleOverlayRow } from "components/advancedSelectWidget/AdvancedSelectOverlayRow"
import AdvancedSingleSelect from "components/advancedSelectWidget/AdvancedSingleSelect"
import LinkTo from "components/LinkTo"
import Messages from "components/Messages"
import Model from "components/Model"
import _isEmpty from "lodash/isEmpty"
import _isEqualWith from "lodash/isEqualWith"
import { Person, Position } from "models"
import PropTypes from "prop-types"
import React, { useCallback, useEffect, useRef, useState } from "react"
import {
  Button,
  Col,
  FormGroup,
  Grid,
  Modal,
  Row,
  Table
} from "react-bootstrap"
import PEOPLE_ICON from "resources/people.png"
import Settings from "settings"
import utils from "utils"

const GQL_DELETE_PERSON_FROM_POSITION = gql`
  mutation($uuid: String!) {
    deletePersonFromPosition(uuid: $uuid)
  }
`

const GQL_PUT_PERSON_IN_POSITION = gql`
  mutation($uuid: String!, $person: PersonInput!) {
    putPersonInPosition(uuid: $uuid, person: $person)
  }
`

const AssignPersonModal = ({ position, showModal, onCancel, onSuccess }) => {
  const latestPositionProp = useRef(position)
  const positionPropUnchanged = _isEqualWith(
    latestPositionProp.current,
    position,
    utils.treatFunctionsAsEqual
  )

  const [error, setError] = useState(null)
  const [person, setPerson] = useState(position && position.person)
  const [doSave, setDoSave] = useState(false)
  const [removeUser, setRemoveUser] = useState(false)

  const save = useCallback(() => {
    let graphql, variables
    if (person === null) {
      graphql = GQL_DELETE_PERSON_FROM_POSITION
      variables = {
        uuid: position.uuid
      }
    } else {
      graphql = GQL_PUT_PERSON_IN_POSITION
      variables = {
        uuid: position.uuid,
        person: { uuid: person.uuid }
      }
    }
    API.mutation(graphql, variables)
      .then(data => {
        onSuccess()
      })
      .catch(error => {
        setError(error)
      })
  }, [position, person, onSuccess])

  useEffect(() => {
    if (!positionPropUnchanged) {
      latestPositionProp.current = position
      setPerson(position && position.person)
    }
  }, [positionPropUnchanged, position])

  useEffect(() => {
    if (doSave) {
      setDoSave(false)
      save()
    }
  }, [doSave, save])

  useEffect(() => {
    let newError = null
    if (
      !_isEmpty(person) &&
      !_isEmpty(person.position) &&
      person.position.uuid !== position.uuid
    ) {
      const errorMessage = (
        <>
          <b>{`${person.rank} ${person.name}`}</b> is currently assigned to the{" "}
          <b>{person.position.name}</b> position. By selecting them, the{" "}
          <b>{person.position.name}</b> position will be left unfilled
          {person.position.type !== Position.TYPE.ADVISOR &&
          person.position.type !== Position.TYPE.PRINCIPAL ? (
            <>
                {" "}
                and the position's permissions will be converted from{" "}
                <b>{Position.convertType(person.position.type)}</b> to{" "}
                <b>{Settings.fields.advisor.position.type}</b>.
              </>
            ) : (
              <>.</>
            )}
          {position.type !== person.position.type && (
            <>
              {" "}
              Furthermore, permissions of the <b>{position.name}</b> position
              will be converted from{" "}
              <b>{Position.convertType(position.type)}</b> to{" "}
              <b>{Position.convertType(person.position.type)}</b>.
            </>
          )}
        </>
      )
      newError = { message: errorMessage }
    } else if (
      !Position.isAdvisor(latestPositionProp.current) &&
      !Position.isPrincipal(latestPositionProp.current) &&
      (removeUser || !person)
    ) {
      const errorMessage = (
        <>
          If you save, permissions of the <b>{position.name}</b> position will
          be converted from <b>{Position.convertType(position.type)}</b> to{" "}
          <b>{Settings.fields.advisor.position.type}</b>.<commit_msg>Reformat code using latest indentation rules<commit_after>import API from "api"
import { gql } from "apollo-boost"
import { PersonSimpleOverlayRow } from "components/advancedSelectWidget/AdvancedSelectOverlayRow"
import AdvancedSingleSelect from "components/advancedSelectWidget/AdvancedSingleSelect"
import LinkTo from "components/LinkTo"
import Messages from "components/Messages"
import Model from "components/Model"
import _isEmpty from "lodash/isEmpty"
import _isEqualWith from "lodash/isEqualWith"
import { Person, Position } from "models"
import PropTypes from "prop-types"
import React, { useCallback, useEffect, useRef, useState } from "react"
import {
  Button,
  Col,
  FormGroup,
  Grid,
  Modal,
  Row,
  Table
} from "react-bootstrap"
import PEOPLE_ICON from "resources/people.png"
import Settings from "settings"
import utils from "utils"

const GQL_DELETE_PERSON_FROM_POSITION = gql`
  mutation($uuid: String!) {
    deletePersonFromPosition(uuid: $uuid)
  }
`

const GQL_PUT_PERSON_IN_POSITION = gql`
  mutation($uuid: String!, $person: PersonInput!) {
    putPersonInPosition(uuid: $uuid, person: $person)
  }
`

const AssignPersonModal = ({ position, showModal, onCancel, onSuccess }) => {
  const latestPositionProp = useRef(position)
  const positionPropUnchanged = _isEqualWith(
    latestPositionProp.current,
    position,
    utils.treatFunctionsAsEqual
  )

  const [error, setError] = useState(null)
  const [person, setPerson] = useState(position && position.person)
  const [doSave, setDoSave] = useState(false)
  const [removeUser, setRemoveUser] = useState(false)

  const save = useCallback(() => {
    let graphql, variables
    if (person === null) {
      graphql = GQL_DELETE_PERSON_FROM_POSITION
      variables = {
        uuid: position.uuid
      }
    } else {
      graphql = GQL_PUT_PERSON_IN_POSITION
      variables = {
        uuid: position.uuid,
        person: { uuid: person.uuid }
      }
    }
    API.mutation(graphql, variables)
      .then(data => {
        onSuccess()
      })
      .catch(error => {
        setError(error)
      })
  }, [position, person, onSuccess])

  useEffect(() => {
    if (!positionPropUnchanged) {
      latestPositionProp.current = position
      setPerson(position && position.person)
    }
  }, [positionPropUnchanged, position])

  useEffect(() => {
    if (doSave) {
      setDoSave(false)
      save()
    }
  }, [doSave, save])

  useEffect(() => {
    let newError = null
    if (
      !_isEmpty(person) &&
      !_isEmpty(person.position) &&
      person.position.uuid !== position.uuid
    ) {
      const errorMessage = (
        <>
          <b>{`${person.rank} ${person.name}`}</b> is currently assigned to the{" "}
          <b>{person.position.name}</b> position. By selecting them, the{" "}
          <b>{person.position.name}</b> position will be left unfilled
          {person.position.type !== Position.TYPE.ADVISOR &&
          person.position.type !== Position.TYPE.PRINCIPAL ? (
            <>
              {" "}
              and the position's permissions will be converted from{" "}
              <b>{Position.convertType(person.position.type)}</b> to{" "}
              <b>{Settings.fields.advisor.position.type}</b>.
            </>
            ) : (
              <>.</>
            )}
          {position.type !== person.position.type && (
            <>
              {" "}
              Furthermore, permissions of the <b>{position.name}</b> position
              will be converted from{" "}
              <b>{Position.convertType(position.type)}</b> to{" "}
              <b>{Position.convertType(person.position.type)}</b>.
            </>
          )}
        </>
      )
      newError = { message: errorMessage }
    } else if (
      !Position.isAdvisor(latestPositionProp.current) &&
      !Position.isPrincipal(latestPositionProp.current) &&
      (removeUser || !person)
    ) {
      const errorMessage = (
        <>
          If you save, permissions of the <b>{position.name}</b> position will
          be converted from <b>{Position.convertType(position.type)}</b> to{" "}
          <b>{Settings.fields.advisor.position.type}</b>.
{'fraction_non_alphanumeric': 0.13049235584446853, 'fraction_numerical': 0.0, 'mean_word_length': 2.6631393298059964, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '965299', 'n_tokens_mistral': 2472, 'n_tokens_neox': 2433, 'n_words': 522}