page.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import React from 'react';
  2. import { Form, Input, Button, Row, Col, DatePicker } from 'antd';
  3. import './index.less';
  4. import Page from '@src/containers/Page';
  5. import Block from '@src/components/Block';
  6. import Select from '@src/components/Select';
  7. // import FileUpload from '@src/components/FileUpload';
  8. import { formatFormError, generateSearch, getMap } from '@src/services/Tools';
  9. import { asyncSMessage } from '@src/services/AsyncTools';
  10. // import { PreviewMode } from '../../../../Constant';
  11. import QuestionNoList from '../../../components/QuestionNoList';
  12. import { Preview } from '../../../stores/preview';
  13. import { User } from '../../../stores/user';
  14. import { Question } from '../../../stores/question';
  15. import { Exercise } from '../../../stores/exercise';
  16. import config from './index';
  17. export default class extends Page {
  18. constructor(props) {
  19. super(props);
  20. const { id } = this.params;
  21. if (id) {
  22. config.title = '编辑预习作业';
  23. } else {
  24. config.title = '添加预习作业';
  25. }
  26. }
  27. init() {
  28. Exercise.allStruct().then(result => {
  29. result = result.filter(row => row.level === 2).map(row => { row.title = `${row.titleZh}/${row.titleEn}`; row.value = row.id; return row; });
  30. this.setState({ exercise: result });
  31. });
  32. }
  33. initData() {
  34. const { id } = this.params;
  35. const { form } = this.props;
  36. let handler;
  37. if (id) {
  38. handler = Preview.get({ id });
  39. } else {
  40. handler = Promise.resolve({ questionNoIds: [] });
  41. }
  42. handler
  43. .then(result => {
  44. const { questionNoIds } = result;
  45. result.time = [result.startTime, result.endTime];
  46. form.setFieldsValue(result);
  47. generateSearch('userIds', { mode: 'multiple' }, this, (search) => {
  48. return User.list(search);
  49. }, (row) => {
  50. return {
  51. title: `${row.nickname}(${row.mobile})`,
  52. value: row.id,
  53. };
  54. }, result.userIds, null);
  55. this.setState({ questionNoIds });
  56. });
  57. }
  58. submit() {
  59. const { form } = this.props;
  60. form.validateFields((err) => {
  61. if (!err) {
  62. const data = form.getFieldsValue();
  63. [data.startTime, data.endTime] = data.time;
  64. let handler;
  65. data.questionNoIds = this.state.questionNos.map(row => row.id);
  66. if (data.id) {
  67. handler = Preview.add(data);
  68. } else {
  69. handler = Preview.edit(data);
  70. }
  71. handler.then(() => {
  72. asyncSMessage('保存成功');
  73. }).catch((e) => {
  74. if (e.result) form.setFields(formatFormError(data, e.result));
  75. });
  76. }
  77. });
  78. }
  79. deleteQuestion(index) {
  80. const { questionNos } = this.state;
  81. questionNos.splice(index, 1);
  82. this.setState({ questionNos });
  83. this.props.form.setFieldsValue({ questionNos: questionNos.map(row => row.no) });
  84. }
  85. orderQuestion(oldIndex, newIndex) {
  86. const { questionNos } = this.state;
  87. const tmp = questionNos[oldIndex];
  88. questionNos[oldIndex] = questionNos[newIndex];
  89. questionNos[newIndex] = tmp;
  90. this.setState({ questionNos });
  91. this.props.form.setFieldsValue({ questionNos: questionNos.map(row => row.no) });
  92. }
  93. searchQuestion(values, field = 'nos') {
  94. if (values.length === 0) {
  95. this.setState({ questionNos: [] });
  96. return;
  97. }
  98. // 查找练习题目
  99. Question.listNo({ [field]: values, module: 'exercise' }).then(result => {
  100. const map = getMap(result, 'no');
  101. const questionNos = values.map(no => map[no]).filter(row => row);
  102. this.setState({ questionNos });
  103. this.props.form.setFieldsValue({ questionNos: questionNos.map(row => row.no) });
  104. });
  105. }
  106. renderBase() {
  107. const { getFieldDecorator } = this.props.form;
  108. return <Block>
  109. <Form>
  110. {getFieldDecorator('id')(<input hidden />)}
  111. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='选择课程'>
  112. {getFieldDecorator('category', {
  113. rules: [
  114. { required: true, message: '请选择课程' },
  115. ],
  116. })(
  117. <Select select={this.state.exercise} placeholder='请选择课程' />,
  118. )}
  119. </Form.Item>
  120. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='起止时间'>
  121. {getFieldDecorator('time', {
  122. rules: [
  123. { required: true, message: '请输入起止时间' },
  124. ],
  125. })(
  126. <DatePicker.RangePicker />,
  127. )}
  128. </Form.Item>
  129. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='作业标题'>
  130. {getFieldDecorator('title', {
  131. rules: [
  132. { required: true, message: '请输入作业标题' },
  133. ],
  134. })(
  135. <Input placeholder='请输入作业标题' />,
  136. )}
  137. </Form.Item>
  138. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='指定做题人'>
  139. {getFieldDecorator('userIds', {
  140. rules: [
  141. { required: true, message: '请指定做题人' },
  142. ],
  143. })(
  144. <Select {...this.state.userIds} placeholder='请指定做题人' />,
  145. )}
  146. </Form.Item>
  147. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='选择作业题'>
  148. {getFieldDecorator('questionNos', {
  149. rules: [
  150. { required: true, message: '请选择作业题' },
  151. ],
  152. })(
  153. <Select mode='tags' maxTagCount={200} notFoundContent={null} placeholder='输入题目id, 逗号分隔' tokenSeparators={[',', ',']} onChange={(values) => {
  154. this.setState({ questionNos: values });
  155. }} />,
  156. )}
  157. </Form.Item>
  158. </Form>
  159. </Block>;
  160. }
  161. renderQuestionList() {
  162. const { getFieldDecorator, setFieldsValue } = this.props.form;
  163. return <Block>
  164. <h1>题目预览</h1>
  165. <QuestionNoList module='exercise' loading={false} ids={this.state.questionNoIds} nos={this.state.questionNos} onChange={(questionNos) => {
  166. this.questionNos = questionNos;
  167. getFieldDecorator('questionNos');
  168. setFieldsValue({ questionNos: questionNos.map(row => row.no) });
  169. }} />
  170. </Block>;
  171. }
  172. renderView() {
  173. return <div flex>
  174. {this.renderBase()}
  175. {this.renderQuestionList()}
  176. <Row type="flex" justify="center">
  177. <Col>
  178. <Button type="primary" onClick={() => {
  179. this.submit();
  180. }}>保存</Button>
  181. </Col>
  182. </Row>
  183. </div>;
  184. }
  185. }