LessonCreate.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import React, { Component } from 'react';
  2. import pathToRegexp from 'path-to-regexp';
  3. import { connect } from 'dva';
  4. import { routerRedux } from 'dva/router';
  5. import { Form, Modal, Card, Button, Input, Switch } from 'antd';
  6. import AXDragSortTable from '../../../components/AXDragSortTable';
  7. import Selector from '../../../components/AXTableSelector/Selector';
  8. import FooterToolbar from '../../../components/FooterToolbar';
  9. import { renderStatus, statusToBool, boolToStatus } from '../../../utils/utils';
  10. const fieldLabels = {
  11. code: '课编号',
  12. title: '课名称',
  13. digest: '课简述',
  14. status: '课状态',
  15. };
  16. const formItemLayout = {
  17. labelCol: {
  18. xs: { span: 24 },
  19. sm: { span: 2 },
  20. },
  21. wrapperCol: {
  22. xs: { span: 24 },
  23. sm: { span: 14 },
  24. md: { span: 12 },
  25. },
  26. };
  27. @connect(({ loading, lesson, courseware }) => ({
  28. lesson,
  29. courseware,
  30. loading: loading.models.courseware,
  31. submitting: loading.models.lesson,
  32. }))
  33. @Form.create()
  34. export default class LessonCreatePage extends Component {
  35. state = {
  36. modalSelectorDestroy: true,
  37. };
  38. componentWillMount() {
  39. const match = pathToRegexp('/product/lesson/create').exec(this.props.location.pathname);
  40. if (match) {
  41. this.cleanPageState();
  42. }
  43. }
  44. componentDidMount() {
  45. const matchId = this.isEdit();
  46. if (matchId) {
  47. this.props.dispatch({
  48. type: 'lesson/fetchLessonItem',
  49. payload: { id: matchId },
  50. });
  51. }
  52. }
  53. isEdit = () => {
  54. const { location } = this.props;
  55. const match = pathToRegexp('/product/lesson/edit/:id').exec(location.pathname);
  56. if (match) {
  57. return match[1];
  58. }
  59. return false;
  60. };
  61. cleanPageState = () => {
  62. this.props.dispatch({
  63. type: 'lesson/cleanItemState',
  64. payload: {},
  65. });
  66. };
  67. selectorDataFetcher = (params) => {
  68. this.props.dispatch({
  69. type: 'courseware/fetchCoursewareList',
  70. payload: params,
  71. });
  72. };
  73. handleSelectorModalShow = () => {
  74. this.setState({
  75. modalSelectorDestroy: false,
  76. });
  77. this.selectorDataFetcher();
  78. };
  79. handleSelectorCancel = () => {
  80. this.setState({
  81. modalSelectorDestroy: true,
  82. });
  83. };
  84. handleSelectorFinish = (rows) => {
  85. this.setState({
  86. modalSelectorDestroy: true,
  87. });
  88. this.props.dispatch({
  89. type: 'lesson/fixCoursewareList',
  90. payload: rows,
  91. });
  92. };
  93. handleSelectorChange = (params) => {
  94. this.selectorDataFetcher(params);
  95. };
  96. handleDragSortTableChange = (rows) => {
  97. this.props.dispatch({
  98. type: 'lesson/fixCoursewareList',
  99. payload: rows,
  100. });
  101. };
  102. handlePageBack = () => {
  103. this.props.dispatch(routerRedux.push({
  104. pathname: '/product/lesson/list',
  105. state: this.props.location.state,
  106. }));
  107. };
  108. handlePageSubmit = (e) => {
  109. e.preventDefault();
  110. this.props.form.validateFieldsAndScroll((err, values) => {
  111. if (!err) {
  112. // 从表单中提取相关字段
  113. const { status, ...rest } = values;
  114. const newVals = { status: boolToStatus(status), ...rest };
  115. const { lesson } = this.props;
  116. const { currentItem } = lesson;
  117. const { wareList } = currentItem;
  118. // 提取wareIdList
  119. let wareIdList;
  120. if (wareList) {
  121. wareIdList = wareList.map(item => item.id);
  122. }
  123. const matchId = this.isEdit();
  124. if (matchId) {
  125. const params = {
  126. id: matchId,
  127. wareList: wareIdList,
  128. ...newVals,
  129. };
  130. this.props.dispatch({
  131. type: 'lesson/updateLessonItem',
  132. payload: params,
  133. states: this.props.location.state,
  134. });
  135. } else {
  136. const params = {
  137. wareList: wareIdList,
  138. ...newVals,
  139. };
  140. this.props.dispatch({
  141. type: 'lesson/createLessonItem',
  142. payload: params,
  143. states: this.props.location.state,
  144. });
  145. }
  146. }
  147. });
  148. };
  149. render() {
  150. const { submitting, lesson, form, loading, courseware } = this.props;
  151. const { list, pageSize, pageNo, totalSize } = courseware;
  152. const { modalSelectorDestroy } = this.state;
  153. const { getFieldDecorator } = form;
  154. const { currentItem } = lesson;
  155. const { code, title, digest, status, wareList = [] } = currentItem;
  156. const coursewareColumns = [{
  157. title: '编号',
  158. dataIndex: 'code',
  159. key: 1,
  160. width: '20%',
  161. render: (text, record) => (
  162. <a
  163. className="a-link"
  164. target="_blank"
  165. rel="noopener noreferrer"
  166. href={`/product/courseware/edit/${record.id}`}
  167. >
  168. {text}
  169. </a>
  170. ),
  171. }, {
  172. title: '名称',
  173. dataIndex: 'title',
  174. key: 2,
  175. width: '30%',
  176. render: (text, record) => (
  177. <a
  178. className="a-link"
  179. target="_blank"
  180. rel="noopener noreferrer"
  181. href={`/product/courseware/edit/${record.id}`}
  182. >
  183. {text}
  184. </a>
  185. ),
  186. }, {
  187. title: '状态',
  188. dataIndex: 'status',
  189. key: 3,
  190. render: text => renderStatus(text),
  191. }];
  192. const renderCardName = () => {
  193. return (
  194. <Button
  195. type="primary"
  196. onClick={this.handleSelectorModalShow}
  197. >课件列表
  198. </Button>
  199. );
  200. };
  201. return (
  202. <div>
  203. <Card title="基础信息" style={{ marginBottom: 16 }}>
  204. <Form>
  205. <Form.Item hasFeedback label={fieldLabels.code} {...formItemLayout}>
  206. {getFieldDecorator('code', {
  207. rules: [
  208. {
  209. required: true, message: '请填写课编号',
  210. }, {
  211. pattern: /^[a-zA-Z0-9|-]+$/g, message: '编号包含非法字符',
  212. },
  213. ],
  214. initialValue: code,
  215. })(
  216. <Input placeholder="请输入" />
  217. )}
  218. </Form.Item>
  219. <Form.Item hasFeedback label={fieldLabels.title} {...formItemLayout}>
  220. {getFieldDecorator('title', {
  221. rules: [{ required: true, message: '请填写课名称' }],
  222. initialValue: title,
  223. })(
  224. <Input placeholder="请输入" />
  225. )}
  226. </Form.Item>
  227. <Form.Item label={fieldLabels.digest} {...formItemLayout}>
  228. {getFieldDecorator('digest', {
  229. initialValue: digest,
  230. })(
  231. <Input.TextArea rows={4} placeholder="请输入" />
  232. )}
  233. </Form.Item>
  234. <Form.Item label={fieldLabels.status} {...formItemLayout}>
  235. {getFieldDecorator('status', {
  236. valuePropName: 'checked',
  237. initialValue: statusToBool(status),
  238. })(
  239. <Switch
  240. checkedChildren="正常"
  241. unCheckedChildren="删除"
  242. />
  243. )}
  244. </Form.Item>
  245. </Form>
  246. </Card>
  247. <Card title={renderCardName()} style={{ marginBottom: 70 }}>
  248. <AXDragSortTable
  249. columns={coursewareColumns}
  250. data={wareList}
  251. onChange={this.handleDragSortTableChange}
  252. />
  253. {!modalSelectorDestroy && (
  254. <Modal
  255. visible
  256. width={1100}
  257. footer={null}
  258. title="课件资源"
  259. maskClosable={false}
  260. onCancel={this.handleSelectorCancel}
  261. >
  262. <Selector
  263. multiple
  264. list={list}
  265. loading={loading}
  266. pageNo={pageNo}
  267. pageSize={pageSize}
  268. totalSize={totalSize}
  269. selectorName="Courseware"
  270. selectedRows={wareList}
  271. onChange={this.handleSelectorChange}
  272. onCancel={this.handleSelectorCancel}
  273. onFinish={this.handleSelectorFinish}
  274. />
  275. </Modal>
  276. )}
  277. </Card>
  278. <FooterToolbar style={{ width: '100%' }}>
  279. <Button
  280. onClick={this.handlePageBack}
  281. style={{ marginRight: 10 }}
  282. >取消
  283. </Button>
  284. <Button
  285. type="primary"
  286. loading={submitting}
  287. onClick={this.handlePageSubmit}
  288. >提交
  289. </Button>
  290. </FooterToolbar>
  291. </div>
  292. );
  293. }
  294. }