index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 && this.props.modal) {
  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. });
  72. }
  73. searchQuestion(params) {
  74. let handler;
  75. switch (this.props.module) {
  76. case 'exercise':
  77. // 查找练习题目
  78. handler = Question.searchNo(params);
  79. break;
  80. case 'sentence':
  81. handler = Sentence.searchQuestion(params);
  82. break;
  83. default:
  84. handler = Promise.reject(new Error('module is error'));
  85. }
  86. return handler;
  87. }
  88. renderForm() {
  89. const { getFieldDecorator, setFieldsValue } = this.props.form;
  90. const { field = 'questionNoIds' } = this.props;
  91. return <Form>
  92. <Form.Item>
  93. {getFieldDecorator(field)(
  94. <Select mode='multiple' maxTagCount={200} notFoundContent={null} placeholder='输入题目id, 逗号分隔' tokenSeparators={[',', ',']} {...this.state[field]} onChange={(values) => {
  95. // this.setState({ nos: values });
  96. this.listQuestion(values, 'ids');
  97. }} />,
  98. )}
  99. </Form.Item>
  100. <QuestionNoList loading={this.state.loading} questionNos={this.state.questionNos} onChange={(nos) => {
  101. this.questionNos = nos;
  102. getFieldDecorator(field);
  103. setFieldsValue({ [field]: nos.map(row => row.id) });
  104. }} />
  105. </Form>;
  106. }
  107. render() {
  108. const { modal, title, confirmText = '确定', cancelText = '取消' } = this.props;
  109. const { show, loading, err } = this.state;
  110. return modal ? (
  111. <Modal
  112. title={title}
  113. visible={show}
  114. okText={confirmText}
  115. cancelText={cancelText}
  116. confirmLoading={loading}
  117. onOk={() => this.onConfirm()}
  118. onCancel={() => this.onCancel()}
  119. >
  120. {err && <Alert type="error" showIcon message={err} closable onClose={() => this.setState({ err: '' })} />}
  121. {this.renderForm()}
  122. </Modal>
  123. ) : (<div>{this.renderForm()}</div>);
  124. }
  125. }
  126. export default Form.create()(Association);