1
0

page.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 { AskTarget, QuestionType } 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. export default class extends Page {
  17. init() {
  18. // Exercise.allStruct().then(result => {
  19. // result = result.filter(row => row.level === 2).map(row => { row.title = `${row.titleZh}/${row.titleEn}`; row.value = row.id; return row; });
  20. // this.setState({ exercise: result });
  21. // });
  22. }
  23. initData() {
  24. const { id } = this.params;
  25. let handler;
  26. if (id) {
  27. handler = Question.getAsk({ id });
  28. } else {
  29. handler = Promise.resolve({ others: [{ content: 123123123, answer: 123123 }, {}] });
  30. }
  31. handler
  32. .then(result => {
  33. const { getFieldDecorator, setFieldsValue } = this.props.form;
  34. getFieldDecorator('id');
  35. getFieldDecorator('answer');
  36. getFieldDecorator('showStatus');
  37. setFieldsValue({ id: result.id, answer: result.answer, showStatus: result.showStatus });
  38. this.setState({ data: result });
  39. });
  40. }
  41. orderQuestion(oldIndex, newIndex) {
  42. const { data } = this.state;
  43. const { others = [] } = data;
  44. const tmp = others[oldIndex];
  45. others[oldIndex] = others[newIndex];
  46. others[newIndex] = tmp;
  47. this.setState({ others });
  48. }
  49. addOrder() {
  50. const { data } = this.state;
  51. const { others } = data;
  52. others.push(data);
  53. this.setState({ data });
  54. }
  55. removeOrder() {
  56. const { data } = this.state;
  57. const { others } = data;
  58. data.others = others.filter(row => row.id !== data.id);
  59. this.setState({ data });
  60. }
  61. submit() {
  62. const { form } = this.props;
  63. form.validateFields((err) => {
  64. if (!err) {
  65. const data = form.getFieldsValue();
  66. data.other = this.state.data.others.map(row => row.id);
  67. Question.editAsk(data).then(() => {
  68. asyncSMessage('保存成功');
  69. }).catch((e) => {
  70. if (e.result) form.setFields(formatFormError(data, e.result));
  71. });
  72. }
  73. });
  74. }
  75. renderBase() {
  76. const { data } = this.state;
  77. const { question = {}, questionNo = {} } = data;
  78. return <Block>
  79. <h1>题目信息</h1>
  80. <Form>
  81. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='题型'>
  82. {QuestionTypeMap[question.type]}
  83. </Form.Item>
  84. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='题目id'>
  85. <a href='' target='_blank'>{questionNo.no}</a>
  86. </Form.Item>
  87. </Form>
  88. </Block>;
  89. }
  90. renderAsk() {
  91. const { data } = this.state;
  92. const { user = {}, createTime, target, originContent, content } = data;
  93. return <Block>
  94. <h1>提问信息</h1>
  95. <Form>
  96. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='用户姓名'>
  97. {user.realName}
  98. </Form.Item>
  99. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='提问时间'>
  100. {formatDate(createTime)}
  101. </Form.Item>
  102. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='提问模块'>
  103. {AskTargetMap[target]}
  104. </Form.Item>
  105. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='提问内容'>
  106. {originContent}
  107. </Form.Item>
  108. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='提问详情'>
  109. {content}
  110. </Form.Item>
  111. </Form>
  112. </Block>;
  113. }
  114. renderQuestionList() {
  115. return <Block>
  116. <h1>该题目的展示中问题</h1>
  117. <DragList
  118. loading={this.props.core.loading}
  119. dataSource={this.state.data.others || []}
  120. handle={'.icon'}
  121. onMove={(oldIndex, newIndex) => {
  122. this.orderQuestion(oldIndex, newIndex);
  123. }}
  124. renderItem={(item) => (
  125. <List.Item actions={[<Icon type='bars' className='icon' />, <Typography.Text copyable={{ text: 'url' }} />]}>
  126. <Row style={{ width: '100%' }}>
  127. <Col span={11}>问题:{item.content}</Col>
  128. <Col span={11} offset={1}>答复:{item.answer}</Col>
  129. </Row>
  130. </List.Item>
  131. )}
  132. /></Block>;
  133. }
  134. renderAnswer() {
  135. const { getFieldDecorator } = this.props.form;
  136. return <Block>
  137. <Form>
  138. {getFieldDecorator('id')(<input hidden />)}
  139. <Form.Item label='教师回复'>
  140. {getFieldDecorator('answer', {
  141. })(
  142. <Input.TextArea placeholder='输入内容' />,
  143. )}
  144. </Form.Item>
  145. <Row type="flex" justify="center">
  146. <Col span={12}>
  147. <Form.Item labelCol={{ span: 12 }} wrapperCol={{ span: 10 }} label='显示状态'>
  148. {getFieldDecorator('showStatus', {
  149. valuePropName: 'checked',
  150. })(
  151. <Switch onChange={(value) => {
  152. if (value) {
  153. this.addOrder();
  154. } else {
  155. this.removeOrder();
  156. }
  157. }} checkedChildren='on' unCheckedChildren='off' />,
  158. )}
  159. </Form.Item>
  160. </Col>
  161. </Row>
  162. </Form>
  163. </Block>;
  164. }
  165. renderView() {
  166. return <div flex>
  167. {this.renderBase()}
  168. {this.renderAsk()}
  169. {this.renderQuestionList()}
  170. {this.renderAnswer()}
  171. <Row type="flex" justify="center">
  172. <Col>
  173. <Button type="primary" onClick={() => {
  174. this.submit();
  175. }}>保存</Button>
  176. </Col>
  177. </Row>
  178. </div>;
  179. }
  180. }