import React,{ Component } from "react"; import { Form, Input, InputNumber, Switch, Radio, Slider, Button, Upload, Icon, Rate, Checkbox, message, Row, Col, Select, Modal, Divider,Tooltip } from 'antd'; import TPMMDEditor from '../../tpm/challengesnew/TPMMDEditor'; import axios from 'axios' import './board.css' import "../common/formCommon.css" import AddDirModal from './AddDirModal' import { RouteHOC } from './common.js' import CBreadcrumb from '../common/CBreadcrumb' import {getUploadActionUrl, bytesToSize, uploadNameSizeSeperator, appendFileSizeToUploadFile, appendFileSizeToUploadFileAll} from 'educoder'; const confirm = Modal.confirm; const $ = window.$ const { Option } = Select; const MAX_TITLE_LENGTH = 60 // https://lanhuapp.com/web/#/item/project/board/detail?pid=a3bcd4b1-99ce-4e43-8ead-5b8b0a410807&project_id=a3bcd4b1-99ce-4e43-8ead-5b8b0a410807&image_id=71072679-b925-4824-aceb-4649535e3652 class BoardsNew extends Component{ constructor(props){ super(props); this.mdRef = React.createRef(); this.state = { fileList: [], boards: [], title_num: 0, email_notify:false, isemail_notify:false, isemail_notifys:false } } addSuccess = () => { this.fetchBoards() } fetchBoards = () => { const isEdit = this.isEdit const boardId = this.props.match.params.boardId const boardsUrl = `/courses/board_list.json?board_id=${boardId}` axios.get(boardsUrl, { }) .then((response) => { if (response.data.status == 0) { this.setState({ boards: response.data.data.boards || [], boardsdata:response.data.data, course_id: response.data.data.course_id }) if (!isEdit) { response.data.data.boards.forEach( board => { if (board.id == boardId) { this.setState({ board_name: board.name }) } }) // board_name } } }) .catch(function (error) { console.log(error); }); } componentDidMount = () => { const topicId = this.props.match.params.topicId const isEdit = !!topicId this.isEdit = isEdit const boardId = this.props.match.params.boardId this.fetchBoards() if (isEdit) { const url = `/messages/${topicId}.json` axios.get(url, { }) .then((response) => { if (response.data.status == 0) { const { id, data } = response.data; if (data) { this.editTopic = data; this.props.form.setFieldsValue({ sticky: !!data.sticky, content: data.content, subject: data.subject, select_board_id: data.board_id // TODO 没返回给前端 }); this.mdRef.current.setValue(data.content) const _fileList = data.attachments.map(item => { return { id: item.id, uid: item.id, name: appendFileSizeToUploadFile(item), url: item.url, status: 'done' } }) this.setState({ fileList: _fileList, board_name: data.board_name, title_num: parseInt(data.subject.length) ,isemail_notifys:response.data.data.email_notify}) } } }) .catch(function (error) { console.log(error); }); } else { const boardId = this.props.match.params.boardId this.props.form.setFieldsValue({ select_board_id: parseInt(boardId) }); } } handleSubmit = (e) => { e.preventDefault(); const cid = this.state.course_id const boardId = this.props.match.params.boardId this.props.form.validateFieldsAndScroll((err, values) => { if (!err) { console.log('Received values of form: ', values); if (this.isEdit == true) { const editTopic = this.editTopic const editUrl = `/messages/${editTopic.id}.json` let attachment_ids = undefined if (this.state.fileList) { attachment_ids = this.state.fileList.map(item => { return item.response ? item.response.id : item.id }) } axios.put(editUrl, { subject: values.subject, select_board_id: values.select_board_id, content: values.content, sticky: values.sticky, email_notify:this.state.isemail_notify, attachment_ids, }) .then((response) => { if (response.data.status == 0) { const { id } = response.data; console.log('--- success') this.props.toDetailPage(cid, values.select_board_id, editTopic.id) } }) .catch(function (error) { console.log(error); }); } else { const url = `/boards/${boardId}/messages.json` let attachment_ids = undefined if (this.state.fileList) { attachment_ids = this.state.fileList.map(item => { return item.response.id }) } axios.post(url, { ...values, email_notify:this.state.isemail_notify, course_id: cid, attachment_ids, }) .then((response) => { if (response.data.data && response.data.status == 0) { const { id } = response.data.data; if (id) { console.log('--- success') this.props.toDetailPage(cid, values.select_board_id, id) } } }) .catch(function (error) { console.log(error); }); } } else { $("html").animate({ scrollTop: $('html').scrollTop() - 100 }) } }); } // 附件相关 START handleChange = (info) => { if (info.file.status === 'uploading' || info.file.status === 'done' || info.file.status === 'removed') { let fileList = info.fileList; this.setState({ fileList: appendFileSizeToUploadFileAll(fileList) }); } } onAttachmentRemove = (file) => { if(!file.percent || file.percent == 100){ this.props.confirm({ // title: '确定要删除这个附件吗?', content: '是否确认删除?', okText: '确定', cancelText: '取消', // content: 'Some descriptions', onOk: () => { this.deleteAttachment(file) }, onCancel() { console.log('Cancel'); }, }); return false; } } deleteAttachment = (file) => { // 初次上传不能直接取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.fileList.indexOf(file); const newFileList = state.fileList.slice(); newFileList.splice(index, 1); return { fileList: newFileList, }; }); } } }) .catch(function (error) { console.log(error); }); } // 附件相关 ------------ END changeTitle=(e)=>{ console.log(e.target.value.length); this.setState({ title_num: parseInt(e.target.value.length) }) } goBack = () => { // this.props.history.goBack() const courseId=this.props.match.params.coursesId; const boardId = this.props.match.params.boardId this.props.toListPage(courseId, boardId) } setemailchange=(e)=>{ this.setState({ isemail_notify:e.target.checked }) } render() { let { addGroup, fileList, course_id, title_num } = this.state; const { getFieldDecorator } = this.props.form; const { current_user } = this.props const formItemLayout = { labelCol: { xs: { span: 24 }, // sm: { span: 8 }, sm: { span: 24 }, }, wrapperCol: { xs: { span: 24 }, // sm: { span: 16 }, sm: { span: 24 }, }, }; const uploadProps = { width: 600, fileList, multiple: true, // https://github.com/ant-design/ant-design/issues/15505 // showUploadList={false},然后外部拿到 fileList 数组自行渲染列表。 // showUploadList: false, action: `${getUploadActionUrl()}`, onChange: this.handleChange, onRemove: this.onAttachmentRemove, beforeUpload: (file) => { console.log('beforeUpload', file.name); const isLt150M = file.size / 1024 / 1024 < 150; if (!isLt150M) { this.props.showNotification('文件大小必须小于150MB!'); } return isLt150M; }, }; const isAdmin = this.props.isAdmin() const courseId=this.props.match.params.coursesId; const boardId = this.props.match.params.boardId const isCourseEnd = this.props.isCourseEnd(); document.title=this.props.coursedata&&this.props.coursedata.name; // console.log(this.state) return(
) } } const WrappedBoardsNew = Form.create({ name: 'boardsNew' })(BoardsNew); export default RouteHOC()(WrappedBoardsNew);