<commit_before>import { Button } from "@artsy/palette"
import { Schema, screenTrack, track } from "lib/utils/track"
import React from "react"
import { commitMutation, graphql, RelayProp } from "react-relay"
import { PayloadError } from "relay-runtime"

interface Props {
  saleArtworkID: string
  relay?: RelayProp
}

interface State {
  requestingConditionReport: boolean
  showConditionReportRequestedModal: boolean
  showErrorModal: boolean
  errorModalText: string
}

@screenTrack({
  context_screen: Schema.PageNames.RequestConditionReportPage,
  context_screen_owner_type: null,
})
export class RequestConditionReport extends React.Component<Props, State> {
  state = {
    requestingConditionReport: false,
    showConditionReportRequestedModal: false,
    showErrorModal: false,
    errorModalText: "",
  }

  requestConditionReport = () => {
    const { saleArtworkID, relay } = this.props
    return new Promise(async (resolve, reject) => {
      commitMutation<RequestConditionReportMutation>(relay.environment, {
        onCompleted: data => {
          resolve(data)
        },
        onError: error => {
          reject(error)
        },
        mutation: graphql`
          mutation RequestConditionReportMutation($input: RequestConditionReportInput!) {
            requestConditionReport(input: $input) {
              conditionReportRequest {
                internalID
              }
            }
          }
        `,
        variables: {
          input: { saleArtworkID },
        },
      })
    })
  }

  // TODO: add tracking
  presentErrorModal(errors: Error | ReadonlyArray<PayloadError>, mutationMessage: string) {
    console.error("RequestConditionReport.tsx", errors)

    const errorMessage = mutationMessage || "There was a problem processing your request. Please try again."
    this.setState({ showErrorModal: true, errorModalText: errorMessage, requestingConditionReport: false })
  }

  // TODO: add tracking
  requestConditionReportSuccess = () => {
    this.setState({ showConditionReportRequestedModal: true, requestingConditionReport: false })
  }

  handleRequestConditionReportClick = () => {
    this.setState({ requestingConditionReport: true })

    this.requestConditionReport()
      .then(data => {
        const theData = data as any
        if (theData.requestConditionReport) {
          this.requestConditionReportSuccess()
        } else {
          this.presentErrorModal(null, null)
        }
      })
      .catch(error => {
        this.presentErrorModal(error, null)
      })
  }

  render() {
    return (
      <Button mt={1} variant="secondaryGray" size="small" onPress={this.handleRequestConditionReportClick}>
        Request condition report
      </Button>
    )
  }
}<commit_msg>Add error and success modals for condition report<commit_after>import { Button } from "@artsy/palette"
import { Modal } from "lib/Components/Modal"
import { Schema, screenTrack, track } from "lib/utils/track"
import React from "react"
import { View } from "react-native"
import { commitMutation, graphql, RelayProp } from "react-relay"
import { PayloadError } from "relay-runtime"

interface Props {
  saleArtworkID: string
  relay?: RelayProp
}

interface State {
  requestingConditionReport: boolean
  showConditionReportRequestedModal: boolean
  showErrorModal: boolean
  errorModalText: string
}

@screenTrack({
  context_screen: Schema.PageNames.RequestConditionReportPage,
  context_screen_owner_type: null,
})
export class RequestConditionReport extends React.Component<Props, State> {
  state = {
    requestingConditionReport: false,
    showConditionReportRequestedModal: false,
    showErrorModal: false,
    errorModalText: "",
  }

  requestConditionReport = () => {
    const { saleArtworkID, relay } = this.props
    return new Promise(async (resolve, reject) => {
      commitMutation<RequestConditionReportMutation>(relay.environment, {
        onCompleted: data => {
          resolve(data)
        },
        onError: error => {
          reject(error)
        },
        mutation: graphql`
          mutation RequestConditionReportMutation($input: RequestConditionReportInput!) {
            requestConditionReport(input: $input) {
              conditionReportRequest {
                internalID
              }
            }
          }
        `,
        variables: {
          input: { saleArtworkID },
        },
      })
    })
  }

  // TODO: add tracking
  presentErrorModal(errors: Error | ReadonlyArray<PayloadError>, mutationMessage: string) {
    console.error("RequestConditionReport.tsx", errors)

    const errorMessage = mutationMessage || "There was a problem processing your request. Please try again."
    this.setState({ showErrorModal: true, errorModalText: errorMessage, requestingConditionReport: false })
  }

  // TODO: add tracking
  requestConditionReportSuccess = () => {
    this.setState({ showConditionReportRequestedModal: true, requestingConditionReport: false })
  }

  handleRequestConditionReportClick = () => {
    this.setState({ requestingConditionReport: true })

    this.requestConditionReport()
      .then(data => {
        const theData = data as any
        if (theData.requestConditionReport) {
          this.requestConditionReportSuccess()
        } else {
          this.presentErrorModal(null, null)
        }
      })
      .catch(error => {
        this.presentErrorModal(error, null)
      })
  }

  closeModals() {
    this.setState({ showConditionReportRequestedModal: false, showErrorModal: false })
  }

  render() {
    const { requestingConditionReport, showErrorModal, errorModalText, showConditionReportRequestedModal } = this.state

    return (
      <View>
        <Button
          mt={1}
          size="small"
          variant="secondaryGray"
          loading={requestingConditionReport}
          onPress={this.handleRequestConditionReportClick}
        >
          Request condition report
        </Button>

        <Modal
          visible={showErrorModal}
          headerText="An error occurred"
          detailText={errorModalText}
          closeModal={this.closeModals.bind(this)}
        />

        <Modal
          visible={showConditionReportRequestedModal}
          headerText="Condition Report Requested"
          detailText="We have received your request. The condition report will be sent to."
          closeModal={this.closeModals.bind(this)}
        />
      </View>
    )
  }
}
{'fraction_non_alphanumeric': 0.09655386525923626, 'fraction_numerical': 0.0003104625892579944, 'mean_word_length': 2.897761645493043, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 33, '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': '5561851', 'n_tokens_mistral': 1713, 'n_tokens_neox': 1685, 'n_words': 371}