import React, { PureComponent, ChangeEvent } from 'react' import axios from 'axios' import { Logger } from '@oceanprotocol/squid' import Modal from '../../atoms/Modal' import styles from './Report.module.scss' import Button from '../../atoms/Button' import Input from '../../atoms/Form/Input' import Form from '../../atoms/Form/Form' import { serviceUri } from '../../../config' import Spinner from '../../atoms/Spinner' export default class Report extends PureComponent< { did: string; title: string }, { isModalOpen: boolean comment: string message: string isSending: boolean hasError?: boolean hasSuccess?: boolean } > { public state = { isModalOpen: false, comment: '', message: 'Sending...', isSending: false, hasError: false, hasSuccess: false } // for canceling axios requests public signal = axios.CancelToken.source() public componentWillUnmount() { this.signal.cancel() } private handleInputChange = (event: ChangeEvent) => { this.setState({ comment: event.target.value }) } private handleToggleModal = () => { this.setState({ isModalOpen: !this.state.isModalOpen }) this.state.isModalOpen && this.reset() } private reset() { this.setState({ comment: '', message: 'Sending...', isSending: false, hasError: false, hasSuccess: false }) } private sendEmail = async (event: Event) => { event.preventDefault() this.setState({ isSending: true }) const msg = { to: process.env.REACT_APP_REPORT_EMAIL, from: 'info@oceanprotocol.com', subject: `[Report] ${this.props.title}`, html: `

The following data set was reported:

${this.props.title}
${this.props.did}

${this.state.comment}
` } try { const response = await axios({ method: 'POST', headers: { 'Content-Type': 'application/json' }, url: `${serviceUri}/api/v1/report`, data: { msg }, cancelToken: this.signal.token }) this.setState({ isSending: false, hasSuccess: true, message: 'Thanks for the report! We will take a look soon.' }) return response.data.result } catch (error) { !axios.isCancel(error) && this.setState({ message: error.message, isSending: false, hasError: true }) && Logger.error(error.message) } } public render() { return (

{this.props.title}

{this.props.did}

{this.state.isSending ? ( ) : this.state.hasError ? (
{this.state.message}
) : this.state.hasSuccess ? (
{this.state.message}
) : (
)}
) } }