import { Component } from 'react'; import Spinner from '../components/Spinner/Spinner'; import UploadUnit from '../components/UploadUnit/UploadUnit'; import { post } from '../helper/axios'; import { apis } from '../helper/apis'; import { alert } from '../helper/alert'; import { images } from '../resources.json'; import './upload.css'; export class UploadContainer extends Component { constructor(props) { super(props); this.state = { file: null, msg: "", url: "", submitting: false }; this.handleChange = this.handleChange.bind(this); this.handleUploadError = this.handleUploadError.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } handleChange(e) { const { files } = e.target; if (files.length > 0) this.setState({ file: files[0] }); } handleUploadError(msg) { this.setState({ submitting: false }); } handleSubmit() { if ((this.state.msg === "" && this.file === null) || this.state.submitting) return; this.setState({ submitting: true }); } componentDidUpdate() { if (this.state.submitting && (!this.state.file || this.state.url !== "")) { // upload using axios post(apis.submitMessage, { content: this.state.msg, image: this.state.url }) .then(({ data, status, networkStatus }) => { if (networkStatus !== 200) return; if (!status) return alert('提交内容失败:' + data); this.setState({ submitting: false, msg: "", url: "", file: null }); alert('内容提交成功啦').then(({ isConfirmed }) => { if (isConfirmed) window.close(); }); }); } } render() { return (