page.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import React from 'react';
  2. import { Link } from 'react-router-dom';
  3. import { Button, Modal, Checkbox } from 'antd';
  4. import './index.less';
  5. import Page from '@src/containers/Page';
  6. import Block from '@src/components/Block';
  7. import EditTableCell from '@src/components/EditTableCell';
  8. import FilterLayout from '@src/layouts/FilterLayout';
  9. import ActionLayout from '@src/layouts/ActionLayout';
  10. import TableLayout from '@src/layouts/TableLayout';
  11. // import { PreviewStatus } from '@src/services/Constant';
  12. import { getMap } from '@src/services/Tools';
  13. import { asyncSMessage, asyncDelConfirm } from '@src/services/AsyncTools';
  14. import { Sentence } from '../../../stores/sentence';
  15. const filterForm = [
  16. {
  17. key: 'chapter',
  18. type: 'select',
  19. allowClear: true,
  20. name: 'chapter',
  21. placeholder: '请选择',
  22. number: true,
  23. },
  24. // {
  25. // key: 'part',
  26. // type: 'select',
  27. // allowClear: true,
  28. // name: '状态',
  29. // number: true,
  30. // placeholder: '请选择',
  31. // },
  32. ];
  33. export default class extends Page {
  34. constructor(props) {
  35. super(props);
  36. this.structMap = {};
  37. this.actionList = [{
  38. key: 'struct',
  39. name: '编辑章节',
  40. }, {
  41. key: 'article',
  42. name: '新建文章',
  43. render: (item) => {
  44. return <Link to='/subject/sentence/article'><Button>{item.name}</Button></Link>;
  45. },
  46. }, {
  47. key: 'question',
  48. name: '新建题目',
  49. render: (item) => {
  50. return <Link to='/subject/sentence/question'><Button>{item.name}</Button></Link>;
  51. },
  52. }, {
  53. key: 'delete',
  54. name: '批量删除',
  55. needSelect: 1,
  56. }];
  57. this.columns = [{
  58. title: 'chapter',
  59. dataIndex: 'chapter',
  60. }, {
  61. title: 'part',
  62. dataIndex: 'part',
  63. }, {
  64. title: 'title',
  65. dataIndex: 'title',
  66. }, {
  67. title: '操作',
  68. dataIndex: 'handler',
  69. render: (text, record) => {
  70. const item = this.structMap[this.state.search.chapter];
  71. return <div className="table-button">
  72. {item.exercise && (<Link to={`/sentence/question/${record.id}`}>编辑</Link>)}
  73. {!item.exercise && (<Link to={`/sentence/article/${record.id}`}>编辑</Link>)}
  74. </div>;
  75. },
  76. }];
  77. this.structColumns = [{
  78. title: 'chapter',
  79. dataIndex: 'chapter',
  80. render: (text, record, index) => {
  81. return index + 1;
  82. },
  83. }, {
  84. title: '短名称',
  85. dataIndex: 'short',
  86. render: (text, record, index) => {
  87. return <EditTableCell value={text} onChange={(v) => {
  88. this.changeStruct(index, 'short', v);
  89. }} />;
  90. },
  91. }, {
  92. title: '长名称',
  93. dataIndex: 'title',
  94. render: (text, record, index) => {
  95. return <EditTableCell value={text} onChange={(v) => {
  96. this.changeStruct(index, 'title', v);
  97. }} />;
  98. },
  99. }, {
  100. title: '练习章',
  101. dataIndex: 'exercise',
  102. render: (text, record, index) => {
  103. return <Checkbox onChange={(e) => {
  104. if (e.target.checked) this.changeStruct(index, 'exercise', 1, 0);
  105. }} checked={!!text} />;
  106. },
  107. }];
  108. }
  109. init() {
  110. Sentence.getStruct().then(result => {
  111. return this.refreshStruct(result);
  112. });
  113. }
  114. initData() {
  115. const item = this.structMap[this.state.search.chapter];
  116. if (!item) return;
  117. if (item.exercise) {
  118. Sentence.listQuestion(this.state.search).then(result => {
  119. this.setTableData(result.list, result.total);
  120. });
  121. } else {
  122. Sentence.listArticle(this.state.search).then(result => {
  123. this.setTableData(result.list, result.total);
  124. });
  125. }
  126. }
  127. refreshStruct(result) {
  128. result = result || {};
  129. result.chapters = result.chapters || [];
  130. filterForm[0].select = result.chapters.map((row, index) => { row.value = index + 1; return row; });
  131. this.structMap = getMap(filterForm[0].select, 'value');
  132. this.setState({ struct: result });
  133. }
  134. structAction() {
  135. const { struct = {} } = this.state;
  136. this.open(struct);
  137. }
  138. changeStruct(index, field, value, other) {
  139. const { detail } = this.state;
  140. if (other !== undefined) {
  141. detail.chapters.forEach((row) => {
  142. row[field] = other;
  143. });
  144. }
  145. detail.chapters[index][field] = value;
  146. this.setState({ detail });
  147. }
  148. submitStruct() {
  149. const { detail } = this.state;
  150. Sentence.setStruct(detail).then(() => {
  151. asyncSMessage('保存成功');
  152. this.close(false, 'detail');
  153. return this.refreshStruct(detail);
  154. }).then(() => {
  155. return this.initData();
  156. });
  157. }
  158. deleteAction() {
  159. const { selectedKeys } = this.state;
  160. const item = this.structMap[this.state.search.chapter];
  161. if (!item) return;
  162. asyncDelConfirm('删除确认', '是否删除选中?', () => {
  163. if (item.exercise) {
  164. return Promise.all(selectedKeys.map(row => Sentence.delQuestion({ id: row })));
  165. }
  166. return Promise.all(selectedKeys.map(row => Sentence.delArticle({ id: row })));
  167. }).then(() => {
  168. asyncSMessage('删除成功!');
  169. this.refresh();
  170. });
  171. }
  172. renderView() {
  173. return <Block flex>
  174. <FilterLayout
  175. show
  176. itemList={filterForm}
  177. data={this.state.search}
  178. onChange={data => {
  179. this.search(data);
  180. }} />
  181. <ActionLayout
  182. itemList={this.actionList}
  183. selectedKeys={this.state.selectedKeys}
  184. onAction={key => this.onAction(key)}
  185. />
  186. <TableLayout
  187. select
  188. columns={this.columns}
  189. list={this.state.list}
  190. pagination={this.state.page}
  191. loading={this.props.core.loading}
  192. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  193. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  194. selectedKeys={this.state.selectedKeys}
  195. />
  196. {this.state.detail && <Modal visible closable title='编辑章节' onCancel={() => {
  197. this.close(false, 'detail');
  198. }} onOk={() => {
  199. this.submitStruct();
  200. }}>
  201. <TableLayout
  202. rowKey={'title'}
  203. columns={this.structColumns}
  204. list={this.state.detail.chapters}
  205. pagination={false}
  206. />
  207. <Button type='link' onClick={() => {
  208. const { detail } = this.state;
  209. detail.chapters.push({});
  210. this.setState({ detail });
  211. }}>增加chapter</Button></Modal>}
  212. </Block>;
  213. }
  214. }