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