index.js 4.2 KB

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