import * as React from "react"; import { ComplaintData } from "../interfaces"; export interface ReportProblemFormProps { reportUrl: string; report: (url: string, data: ComplaintData) => Promise; fetchTypes: (url: string) => Promise; close: () => void; types: string[]; } export interface ReportProblemFormState { submitted: boolean; error?: string; } export default class ReportProblemForm extends React.Component { constructor(props) { super(props); this.state = { submitted: false, error: null }; this.submit = this.submit.bind(this); } render() { let title = this.state.submitted ? "Problem Reported" : "Report a Problem"; return (

{title}

{ this.state.error &&
{this.state.error}
} { !this.state.submitted && this.props.types.length > 0 &&


 
} { this.state.submitted &&
}
); } componentWillMount() { this.props.fetchTypes(this.props.reportUrl); } displayType(type) { return type .replace("http://librarysimplified.org/terms/problem/", "") .replace(/-/g, " ") .split(" ") .map(t => t[0].toUpperCase() + t.slice(1)) .join(" "); } submit() { if (this.typeSelected()) { let data = { type: (this.refs as any).type.value, detail: (this.refs as any).detail.value }; return this.props.report(this.props.reportUrl, data).then(() => { this.setState({ submitted: true, error: null }); }).catch(err => { this.setState({ ...this.state, error: "There was an error posting this problem" }); }); } else { this.setState({ ...this.state, error: "You must select a type" }); } } typeSelected() { return !!(this.refs as any).type.value; } }