page.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import React from 'react';
  2. import { Form, Button, Row, Col, List, Icon, Switch, Typography, Input } from 'antd';
  3. import './index.less';
  4. import Editor from '@src/components/Editor';
  5. import Page from '@src/containers/Page';
  6. import Block from '@src/components/Block';
  7. import DragList from '@src/components/DragList';
  8. // import FileUpload from '@src/components/FileUpload';
  9. import { formatFormError, formatDate, getMap } from '@src/services/Tools';
  10. import { asyncSMessage } from '@src/services/AsyncTools';
  11. import { UserUrl, AskTarget, QuestionType, AskModule } from '../../../../Constant';
  12. // import { User } from '../../../stores/user';
  13. import { Question } from '../../../stores/question';
  14. const QuestionTypeMap = getMap(QuestionType, 'value', 'label');
  15. const AskTargetMap = getMap(AskTarget, 'value', 'label');
  16. const AskModuleMap = getMap(AskModule, 'value', 'label');
  17. export default class extends Page {
  18. init() {
  19. // Exercise.allStruct().then(result => {
  20. // result = result.filter(row => row.level === 2).map(row => { row.title = `${row.titleZh}/${row.titleEn}`; row.value = row.id; return row; });
  21. // this.setState({ exercise: result });
  22. // });
  23. }
  24. initData() {
  25. const { id } = this.params;
  26. let handler;
  27. if (id) {
  28. handler = Question.getAsk({ id });
  29. } else {
  30. handler = Promise.resolve({ others: [] });
  31. }
  32. handler
  33. .then(result => {
  34. result.ignoreStatus = result.answerStatus === 2;
  35. const { getFieldDecorator, setFieldsValue } = this.props.form;
  36. getFieldDecorator('id');
  37. getFieldDecorator('answer');
  38. getFieldDecorator('showStatus');
  39. setFieldsValue({ id: result.id, answer: result.answer, showStatus: result.showStatus });
  40. this.setState({ data: result });
  41. });
  42. }
  43. orderQuestion(oldIndex, newIndex) {
  44. const { data } = this.state;
  45. const { others = [] } = data;
  46. const tmp = others.splice(oldIndex, 1);
  47. if (newIndex === others.length) {
  48. others.push(tmp[0]);
  49. } else {
  50. others.splice(newIndex, 0, tmp[0]);
  51. }
  52. this.setState({ others });
  53. }
  54. addOrder() {
  55. const { data } = this.state;
  56. const { others } = data;
  57. others.push(data);
  58. this.setState({ data });
  59. }
  60. removeOrder() {
  61. const { data } = this.state;
  62. const { others } = data;
  63. data.others = others.filter(row => row.id !== data.id);
  64. this.setState({ data });
  65. }
  66. submit() {
  67. const { form } = this.props;
  68. form.validateFields((err) => {
  69. if (!err) {
  70. const data = form.getFieldsValue();
  71. data.showStatus = data.showStatus ? 1 : 0;
  72. data.ignoreStatus = data.ignoreStatus ? 1 : 0;
  73. data.other = this.state.data.others.map(row => row.id);
  74. Question.editAsk(data).then(() => {
  75. asyncSMessage('保存成功');
  76. }).catch((e) => {
  77. if (e.result) form.setFields(formatFormError(data, e.result));
  78. });
  79. }
  80. });
  81. }
  82. renderBase() {
  83. const { data } = this.state;
  84. const { question = {}, questionNo = {} } = data;
  85. return <Block>
  86. <h1>题目信息</h1>
  87. <Form>
  88. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='板块'>
  89. {AskModuleMap[data.askModule]}
  90. </Form.Item>
  91. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='题型'>
  92. {QuestionTypeMap[question.type]}
  93. </Form.Item>
  94. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='题目id'>
  95. <a href='' target='_blank'>{questionNo.no}</a>
  96. </Form.Item>
  97. </Form>
  98. </Block>;
  99. }
  100. renderAsk() {
  101. const { getFieldDecorator, getFieldValue } = this.props.form;
  102. const { data, editContent } = this.state;
  103. const { user = {}, createTime, target, originContent, content } = data;
  104. return <Block>
  105. <h1>提问信息</h1>
  106. <Form>
  107. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='用户姓名'>
  108. {user.realName}
  109. </Form.Item>
  110. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='提问时间'>
  111. {formatDate(createTime)}
  112. </Form.Item>
  113. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='提问模块'>
  114. {AskTargetMap[target]}
  115. </Form.Item>
  116. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='提问内容'>
  117. {originContent}
  118. </Form.Item>
  119. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='提问详情'>
  120. {!editContent && content}
  121. {getFieldDecorator('content', {
  122. })(
  123. editContent ? <Input.TextArea placeholder='输入内容' /> : <input hidden />,
  124. )}
  125. <Switch checked={editContent} checkedChildren='编辑模式' unCheckedChildren='关闭' onChange={(value) => {
  126. data.content = getFieldValue('content');
  127. this.setState({ editContent: value, data });
  128. }} />
  129. </Form.Item>
  130. </Form>
  131. </Block>;
  132. }
  133. renderQuestionList() {
  134. return <Block>
  135. <h1>该题目的展示中问题</h1>
  136. <DragList
  137. loading={this.props.core.loading}
  138. dataSource={this.state.data.others || []}
  139. handle={'.icon'}
  140. onMove={(oldIndex, newIndex) => {
  141. this.orderQuestion(oldIndex, newIndex);
  142. }}
  143. renderItem={(item) => (
  144. <List.Item actions={[<Icon type='bars' className='icon' />, <Typography.Text copyable={{ text: `${UserUrl}/paper/question/${this.state.data.userQuestionId}?askId=${item.id}` }} />]}>
  145. <Row style={{ width: '100%' }}>
  146. <Col span={11}>问题:<span dangerouslySetInnerHTML={{ __html: item.content }} /></Col>
  147. <Col span={11} offset={1}>答复:<span dangerouslySetInnerHTML={{ __html: item.answer }} /></Col>
  148. </Row>
  149. </List.Item>
  150. )}
  151. /></Block>;
  152. }
  153. renderAnswer() {
  154. const { getFieldDecorator } = this.props.form;
  155. return <Block>
  156. <Form>
  157. {getFieldDecorator('id')(<input hidden />)}
  158. <Form.Item label='教师回复'>
  159. {getFieldDecorator('answer', {
  160. })(
  161. <Editor placeholder='输入内容' />,
  162. )}
  163. </Form.Item>
  164. <Row type="flex" justify="center">
  165. <Col span={12}>
  166. <Form.Item labelCol={{ span: 12 }} wrapperCol={{ span: 10 }} label='显示状态'>
  167. {getFieldDecorator('showStatus', {
  168. valuePropName: 'checked',
  169. })(
  170. <Switch onChange={(value) => {
  171. if (value) {
  172. this.addOrder();
  173. } else {
  174. this.removeOrder();
  175. }
  176. }} checkedChildren='on' unCheckedChildren='off' />,
  177. )}
  178. </Form.Item>
  179. </Col>
  180. <Col span={12}>
  181. <Form.Item labelCol={{ span: 12 }} wrapperCol={{ span: 10 }} label='是否忽略'>
  182. {getFieldDecorator('ignoreStatus', {
  183. valuePropName: 'checked',
  184. })(
  185. <Switch checkedChildren='on' unCheckedChildren='off' />,
  186. )}
  187. </Form.Item>
  188. </Col>
  189. </Row>
  190. </Form>
  191. </Block>;
  192. }
  193. renderView() {
  194. return <div flex>
  195. {this.renderBase()}
  196. {this.renderAsk()}
  197. {this.renderQuestionList()}
  198. {this.renderAnswer()}
  199. <Row type="flex" justify="center">
  200. <Col>
  201. <Button type="primary" onClick={() => {
  202. this.submit();
  203. }}>保存</Button>
  204. </Col>
  205. </Row>
  206. </div>;
  207. }
  208. }