page.js 7.5 KB

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