page.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 { PcUrl, 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. getFieldDecorator('content');
  40. setFieldsValue({ id: result.id, content: result.content, answer: result.answer, showStatus: result.showStatus });
  41. this.setState({ data: result });
  42. });
  43. }
  44. orderQuestion(oldIndex, newIndex) {
  45. const { data } = this.state;
  46. const { others = [] } = data;
  47. const tmp = others.splice(oldIndex, 1);
  48. if (newIndex === others.length) {
  49. others.push(tmp[0]);
  50. } else {
  51. others.splice(newIndex, 0, tmp[0]);
  52. }
  53. this.setState({ others });
  54. }
  55. addOrder() {
  56. const { data } = this.state;
  57. const { others } = data;
  58. others.push(data);
  59. this.setState({ data });
  60. }
  61. removeOrder() {
  62. const { data } = this.state;
  63. const { others } = data;
  64. data.others = others.filter(row => row.id !== data.id);
  65. this.setState({ data });
  66. }
  67. submit() {
  68. const { form } = this.props;
  69. form.validateFields((err) => {
  70. if (!err) {
  71. const data = form.getFieldsValue();
  72. data.showStatus = data.showStatus ? 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. ignore() {
  83. const { data } = this.state;
  84. Question.editAsk({ id: data.id, ignoreStatus: 1 }).then(() => {
  85. asyncSMessage('操作成功');
  86. goBack();
  87. });
  88. }
  89. renderBase() {
  90. const { data } = this.state;
  91. const { question = {}, questionNo = {} } = data;
  92. return <Block>
  93. <h1>题目信息</h1>
  94. <Form>
  95. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='板块'>
  96. {AskModuleMap[data.askModule]}
  97. </Form.Item>
  98. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='题型'>
  99. {QuestionTypeMap[question.questionType]}
  100. </Form.Item>
  101. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='题目id'>
  102. <a href={`/subject/question/${question.id}`} target='_blank'>{questionNo.title}</a>
  103. </Form.Item>
  104. </Form>
  105. </Block>;
  106. }
  107. renderAsk() {
  108. const { getFieldDecorator, getFieldValue } = this.props.form;
  109. const { data, editContent } = this.state;
  110. const { user = {}, createTime, target, originContent, content } = data;
  111. return <Block>
  112. <h1>提问信息</h1>
  113. <Form>
  114. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='用户姓名'>
  115. {user.nickname}
  116. </Form.Item>
  117. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='提问时间'>
  118. {formatDate(createTime, 'YYYY-MM-DD HH:mm:ss')}
  119. </Form.Item>
  120. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='提问模块'>
  121. {AskTargetMap[target]}
  122. </Form.Item>
  123. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='提问内容'>
  124. {originContent}
  125. </Form.Item>
  126. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='提问详情'>
  127. {!editContent && content}
  128. {getFieldDecorator('content', {
  129. })(
  130. editContent ? <Input.TextArea placeholder='输入内容' /> : <input hidden />,
  131. )}
  132. <Switch checked={editContent} checkedChildren='编辑模式' unCheckedChildren='关闭' onChange={(value) => {
  133. data.content = getFieldValue('content');
  134. this.setState({ editContent: value, data });
  135. }} />
  136. </Form.Item>
  137. </Form>
  138. </Block>;
  139. }
  140. renderQuestionList() {
  141. return <Block>
  142. <h1>该题目的展示中问题</h1>
  143. <DragList
  144. loading={this.props.core.loading}
  145. dataSource={this.state.data.others || []}
  146. handle={'.icon'}
  147. onMove={(oldIndex, newIndex) => {
  148. this.orderQuestion(oldIndex, newIndex);
  149. }}
  150. renderItem={(item) => (
  151. <List.Item actions={[<Icon type='bars' className='icon' />, <Typography.Text copyable={{ text: this.state.data.userQuestionId ? `${PcUrl}/paper/question/${this.state.data.userQuestionId}?askId=${item.id}` : `${PcUrl}/question/detail/${this.state.data.questionNoId}?askId=${item.id}` }} />]}>
  152. <Row style={{ width: '100%' }}>
  153. <Col span={11}>问题:<span dangerouslySetInnerHTML={{ __html: item.content }} /></Col>
  154. <Col span={11} offset={1}>答复:<span dangerouslySetInnerHTML={{ __html: item.answer }} /></Col>
  155. </Row>
  156. </List.Item>
  157. )}
  158. /></Block>;
  159. }
  160. renderAnswer() {
  161. const { getFieldDecorator } = this.props.form;
  162. return <Block>
  163. <Form>
  164. {getFieldDecorator('id')(<input hidden />)}
  165. <Form.Item label='教师回复'>
  166. {getFieldDecorator('answer', {
  167. })(
  168. <Editor placeholder='输入内容' />,
  169. )}
  170. </Form.Item>
  171. <Row type="flex" justify="center">
  172. <Col span={12}>
  173. <Form.Item labelCol={{ span: 12 }} wrapperCol={{ span: 10 }} label='显示状态'>
  174. {getFieldDecorator('showStatus', {
  175. valuePropName: 'checked',
  176. })(
  177. <Switch onChange={(value) => {
  178. if (value) {
  179. this.addOrder();
  180. } else {
  181. this.removeOrder();
  182. }
  183. }} checkedChildren='on' unCheckedChildren='off' />,
  184. )}
  185. </Form.Item>
  186. </Col>
  187. </Row>
  188. </Form>
  189. </Block>;
  190. }
  191. renderView() {
  192. return <div flex>
  193. {this.renderBase()}
  194. {this.renderAsk()}
  195. {this.renderQuestionList()}
  196. {this.renderAnswer()}
  197. <Row type="flex" justify="center">
  198. <Col>
  199. <Button type="drange" onClick={() => {
  200. this.ignore();
  201. }}>忽略问题</Button>
  202. </Col>
  203. <Col>
  204. <Button type="primary" onClick={() => {
  205. this.submit();
  206. }}>保存</Button>
  207. </Col>
  208. </Row>
  209. </div>;
  210. }
  211. }