page.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import React from 'react';
  2. import { Form, Input, InputNumber, Tabs, Switch, Checkbox, Row, Col, Button } from 'antd';
  3. import './index.less';
  4. import Page from '@src/containers/Page';
  5. import Block from '@src/components/Block';
  6. import Editor from '@src/components/Editor';
  7. import Select from '@src/components/Select';
  8. // import FileUpload from '@src/components/FileUpload';
  9. import { formatFormError } from '@src/services/Tools';
  10. import { asyncSMessage } from '@src/services/AsyncTools';
  11. import { SentenceOption } from '../../../../Constant';
  12. import { Preview } from '../../../stores/preview';
  13. import { Exercise } from '../../../stores/exercise';
  14. import { Sentence } from '../../../stores/sentence';
  15. import config from './index';
  16. export default class extends Page {
  17. constructor(props) {
  18. super(props);
  19. const { id } = this.params;
  20. if (id) {
  21. config.title = '编辑长难句题目';
  22. } else {
  23. config.title = '添加长难句题目';
  24. }
  25. }
  26. init() {
  27. Exercise.allStruct().then(result => {
  28. result = result.filter(row => row.level === 2).map(row => { row.title = `${row.titleZh}/${row.titleEn}`; row.value = row.id; return row; });
  29. this.setState({ exercise: result });
  30. });
  31. }
  32. initData() {
  33. const { id } = this.params;
  34. const { form } = this.props;
  35. let handler;
  36. if (id) {
  37. handler = Preview.get({ id });
  38. } else {
  39. handler = Promise.resolve({ no: 1 });
  40. }
  41. handler
  42. .then(result => {
  43. form.setFieldsValue(result);
  44. });
  45. }
  46. submit() {
  47. const { form } = this.props;
  48. form.validateFields((err) => {
  49. if (!err) {
  50. const data = form.getFieldsValue();
  51. data.isTrail = data.isTrail ? 1 : 0;
  52. data.question.description = data.question.stem.replace(/<[^>]+>/g, '');
  53. let handler;
  54. if (data.id) {
  55. handler = Sentence.editQuestion(data);
  56. } else {
  57. handler = Sentence.addQuestion(data);
  58. }
  59. handler.then(() => {
  60. asyncSMessage('保存成功');
  61. }).catch((e) => {
  62. if (e.result) form.setFields(formatFormError(data, e.result));
  63. });
  64. }
  65. });
  66. }
  67. renderTitle() {
  68. const { getFieldDecorator } = this.props.form;
  69. return <Block>
  70. <Form>
  71. {getFieldDecorator('id')(<input hidden />)}
  72. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='题目序号'>
  73. {getFieldDecorator('no', {
  74. rules: [
  75. { required: true, message: '请输入序号' },
  76. // {
  77. // validator: (rule, value, callback) => {
  78. // if (this.partList.indexOf(value) >= 0) callback('该part已被使用');
  79. // else callback();
  80. // callback();
  81. // },
  82. // },
  83. ],
  84. })(
  85. <InputNumber min={1} precision={0} formatter={(v) => parseInt(v, 10) || 1} />,
  86. )}
  87. </Form.Item>
  88. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='名称'>
  89. {getFieldDecorator('title', {
  90. rules: [
  91. { required: true, message: '请输入名称' },
  92. ],
  93. })(
  94. <Input placeholder='请输入' />,
  95. )}
  96. </Form.Item>
  97. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='开放试用'>
  98. {getFieldDecorator('isTrail', {
  99. valuePropName: 'checked',
  100. })(
  101. <Switch checkedChildren='on' unCheckedChildren='off' />,
  102. )}
  103. </Form.Item>
  104. </Form>
  105. </Block>;
  106. }
  107. renderBase() {
  108. const { getFieldDecorator } = this.props.form;
  109. return <Block flex>
  110. <h1>题干信息</h1>
  111. <Form>
  112. {getFieldDecorator('question.id')(<input hidden />)}
  113. <Form.Item>
  114. {getFieldDecorator('question.stem', {
  115. })(
  116. <Editor placeholder='请输入内容' />,
  117. )}
  118. </Form.Item>
  119. </Form>
  120. </Block>;
  121. }
  122. renderAnswer() {
  123. const { getFieldDecorator } = this.props.form;
  124. return <Block flex>
  125. <h1>题目答案</h1>
  126. <table boarder cellSpacing className='answer'>
  127. <tr>
  128. <td>主语</td>
  129. <td>
  130. <Form.Item>
  131. {getFieldDecorator('question.content.subject')(
  132. <Select mode='tags' maxTagCount={200} notFoundContent={null} tokenSeparators={[',', ',']} />,
  133. )}
  134. </Form.Item>
  135. </td>
  136. </tr>
  137. <tr>
  138. <td>谓语</td>
  139. <td><Form.Item>
  140. {getFieldDecorator('question.content.predicate')(
  141. <Select mode='tags' maxTagCount={200} notFoundContent={null} tokenSeparators={[',', ',']} />,
  142. )}
  143. </Form.Item></td>
  144. </tr>
  145. <tr>
  146. <td>宾语</td>
  147. <td><Form.Item>
  148. {getFieldDecorator('question.content.object')(
  149. <Select mode='tags' maxTagCount={200} notFoundContent={null} tokenSeparators={[',', ',']} />,
  150. )}
  151. </Form.Item></td>
  152. </tr>
  153. </table>
  154. </Block>;
  155. }
  156. renderOption() {
  157. const { getFieldDecorator } = this.props.form;
  158. return <Block flex>
  159. <h1>选项信息(长难句)</h1>
  160. <Form>
  161. <Form.Item>
  162. {getFieldDecorator('question.content.options')(
  163. <Checkbox.Group options={SentenceOption} />,
  164. )}
  165. </Form.Item>
  166. </Form>
  167. </Block>;
  168. }
  169. renderQX() {
  170. const { getFieldDecorator } = this.props.form;
  171. return <Block flex>
  172. <Form>
  173. <Form.Item label='千行解析'>
  174. {getFieldDecorator('question.qxContent', {
  175. })(
  176. <Editor placeholder='输入内容' />,
  177. )}
  178. </Form.Item>
  179. </Form>
  180. </Block>;
  181. }
  182. renderChinese() {
  183. const { getFieldDecorator } = this.props.form;
  184. return <Block flex>
  185. <Form>
  186. <Form.Item label='中文解析'>
  187. {getFieldDecorator('chinese', {
  188. })(
  189. <Editor placeholder='输入内容' />,
  190. )}
  191. </Form.Item>
  192. </Form>
  193. </Block>;
  194. }
  195. renderTab() {
  196. return <Tabs activeKey='sentence' onChange={(tab) => {
  197. switch (tab) {
  198. case 'textbook':
  199. linkTo('/subject/textbook/question');
  200. break;
  201. case 'base':
  202. linkTo('/subject/question');
  203. break;
  204. default:
  205. }
  206. }}>
  207. <Tabs.TabPane key='base' tab='考试题型' />
  208. <Tabs.TabPane key='sentence' tab='长难句' />
  209. <Tabs.TabPane key='textbook' tab='数学机经' />
  210. </Tabs>;
  211. }
  212. renderView() {
  213. return <div flex >
  214. {this.renderTab()}
  215. {this.renderTitle()}
  216. {this.renderBase()}
  217. {this.renderAnswer()}
  218. {this.renderOption()}
  219. {this.renderQX()}
  220. {this.renderChinese()}
  221. <Row type="flex" justify="center">
  222. <Col>
  223. <Button type="primary" onClick={() => {
  224. this.submit();
  225. }}>保存</Button>
  226. </Col>
  227. </Row>
  228. </div>;
  229. }
  230. }