page.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import React from 'react';
  2. import './index.less';
  3. import { Switch } from 'antd';
  4. import Page from '@src/containers/Page';
  5. import { getMap, formatDate } from '@src/services/Tools';
  6. import UserAction from '../../../components/UserAction';
  7. import Tabs from '../../../components/Tabs';
  8. import Filter from '../../../components/Filter';
  9. import Icon from '../../../components/Icon';
  10. import { DataItem } from '../../../components/Item';
  11. import UserTable from '../../../components/UserTable';
  12. import UserPagination from '../../../components/UserPagination';
  13. import { FeedbackErrorDataModal } from '../../../components/OtherModal';
  14. import { DataType } from '../../../../Constant';
  15. import { Main } from '../../../stores/main';
  16. import { Course } from '../../../stores/course';
  17. import { My } from '../../../stores/my';
  18. import { User } from '../../../stores/user';
  19. const dataHistoryColumns = [
  20. { title: '更新时间', key: 'time', width: 120 },
  21. { title: '位置', key: 'position', width: 120 },
  22. { title: '原内容', key: 'originContent', width: 120 },
  23. { title: '更改为', key: 'content', width: 120 },
  24. { title: '更新至', key: 'version', width: 90 },
  25. ];
  26. export default class extends Page {
  27. initState() {
  28. const dataTypeSelect = DataType.map((row) => {
  29. return {
  30. title: row.label,
  31. key: row.value,
  32. };
  33. });
  34. dataTypeSelect.unshift({
  35. title: '全部',
  36. key: '',
  37. });
  38. return {
  39. tab: '',
  40. filterMap: {},
  41. sortMap: {},
  42. list: [],
  43. dataStructSelect: [],
  44. dataTypeSelect,
  45. // type: [
  46. // { title: '长难句', key: '1', open: true, children: [{ key: '1', title: 'OG19 语法千行' }] },
  47. // { title: '语文 Verbal', key: '2', open: true, children: [{ key: '1', title: 'OG19 语法千行' }] },
  48. // { title: '数学 Quant', key: '3', open: true, children: [{ key: '1', title: 'OG19 语法千行' }] },
  49. // ],
  50. };
  51. }
  52. init() {
  53. Main.dataStruct().then(result => {
  54. const dataStructSelect = result.filter(row => row.level === 1).map(row => {
  55. return {
  56. title: `${row.titleZh}${row.titleEn}`,
  57. key: `${row.id}`,
  58. };
  59. });
  60. dataStructSelect.unshift({
  61. title: '全部',
  62. key: '',
  63. });
  64. const dataStructMap = getMap(dataStructSelect, 'key');
  65. this.setState({ dataStructSelect, dataStructMap });
  66. });
  67. }
  68. initData() {
  69. const data = Object.assign(this.state, this.state.search);
  70. if (data.order) {
  71. data.sortMap = { [data.order]: data.direction };
  72. }
  73. data.filterMap = this.state.search;
  74. this.setState(data);
  75. switch (data.tab) {
  76. case 'history':
  77. this.refreshHistory();
  78. break;
  79. default:
  80. this.refreshData();
  81. }
  82. }
  83. refreshData() {
  84. Course.listData(Object.assign({}, this.state.search))
  85. .then(result => {
  86. this.setState({ list: result.list, total: result.total });
  87. });
  88. }
  89. refreshHistory() {
  90. let { all, dataId } = this.state;
  91. dataId = Number(dataId);
  92. if (!all) {
  93. Course.listData({ page: 1, size: 1000 })
  94. .then(result => {
  95. this.dataMap = getMap(result.list, 'id');
  96. const allIds = [];
  97. result.list.forEach(row => {
  98. if (allIds.indexOf(row.structId) >= 0) return;
  99. allIds.push(row.structId);
  100. });
  101. all = allIds.map(row => {
  102. return { id: row, children: [] };
  103. });
  104. result.list.forEach(row => {
  105. const index = allIds.indexOf(row.structId);
  106. all[index].children.push(row);
  107. if (row.id === dataId) all[index].open = true;
  108. });
  109. if (!dataId) {
  110. dataId = result.list[0].id;
  111. all[0].open = true;
  112. }
  113. this.refreshHistoryList(dataId);
  114. this.setState({ all });
  115. });
  116. } else if (dataId) {
  117. this.refreshHistoryList(dataId);
  118. }
  119. }
  120. refreshHistoryList(dataId) {
  121. Course.historyData({ dataId, page: 1, size: 1000 })
  122. .then(result => {
  123. result.list = result.list.map(row => {
  124. row.time = formatDate(row.time, 'YYYY-MM-DD\nHH:mm:ss');
  125. return row;
  126. });
  127. this.setState({ item: this.dataMap[dataId], list: result.list, dataId });
  128. });
  129. }
  130. onTabChange(tab) {
  131. const data = { tab };
  132. this.refreshQuery(data);
  133. }
  134. onFilter(key, value) {
  135. const { filterMap } = this.state;
  136. filterMap[key] = value;
  137. this.search(filterMap);
  138. }
  139. onChangePage(page) {
  140. this.search({ page }, false);
  141. this.initData();
  142. }
  143. onSort(value) {
  144. const keys = Object.keys(value);
  145. // this.search({ order: keys.length ? keys.join('|') : null, direction: keys.length ? Object.values(value).join('|') : null });
  146. const { sortMap } = this.state;
  147. const index = keys.length > 1 && sortMap[keys[0]] ? 1 : 0;
  148. this.search({ order: keys.length ? keys[index] : null, direction: keys.length ? value[keys[index]] : null });
  149. }
  150. onOpen(index) {
  151. const { all } = this.state;
  152. all[index].open = !all[index].open;
  153. this.setState({ all });
  154. }
  155. onSelect(dataId) {
  156. this.search({ dataId }, false);
  157. this.initData();
  158. }
  159. subscribe(dataId, subscribe) {
  160. User.needLogin()
  161. .then(() => {
  162. My.subscribeData(dataId, subscribe)
  163. .then(() => {
  164. this.refresh();
  165. });
  166. });
  167. }
  168. renderView() {
  169. const { tab } = this.state;
  170. return (
  171. <div>
  172. <div className="top content t-8">
  173. 千行课堂 > <span className="t-1">资料列表</span>
  174. <div className="f-r"><a href="/my/tools?tab=data">我的资料</a></div>
  175. </div>
  176. <div className="center content">
  177. <Tabs
  178. type="division"
  179. theme="theme"
  180. size="small"
  181. space={2.5}
  182. width={100}
  183. border
  184. active={tab}
  185. tabs={[{ title: '全部资料', key: '' }, { title: '更新日志', key: 'history' }]}
  186. onChange={key => this.onTabChange(key)}
  187. />
  188. {this[`renderTab${tab}`]()}
  189. </div>
  190. </div>
  191. );
  192. }
  193. renderTab() {
  194. const { list = [], total, page, sortMap, filterMap, dataStructSelect, dataTypeSelect } = this.state;
  195. return [
  196. <Filter
  197. filter={filterMap}
  198. list={[
  199. {
  200. key: 'structId',
  201. placeholder: '学科',
  202. children: dataStructSelect,
  203. },
  204. {
  205. placeholder: '形式',
  206. key: 'dataType',
  207. children: dataTypeSelect,
  208. },
  209. ]}
  210. onFilter={(key, value) => this.onFilter(key, value)}
  211. />,
  212. <UserAction
  213. // search
  214. defaultSearch={filterMap.keyword}
  215. sortList={[
  216. { label: '更新时间', key: 'updateTime', fixed: true },
  217. { label: '销量', key: 'saleNumber', fixed: true },
  218. ]}
  219. sortMap={sortMap}
  220. onSort={value => this.onSort(value)}
  221. />,
  222. <div className="data-list">
  223. {list.map(item => {
  224. return <DataItem data={item} />;
  225. })}
  226. </div>,
  227. total > 0 && list.length > 0 && (
  228. <UserPagination total={total} current={page} pageSize={this.state.search.size} onChange={p => this.onChangePage(p)} />
  229. ),
  230. ];
  231. }
  232. renderTabhistory() {
  233. const { all = [], dataId, item = {}, list = [], dataStructMap = {}, showFeedbackError, feedbackError } = this.state;
  234. return [
  235. <div className="update-search">
  236. {/* <UserAction search defaultSearch={filterMap.keyword} /> */}
  237. </div>,
  238. <div className="update-log">
  239. <div className="left">
  240. {all.map((node, index) => {
  241. const struct = dataStructMap[node.id] || {};
  242. return (
  243. <div className="block">
  244. <div className="title" onClick={() => this.onOpen(index)}>
  245. {struct.title}
  246. <div className="f-r">
  247. {node.open ? <Icon name="arrow-up" noHover /> : <Icon name="arrow-down" noHover />}
  248. </div>
  249. </div>
  250. <div className={`list ${node.open ? 'open' : ''}`}>
  251. {node.children.map(child => {
  252. return <div className={`item ${child.id === dataId ? 't-4' : ''}`} onClick={() => this.onSelect(child.id)}>{child.title}</div>;
  253. })}
  254. </div>
  255. </div>
  256. );
  257. })}
  258. </div>
  259. <div className="right">
  260. <div className="item">
  261. <div className="m-b-5">
  262. <span className="t-1 t-s-18 f-w-b">{item.title}</span>
  263. <div className="f-r">
  264. <span className="m-r-5">订阅</span>
  265. <Switch checked={item.subscribe} onChange={() => {
  266. this.subscribe(item.id, !item.subscribe);
  267. }} />
  268. <a className="m-l-5 t-4" onClick={() => this.setState({ showFeedbackError: true, feedbackError: { dataId: item.id, title: item.title, position: ['', '', ''] } })}>纠错</a>
  269. </div>
  270. </div>
  271. <UserTable size="small" columns={dataHistoryColumns} data={list} />
  272. </div>
  273. </div>
  274. </div>,
  275. <FeedbackErrorDataModal
  276. show={showFeedbackError}
  277. defaultData={feedbackError}
  278. onConfirm={() => this.setState({ showFeedbackError: false })}
  279. onCancel={() => this.setState({ showFeedbackError: false })}
  280. />,
  281. ];
  282. }
  283. }