page.js 7.3 KB

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