page.js 9.6 KB

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