index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import React, { Component } from 'react';
  2. import { Form, Modal, Alert } from 'antd';
  3. import './index.less';
  4. import Select from '@src/components/Select';
  5. import { getMap, generateSearch } from '@src/services/Tools';
  6. import QuestionNoList from '../QuestionNoList';
  7. import { Question } from '../../stores/question';
  8. import { Sentence } from '../../stores/sentence';
  9. class Association extends Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = { ids: this.props.ids, show: !!props.modal, loading: false, err: '' };
  13. this.questionNos = [];
  14. generateSearch(this.props.field, { mode: 'multiple' }, this, (search) => {
  15. return this.searchQuestion(search);
  16. }, (row) => {
  17. return {
  18. title: row.title,
  19. value: row.id,
  20. };
  21. }, this.props.ids, null);
  22. this.listQuestion(this.props.ids, 'ids');
  23. }
  24. onConfirm() {
  25. this.props.form.validateFields((err, fieldsValue) => {
  26. if (err) {
  27. return;
  28. }
  29. if (this.props.onConfirm) {
  30. this.setState({ loading: true });
  31. this.props
  32. .onConfirm(fieldsValue)
  33. .then(() => {
  34. this.setState({ loading: false });
  35. this.onCancel();
  36. })
  37. .catch(e => {
  38. this.setState({ loading: false, err: e.message });
  39. });
  40. } else {
  41. this.onCancel();
  42. }
  43. });
  44. }
  45. onCancel() {
  46. if (this.props.modal) this.setState({ show: false });
  47. if (this.props.onCancel) this.props.onCancel();
  48. }
  49. listQuestion(values, field = 'ids') {
  50. if (!values || values.length === 0) {
  51. this.setState({ questionNos: [] });
  52. return;
  53. }
  54. let handler;
  55. switch (this.props.module) {
  56. case 'exercise':
  57. // 查找练习题目
  58. handler = Question.listNo({ [field]: values, module: this.props.module });
  59. break;
  60. case 'sentence':
  61. handler = Sentence.listQuestion({ [field]: values });
  62. break;
  63. default:
  64. return;
  65. }
  66. this.setState({ loading: true });
  67. handler.then(result => {
  68. const map = getMap(result, 'id');
  69. const questionNos = values.map(row => map[row]).filter(row => row);
  70. this.setState({ questionNos, loading: false });
  71. if (!this.props.modal) this.onConfirm();
  72. });
  73. }
  74. searchQuestion(params) {
  75. let handler;
  76. switch (this.props.module) {
  77. case 'exercise':
  78. // 查找练习题目
  79. handler = Question.searchNo(params);
  80. break;
  81. case 'sentence':
  82. handler = Sentence.searchQuestion(params);
  83. break;
  84. default:
  85. handler = Promise.reject(new Error('module is error'));
  86. }
  87. return handler;
  88. }
  89. renderForm() {
  90. const { getFieldDecorator, setFieldsValue } = this.props.form;
  91. const { field = 'questionNoIds' } = this.props;
  92. return <Form>
  93. <Form.Item>
  94. {getFieldDecorator(field)(
  95. <Select mode='multiple' maxTagCount={200} notFoundContent={null} placeholder='输入题目id, 逗号分隔' tokenSeparators={[',', ',']} {...this.state[field]} onChange={(values) => {
  96. // this.setState({ nos: values });
  97. this.listQuestion(values, 'ids');
  98. }} />,
  99. )}
  100. </Form.Item>
  101. <QuestionNoList loading={this.state.loading} questionNos={this.state.questionNos} onChange={(nos) => {
  102. this.questionNos = nos;
  103. getFieldDecorator(field);
  104. setFieldsValue({ [field]: nos.map(row => row.id) });
  105. }} />
  106. </Form>;
  107. }
  108. render() {
  109. const { modal, title, confirmText = '确定', cancelText = '取消' } = this.props;
  110. const { show, loading, err } = this.state;
  111. return modal ? (
  112. <Modal
  113. title={title}
  114. visible={show}
  115. okText={confirmText}
  116. cancelText={cancelText}
  117. confirmLoading={loading}
  118. onOk={() => this.onConfirm()}
  119. onCancel={() => this.onCancel()}
  120. >
  121. {err && <Alert type="error" showIcon message={err} closable onClose={() => this.setState({ err: '' })} />}
  122. {this.renderForm()}
  123. </Modal>
  124. ) : (<div><h1>{title}</h1>{this.renderForm()}</div>);
  125. }
  126. }
  127. export default Form.create()(Association);