page.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import React from 'react';
  2. import { Form, Input, Row, Col, Button, Icon, Checkbox } from 'antd';
  3. import './index.less';
  4. // import DragList from '@src/components/DragList';
  5. import Editor from '@src/components/Editor';
  6. import Page from '@src/containers/Page';
  7. import Block from '@src/components/Block';
  8. import Select from '@src/components/Select';
  9. import FilterLayout from '@src/layouts/FilterLayout';
  10. // import FileUpload from '@src/components/FileUpload';
  11. import { formatFormError, formatDate, bindSearch } from '@src/services/Tools';
  12. import { asyncSMessage } from '@src/services/AsyncTools';
  13. import { TextbookSubject, TextbookType, TextbookQuality } from '../../../../Constant';
  14. import { Textbook } from '../../../stores/textbook';
  15. export default class extends Page {
  16. init() {
  17. this.filterForm = [{
  18. key: 'textbookSubject',
  19. type: 'select',
  20. name: '单项',
  21. select: TextbookSubject,
  22. allowClear: true,
  23. }, {
  24. key: 'libraryId',
  25. type: 'select',
  26. name: '换库表',
  27. select: [],
  28. number: true,
  29. allowClear: true,
  30. }];
  31. bindSearch(this.filterForm, 'libraryId', this, (search) => {
  32. return Textbook.listLibrary(search);
  33. }, (row) => {
  34. return {
  35. title: `${formatDate(row.startDate, 'YYYY-MM-DD')}-${row.endDate ? `${formatDate(row.endDate, 'YYYY-MM-DD')}` : '至今'}`,
  36. value: row.id,
  37. };
  38. }, this.state.search.libraryId ? Number(this.state.search.libraryId) : null, null);
  39. }
  40. initData() {
  41. const { id } = this.params;
  42. const { form } = this.props;
  43. if (id) {
  44. Textbook.getTopic({ id })
  45. .then(result => {
  46. form.setFieldsValue({ 'topic[0]': result });
  47. this.setState({ data: result });
  48. });
  49. } else {
  50. this.refreshLibrary();
  51. }
  52. }
  53. refreshLibrary() {
  54. const { libraryId, textbookSubject } = this.state.search;
  55. Textbook.getNextTopic({ libraryId, textbookSubject }).then(result => {
  56. this.setState({ no: result });
  57. this.addTopic();
  58. });
  59. }
  60. removeTopic(k) {
  61. const { form } = this.props;
  62. const keys = form.getFieldValue('keys');
  63. if (keys.length === 1) {
  64. return;
  65. }
  66. form.setFieldsValue({
  67. keys: keys.filter(key => key !== k),
  68. });
  69. }
  70. addTopic() {
  71. const { form } = this.props;
  72. const keys = form.getFieldValue('keys') || [];
  73. if (!this.uuid) {
  74. this.uuid = 1;
  75. } else {
  76. this.uuid += 1;
  77. }
  78. const nextKeys = keys.concat(this.uuid);
  79. form.setFieldsValue({
  80. keys: nextKeys,
  81. });
  82. }
  83. submit() {
  84. const { form } = this.props;
  85. form.validateFields((err) => {
  86. if (!err) {
  87. const data = form.getFieldsValue();
  88. let handler;
  89. if (!this.params.id) {
  90. const { libraryId, textbookSubject } = this.state.search;
  91. handler = Promise.all([data.topic.filter(row => row).map((row, index) => Textbook.addTopic(Object.assign({ libraryId, textbookSubject }, row, { isOld: row.isOld ? 1 : 0, no: this.state.no + index })))]);
  92. } else {
  93. handler = Textbook.editTopic(Object.assign({ id: this.params.id }, data.topic[0], { isOld: data.topic[0].isOld }));
  94. }
  95. handler.then(() => {
  96. asyncSMessage('保存成功');
  97. if (!this.params.id) {
  98. linkTo('/textbook/topic');
  99. }
  100. }).catch((e) => {
  101. if (e.result) form.setFields(formatFormError(data, e.result));
  102. });
  103. }
  104. });
  105. }
  106. renderInfo(index, textbookSubject, no) {
  107. const { getFieldDecorator } = this.props.form;
  108. return <Block flex>
  109. <Row>
  110. <Col span={10}>
  111. <h1>录入题目</h1>
  112. </Col>
  113. <Col span={2} offset={10}>
  114. 序号:{no}</Col>
  115. <Col span={1} offset={1}>
  116. {!this.params.id && <Icon
  117. type='minus-circle-o'
  118. onClick={() => this.removeTopic(index)}
  119. />}</Col>
  120. </Row>
  121. <Form>
  122. <Row key={index}>
  123. <Col span={8}>
  124. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='关键词'>
  125. {getFieldDecorator(`topic[${index}].keyword`, {
  126. rules: [{
  127. required: true,
  128. message: '请选择',
  129. }],
  130. })(
  131. textbookSubject === 'quant' ? <Select select={TextbookType} /> : <Input />,
  132. )}
  133. </Form.Item>
  134. </Col>
  135. <Col span={8}>
  136. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='机经质量'>
  137. {getFieldDecorator(`topic[${index}].quality`, {
  138. rules: [{
  139. required: true,
  140. message: '请选择',
  141. }],
  142. })(
  143. <Select select={TextbookQuality} />,
  144. )}
  145. </Form.Item>
  146. </Col>
  147. <Col span={4} offset={4}>
  148. {getFieldDecorator(`topic[${index}].isOld`, {
  149. valuePropName: 'checked',
  150. })(
  151. <Checkbox>考古题目</Checkbox>,
  152. )}
  153. </Col>
  154. </Row>
  155. <Form.Item>
  156. {getFieldDecorator(`topic[${index}].detail`, {
  157. })(
  158. <Editor placeholder='输入机经题目' />,
  159. )}
  160. </Form.Item>
  161. <Form.Item>
  162. {getFieldDecorator(`topic[${index}].content`, {
  163. })(
  164. <Editor placeholder='输入机经解析' />,
  165. )}
  166. </Form.Item>
  167. </Form>
  168. </Block>;
  169. }
  170. renderAdd() {
  171. const { getFieldDecorator, getFieldValue } = this.props.form;
  172. getFieldDecorator('keys');
  173. const keys = getFieldValue('keys') || [];
  174. return <div><Block flex>
  175. <FilterLayout
  176. show
  177. itemList={this.filterForm}
  178. data={this.state.search}
  179. onChange={data => {
  180. data.page = 1;
  181. this.search(data);
  182. }} />
  183. </Block>
  184. {keys.map((key, index) => this.renderInfo(key, this.state.search.textbookSubject, this.state.no + index))}
  185. {this.state.no && <Row type="flex" justify="center">
  186. <Col>
  187. <Form.Item>
  188. <Button type='dashed' onClick={() => this.addTopic()}>
  189. <Icon type='plus' /> 新增
  190. </Button>
  191. </Form.Item>
  192. </Col>
  193. </Row>}
  194. </div>;
  195. }
  196. renderEdit() {
  197. return this.renderInfo(0, this.state.data.textbookSubject, this.state.data.no);
  198. }
  199. renderView() {
  200. return <div flex >
  201. {!this.params.id && this.renderAdd()}
  202. {this.params.id && this.renderEdit()}
  203. <Row type="flex" justify="center">
  204. <Col>
  205. <Button type="primary" onClick={() => {
  206. this.submit();
  207. }}>保存</Button>
  208. </Col>
  209. </Row>
  210. </div>;
  211. }
  212. }