import React,{ Component } from "react"; import { Input, InputNumber, Form, Button, Checkbox, Upload, Icon, message, Modal } from "antd"; import axios from 'axios' import { WordsBtn, getUrl, ConditionToolTip, appendFileSizeToUploadFile, appendFileSizeToUploadFileAll } from 'educoder' import TPMMDEditor from '../../../tpm/challengesnew/TPMMDEditor'; const $ = window.$; const MAX_TITLE_LENGTH = 60; class NewGtaskForms extends Component{ constructor(props){ super(props); this.contentMdRef = React.createRef(); this.state={ title_num:0, description:"", contentFileList: [], } } initValue = (data) => { const contentFileList = data.attachments.map(item => { return { id: item.id, uid: item.id, name: appendFileSizeToUploadFile(item), url: item.url, filesize: item.filesize, status: 'done' } }) this.setState({ ...data, base_on_project: data.group_info.base_on_project, title_num: parseInt(data.name.length), min_num: data.group_info.min_number, max_num: data.group_info.max_number, contentFileList, }, () => { setTimeout(() => { this.contentMdRef.current.setValue(data.description || '') }, 2000) this.props.form.setFieldsValue({ title: data.name, description: data.description || '', }); }) } // 输入title changeTitle=(e)=>{ console.log(e.target.value.length); this.setState({ title_num: parseInt(e.target.value.length) }) } handleContentUploadChange = (info) => { let contentFileList = info.fileList; this.setState({ contentFileList: appendFileSizeToUploadFileAll(contentFileList) }); } deleteAttachment = (file, stateName) => { // 初次上传不能直接取uid const url = `/attachments/${file.response ? file.response.id : file.uid}.json` axios.delete(url, { }) .then((response) => { if (response.data) { const { status } = response.data; if (status == 0) { console.log('--- success') this.setState((state) => { const index = state[stateName].indexOf(file); const newFileList = state[stateName].slice(); newFileList.splice(index, 1); return { [stateName]: newFileList, }; }); } } }) .catch(function (error) { console.log(error); }); } onAttachmentRemove = (file, stateName) => { if(file.response!=undefined){ this.props.confirm({ content: '是否确认删除?', onOk: () => { this.deleteAttachment(file, stateName) }, onCancel() { console.log('Cancel'); }, }); return false; } } handleSubmit = () => { debugger let {contentFileList,min_num,max_num,base_on_project}=this.state; let {data}=this.props; let task_type=data.task_type let topicId=this.props.topicId this.props.form.validateFields((err, values) => { const mdContnet = this.contentMdRef.current.getValue().trim(); values.description = mdContnet; if (!err) { let url="/task_banks/"+topicId+".json"; axios.put(url, { gtask_bank: { name: values.title, description: values.description, min_num:task_type===1?undefined:min_num, max_num:task_type===1?undefined:max_num, base_on_project: task_type===1?undefined:base_on_project===true?1:0 }, attachment_ids:contentFileList } ).then((response) => { if(response.data.status===0){ this.props.showNotification(response.data.message) }else{ this.props.showNotification(response.data.message) } }).catch((error) => { console.log(error) }) } else { $("html").animate({ scrollTop: $('html').scrollTop() - 100 }) } }) } max_num_change = (val) => { if (val < 2) { this.setState({ max_num: 2, }) return; } const { min_num } = this.state; this.setState({ max_num: val, min_num: val <= min_num ? val - 1 : min_num }) } min_num_change = (val) => { this.setState({ min_num: val }) } base_on_project_change = () => { this.setState({ base_on_project: !this.state.base_on_project }) } render(){ const { getFieldDecorator } = this.props.form; let{ title_value, contentFileList, answerFileList, max_num, min_num, base_on_project, init_max_num, init_min_num, title_num, course_name, category, has_commit, has_project, isEdit }=this.state const uploadProps = { width: 600, fileList: contentFileList, multiple: true, // https://github.com/ant-design/ant-design/issues/15505 // showUploadList={false},然后外部拿到 fileList 数组自行渲染列表。 // showUploadList: false, action: `${getUrl()}/api/attachments.json`, onChange: this.handleContentUploadChange, onRemove: (file) => this.onAttachmentRemove(file, 'contentFileList'), beforeUpload: (file) => { console.log('beforeUpload', file.name); const isLt150M = file.size / 1024 / 1024 < 150; if (!isLt150M) { message.error('文件大小必须小于150MB!'); } return isLt150M; }, }; return(