<commit_before>import { Button } from "@artsy/palette"
import { RequestConditionReport_artwork } from "__generated__/RequestConditionReport_artwork.graphql"
import { RequestConditionReport_me } from "__generated__/RequestConditionReport_me.graphql"
import { RequestConditionReportMutation } from "__generated__/RequestConditionReportMutation.graphql"
import { RequestConditionReportQuery } from "__generated__/RequestConditionReportQuery.graphql"
import { Modal } from "lib/Components/Modal"
import { defaultEnvironment } from "lib/relay/createEnvironment"
import { Schema, track } from "lib/utils/track"
import React from "react"
import { View } from "react-native"
import { commitMutation, createFragmentContainer, graphql, QueryRenderer } from "react-relay"
import { PayloadError } from "relay-runtime"

interface RequestConditionReportProps {
  artwork: RequestConditionReport_artwork
  me: RequestConditionReport_me
}

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

@track()
export class RequestConditionReport extends React.Component<RequestConditionReportProps, State> {
  state = {
    requestingConditionReport: false,
    showConditionReportRequestedModal: false,
    showErrorModal: false,
    errorModalText: "",
  }

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

  @track(() => ({
    action_name: Schema.ActionNames.RequestConditionReport,
    action_type: Schema.ActionTypes.Fail,
    context_module: Schema.ContextModules.ArtworkDetails,
  }))
  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 })
  }

  @track(() => ({
    action_name: Schema.ActionNames.RequestConditionReport,
    action_type: Schema.ActionTypes.Success,
    context_module: Schema.ContextModules.ArtworkDetails,
  }))
  requestConditionReportSuccess() {
    this.setState({ showConditionReportRequestedModal: true, requestingConditionReport: false })
  }

  @track(() => ({
    action_name: Schema.ActionNames.RequestConditionReport,
    action_type: Schema.ActionTypes.Tap,
    context_module: Schema.ContextModules.ArtworkDetails,
  }))
  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 { me } = this.props
    const { requestingConditionReport, showErrorModal, errorModalText, showConditionReportRequestedModal } = this.state

    return (
      <View>
        <Button
          mt={1}
          size="small"
          variant="secondaryGray"
          loading={requestingConditionReport}
          onPress={this.handleRequestConditionReportClick.bind(this)}
        >
          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.\nThe condition report will be sent to ${me &&
            me.email}.\nFor questions contact specialist<EMAIL>.`}
          closeModal={this.closeModals.bind(this)}
        />
      </View>
    )
  }
}

export const RequestConditionReportQueryRenderer: React.FC<{
  artworkID: string
}> = ({ artworkID }) => {
  return (<commit_msg>Consolidate analytics in request condition report<commit_after>import { Button } from "@artsy/palette"
import { RequestConditionReport_artwork } from "__generated__/RequestConditionReport_artwork.graphql"
import { RequestConditionReport_me } from "__generated__/RequestConditionReport_me.graphql"
import { RequestConditionReportMutation } from "__generated__/RequestConditionReportMutation.graphql"
import { RequestConditionReportQuery } from "__generated__/RequestConditionReportQuery.graphql"
import { Modal } from "lib/Components/Modal"
import { defaultEnvironment } from "lib/relay/createEnvironment"
import { Schema, track } from "lib/utils/track"
import React from "react"
import { View } from "react-native"
import { commitMutation, createFragmentContainer, graphql, QueryRenderer } from "react-relay"
import { PayloadError } from "relay-runtime"

interface RequestConditionReportProps {
  artwork: RequestConditionReport_artwork
  me: RequestConditionReport_me
}

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

@track({
  action_name: Schema.ActionNames.RequestConditionReport,
  context_module: Schema.ContextModules.ArtworkDetails,
})
export class RequestConditionReport extends React.Component<RequestConditionReportProps, State> {
  state = {
    requestingConditionReport: false,
    showConditionReportRequestedModal: false,
    showErrorModal: false,
    errorModalText: "",
  }

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

  @track({
    action_type: Schema.ActionTypes.Fail,
  })
  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 })
  }

  @track({
    action_type: Schema.ActionTypes.Success,
  })
  presentSuccessModal() {
    this.setState({ showConditionReportRequestedModal: true, requestingConditionReport: false })
  }

  @track({
    action_type: Schema.ActionTypes.Tap,
  })
  handleRequestConditionReportTap() {
    this.setState({ requestingConditionReport: true })

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

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

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

    return (
      <View>
        <Button
          mt={1}
          size="small"
          variant="secondaryGray"
          loading={requestingConditionReport}
          onPress={this.handleRequestConditionReportTap.bind(this)}
        >
          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.\nThe condition report will be sent to ${me &&
            me.email}.\nFor questions contact <EMAIL>.`}
          closeModal={this.closeModals.bind(this)}
        />
      </View>
    )
  }
}

export const RequestConditionReportQueryRenderer: React.FC<{
  artworkID: string
}> = ({ artworkID }) => {
  return (
{'fraction_non_alphanumeric': 0.09526840653511563, 'fraction_numerical': 0.0002121790791427965, 'mean_word_length': 3.285, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 42, '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': '3225886', 'n_tokens_mistral': 2475, 'n_tokens_neox': 2455, 'n_words': 501}