import React,{ Component } from "react"; import { Input, InputNumber, Form, Button, Checkbox, Upload, Icon, message, Modal } from "antd"; import axios from 'axios' import '../css/busyWork.css' import '../css/Courses.css' import { WordsBtn, getUrl, ConditionToolTip, appendFileSizeToUploadFile, appendFileSizeToUploadFileAll } from 'educoder' import TPMMDEditor from '../../tpm/challengesnew/TPMMDEditor'; import CBreadcrumb from '../common/CBreadcrumb' const confirm = Modal.confirm; const $ = window.$ const MAX_TITLE_LENGTH = 60; class NewWork extends Component{ constructor(props){ super(props); this.contentMdRef = React.createRef(); this.answerMdRef = React.createRef(); this.state={ title_value:"", title_num: 0, contentFileList: [], answerFileList: [], workLoaded: false, base_on_project: true, category: {}, min_num: 2, max_num: 10, } } componentDidMount () { let {typeId, coursesId, pageType, workId}=this.props.match.params; const isEdit = pageType === "edit" this.isEdit = isEdit; if (isEdit) { this.fetchWork(workId) } else { this.fetchCourseData(coursesId) } } fetchCourseData = (courseId) => { const isGroup = this.props.isGroup() const url = `/courses/${courseId}/homework_commons/new.json?type=${isGroup ? 3 : 1}` axios.get(url, { }) .then((response) => { if (response.data.course_name) { const data = response.data; this.setState({ course_id: data.course_id, course_name: data.course_name, category: data.category, }) } }) .catch(function (error) { console.log(error); }); } fetchWork = (workId) => { const url = `/homework_commons/${workId}/edit.json` axios.get(url, { }) .then((response) => { if (response.data.name) { const data = response.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' } }) const answerFileList = data.ref_attachments.map(item => { return { id: item.id, uid: item.id, name: appendFileSizeToUploadFile(item), url: item.url, filesize: item.filesize, status: 'done' } }) this.setState({ ...data, // course_id: data.course_id, // course_name: data.course_name, // category: data.category, title_num: parseInt(data.name.length), workLoaded: true, init_min_num: data.min_num, init_max_num: data.max_num, // description: data.description, reference_answer: data.reference_answer, contentFileList, answerFileList, }, () => { setTimeout(() => { this.contentMdRef.current.setValue(data.description || '') this.answerMdRef.current.setValue(data.reference_answer || '') }, 2000) this.props.form.setFieldsValue({ title: data.name, description: data.description || '', reference_answer: data.reference_answer || '', }); }) } }) .catch(function (error) { console.log(error); }); } // 输入title changeTitle=(e)=>{ console.log(e.target.value.length); this.setState({ title_num: parseInt(e.target.value.length) }) } handleSubmit = () => { const courseId = this.state.course_id || this.props.match.params.coursesId ; this.props.form.validateFieldsAndScroll((err, values) => { console.log(values) const mdContnet = this.contentMdRef.current.getValue().trim(); console.log(mdContnet) values.description = mdContnet; // return; {/* max={has_commit ? init_min_num : null } */} {/* min={has_commit ? init_max_num : (min_num == undefined ? 2 : min_num + 1) } */} // 已有提交作品,人数范围只能扩大 const { has_commit, max_num, init_max_num, min_num, init_min_num } = this.state; if (has_commit) { if (max_num < init_max_num || min_num > init_min_num) { this.props.showNotification(`已有提交作品,人数范围只能扩大(原设置为:${init_min_num} - ${init_max_num})`) return; } } // const errKeys = Object.keys(err); // || errKeys.length == 1 && errKeys[0] == 'content' && mdContnet if (!err) { if (this.isEdit) { this.doEdit(courseId, values) } else { this.doNew(courseId, values) } } else { $("html").animate({ scrollTop: $('html').scrollTop() - 100 }) } }) } doEdit = (courseId, values) => { const workId = this.props.match.params.workId const newUrl = `/homework_commons/${workId}.json` let attachment_ids = this.state.contentFileList.map(item => { return item.response ? item.response.id : item.id }) let reference_attachment_ids = this.state.answerFileList.map(item => { return item.response ? item.response.id : item.id }) const { min_num, max_num, base_on_project, category } = this.state const isGroup = this.props.isGroup() axios.put(newUrl, { type: isGroup ? 3 : 1, name: values.title, description: values.description, reference_answer: values.reference_answer, attachment_ids, reference_attachment_ids, min_num, max_num, base_on_project }) .then((response) => { if (response.data.status == 0) { this.props.showNotification('保存成功') this.props.toWorkListPage(this.props.match.params, workId) } }) .catch(function (error) { console.log(error); }); } doNew = (courseId, values) => { const newUrl = `/courses/${courseId}/homework_commons.json` let attachment_ids = this.state.contentFileList.map(item => { return item.response ? item.response.id : item.id }) let reference_attachment_ids = this.state.answerFileList.map(item => { return item.response ? item.response.id : item.id }) const isGroup = this.props.isGroup() const { min_num, max_num, base_on_project, category } = this.state axios.post(newUrl, { type: isGroup ? 3 : 1, name: values.title, description: values.description, reference_answer: values.reference_answer, attachment_ids, reference_attachment_ids, min_num, max_num, base_on_project }) .then((response) => { if (response.data.status == 0) { this.props.showNotification('保存成功') this.props.toWorkListPage(this.props.match.params, response.data.homework_id) } }) .catch(function (error) { console.log(error); }); } handleContentUploadChange = (info) => { let contentFileList = info.fileList; this.setState({ contentFileList: appendFileSizeToUploadFileAll(contentFileList) }); } handleAnswerUploadChange = (info) => { let answerFileList = info.fileList; this.setState({ answerFileList: appendFileSizeToUploadFileAll(answerFileList) }); } onAttachmentRemove = (file, stateName) => { this.props.confirm({ content: '是否确认删除?', onOk: () => { this.deleteAttachment(file, stateName) }, onCancel() { console.log('Cancel'); }, }); return false; } 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); }); } 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(){ let {typeId,coursesId,pageType}=this.props.match.params; const { getFieldDecorator } = this.props.form; const isGroup = this.props.isGroup() const moduleName = !isGroup? "普通作业":"分组作业"; const moduleEngName = this.props.getModuleName() 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 }=this.state const { current_user } = this.props const courseId = this.state.course_id || this.props.match.params.coursesId ; const isEdit = this.isEdit; if ((isEdit == undefined || isEdit) && !this.state.workLoaded) { return '' } 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; }, }; const answerUploadProps = { width: 600, fileList: answerFileList, multiple: true, // https://github.com/ant-design/ant-design/issues/15505 // showUploadList={false},然后外部拿到 fileList 数组自行渲染列表。 // showUploadList: false, action: `${getUrl()}/api/attachments.json`, onChange: this.handleAnswerUploadChange, onRemove: (file) => this.onAttachmentRemove(file, 'answerFileList'), beforeUpload: (file) => { console.log('beforeUpload', file.name); const isLt150M = file.size / 1024 / 1024 < 150; if (!isLt150M) { message.error('文件大小必须小于150MB!'); } return isLt150M; }, }; return(
{this.isEdit ?"编辑":"新建"}{ moduleName } {/* history.goBack() this.props.toListPage(this.props.match.params, category.category_id)} */} this.props.history.goBack()}> 返回