1
0

page.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 > 0 ? (<Link to={`/subject/sentence/question/${record.id}`}>编辑</Link>) : (<Link to={`/subject/sentence/article/${record.id}`}>编辑</Link>)}
  73. </div>;
  74. },
  75. }];
  76. this.structColumns = [{
  77. title: 'chapter',
  78. dataIndex: 'chapter',
  79. render: (text, record, index) => {
  80. return index + 1;
  81. },
  82. }, {
  83. title: '短名称',
  84. dataIndex: 'short',
  85. render: (text, record, index) => {
  86. return <EditTableCell value={text} onChange={(v) => {
  87. this.changeStruct(index, 'short', v);
  88. }} />;
  89. },
  90. }, {
  91. title: '长名称',
  92. dataIndex: 'title',
  93. render: (text, record, index) => {
  94. return <EditTableCell value={text} onChange={(v) => {
  95. this.changeStruct(index, 'title', v);
  96. }} />;
  97. },
  98. }, {
  99. title: '练习章',
  100. dataIndex: 'exercise',
  101. render: (text, record, index) => {
  102. return <Checkbox onChange={(e) => {
  103. if (e.target.checked) this.changeStruct(index, 'exercise', 1, 0);
  104. }} checked={!!text} />;
  105. },
  106. }];
  107. }
  108. init() {
  109. Sentence.getStruct().then(result => {
  110. return this.refreshStruct(result);
  111. }).then(() => {
  112. this.initData();
  113. });
  114. }
  115. initData() {
  116. const item = this.structMap[this.state.search.chapter];
  117. if (!item) return;
  118. if (item.exercise) {
  119. Sentence.listQuestion(this.state.search).then(result => {
  120. result.list = result.list.map(row => {
  121. row.chapter = this.state.search.chapter || 0;
  122. return row;
  123. });
  124. this.setTableData(result.list, result.total);
  125. });
  126. } else {
  127. Sentence.listArticle(this.state.search).then(result => {
  128. this.setTableData(result.list, result.total);
  129. });
  130. }
  131. }
  132. refreshStruct(result) {
  133. result = result || {};
  134. result.chapters = result.chapters || [];
  135. filterForm[0].select = result.chapters.map((row, index) => { row.value = index + 1; return row; });
  136. this.structMap = getMap(filterForm[0].select, 'value');
  137. this.setState({ struct: result });
  138. }
  139. structAction() {
  140. const { struct = {} } = this.state;
  141. this.open(struct);
  142. }
  143. changeStruct(index, field, value, other) {
  144. const { detail } = this.state;
  145. if (other !== undefined) {
  146. detail.chapters.forEach((row) => {
  147. row[field] = other;
  148. });
  149. }
  150. detail.chapters[index][field] = value;
  151. this.setState({ detail });
  152. }
  153. submitStruct() {
  154. const { detail } = this.state;
  155. Sentence.setStruct(detail).then(() => {
  156. asyncSMessage('保存成功');
  157. this.close(false, 'detail');
  158. return this.refreshStruct(detail);
  159. }).then(() => {
  160. return this.initData();
  161. });
  162. }
  163. deleteAction() {
  164. const { selectedKeys } = this.state;
  165. const item = this.structMap[this.state.search.chapter];
  166. if (!item) return;
  167. asyncDelConfirm('删除确认', '是否删除选中?', () => {
  168. if (item.exercise) {
  169. return Promise.all(selectedKeys.map(row => Sentence.delQuestion({ id: row })));
  170. }
  171. return Promise.all(selectedKeys.map(row => Sentence.delArticle({ id: row })));
  172. }).then(() => {
  173. asyncSMessage('删除成功!');
  174. this.refresh();
  175. });
  176. }
  177. renderView() {
  178. return <Block flex>
  179. <FilterLayout
  180. show
  181. itemList={filterForm}
  182. data={this.state.search}
  183. onChange={data => {
  184. this.search(data);
  185. }} />
  186. <ActionLayout
  187. itemList={this.actionList}
  188. selectedKeys={this.state.selectedKeys}
  189. onAction={key => this.onAction(key)}
  190. />
  191. <TableLayout
  192. select
  193. columns={this.columns}
  194. list={this.state.list}
  195. pagination={this.state.page}
  196. loading={this.props.core.loading}
  197. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  198. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  199. selectedKeys={this.state.selectedKeys}
  200. />
  201. {this.state.detail && <Modal visible closable title='编辑章节' onCancel={() => {
  202. this.close(false, 'detail');
  203. }} onOk={() => {
  204. this.submitStruct();
  205. }}>
  206. <TableLayout
  207. rowKey={'title'}
  208. columns={this.structColumns}
  209. list={this.state.detail.chapters}
  210. pagination={false}
  211. />
  212. <Button type='link' onClick={() => {
  213. const { detail } = this.state;
  214. detail.chapters.push({});
  215. this.setState({ detail });
  216. }}>增加chapter</Button></Modal>}
  217. </Block>;
  218. }
  219. }