index.js 4.1 KB

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