page.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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, FinishModal } 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, false);
  138. this.initData();
  139. }
  140. onChangePage(page) {
  141. this.search({ page }, false);
  142. this.initData();
  143. }
  144. onSort(value) {
  145. const keys = Object.keys(value);
  146. // this.search({ order: keys.length ? keys.join('|') : null, direction: keys.length ? Object.values(value).join('|') : null });
  147. const { sortMap } = this.state;
  148. const index = keys.length > 1 && sortMap[keys[0]] ? 1 : 0;
  149. this.search({ order: keys.length && value[keys[index]] ? keys[index] : null, direction: keys.length ? value[keys[index]] : null });
  150. }
  151. onOpen(index) {
  152. const { all } = this.state;
  153. all[index].open = !all[index].open;
  154. this.setState({ all });
  155. }
  156. onSelect(dataId) {
  157. this.search({ dataId }, false);
  158. this.initData();
  159. }
  160. subscribe(dataId, subscribe) {
  161. User.needLogin()
  162. .then(() => {
  163. My.subscribeData(dataId, subscribe)
  164. .then(() => {
  165. this.refresh();
  166. });
  167. });
  168. }
  169. renderView() {
  170. const { tab } = this.state;
  171. return (
  172. <div>
  173. <div className="top content t-8">
  174. 千行课堂 > <span className="t-1">资料列表</span>
  175. <div className="f-r"><a href="/my/tools?tab=data">我的资料</a></div>
  176. </div>
  177. <div className="center content">
  178. <Tabs
  179. type="division"
  180. theme="theme"
  181. size="small"
  182. space={2.5}
  183. width={100}
  184. border
  185. active={tab}
  186. tabs={[{ title: '全部资料', key: '' }, { title: '更新日志', key: 'history' }]}
  187. onChange={key => this.onTabChange(key)}
  188. />
  189. {this[`renderTab${tab}`]()}
  190. </div>
  191. </div>
  192. );
  193. }
  194. renderTab() {
  195. const { list = [], total, page, sortMap, filterMap, dataStructSelect, dataTypeSelect } = this.state;
  196. return [
  197. <Filter
  198. filter={filterMap}
  199. list={[
  200. {
  201. key: 'structId',
  202. placeholder: '学科',
  203. children: dataStructSelect,
  204. },
  205. {
  206. placeholder: '形式',
  207. key: 'dataType',
  208. children: dataTypeSelect,
  209. },
  210. ]}
  211. onFilter={(key, value) => this.onFilter(key, value)}
  212. />,
  213. <UserAction
  214. // search
  215. defaultSearch={filterMap.keyword}
  216. sortList={[
  217. { label: '更新时间', key: 'updateTime', fixed: true },
  218. { label: '销量', key: 'saleNumber', fixed: true },
  219. ]}
  220. sortMap={sortMap}
  221. onSort={value => this.onSort(value)}
  222. />,
  223. <div className="data-list">
  224. {list.map(item => {
  225. return <DataItem data={item} />;
  226. })}
  227. </div>,
  228. total > 0 && list.length > 0 && (
  229. <UserPagination total={total} current={page} pageSize={this.state.search.size} onChange={p => this.onChangePage(p)} />
  230. ),
  231. ];
  232. }
  233. renderTabhistory() {
  234. const { all = [], dataId, item = {}, list = [], dataStructMap = {}, showFeedbackError, feedbackError, showFinish } = this.state;
  235. return [
  236. <div className="update-search">
  237. {/* <UserAction search defaultSearch={filterMap.keyword} /> */}
  238. </div>,
  239. <div className="update-log">
  240. <div className="left">
  241. {all.map((node, index) => {
  242. const struct = dataStructMap[node.id] || {};
  243. return (
  244. <div className="block">
  245. <div className="title" onClick={() => this.onOpen(index)}>
  246. {struct.title}
  247. <div className="f-r">
  248. {node.open ? <Icon name="arrow-up" noHover /> : <Icon name="arrow-down" noHover />}
  249. </div>
  250. </div>
  251. <div className={`list ${node.open ? 'open' : ''}`}>
  252. {node.children.map(child => {
  253. return <div className={`item ${child.id === dataId ? 't-4' : ''}`} onClick={() => this.onSelect(child.id)}>{child.title}</div>;
  254. })}
  255. </div>
  256. </div>
  257. );
  258. })}
  259. </div>
  260. <div className="right">
  261. <div className="item">
  262. <div className="m-b-5">
  263. <span className="t-1 t-s-18 f-w-b">{item.title}</span>
  264. <div className="f-r">
  265. <span className="m-r-5">订阅</span>
  266. <Switch checked={item.subscribe} onChange={() => {
  267. this.subscribe(item.id, !item.subscribe);
  268. }} />
  269. <a className="m-l-5 t-4" onClick={() => this.setState({ showFeedbackError: true, feedbackError: { dataId: item.id, title: item.title, position: ['', '', ''] } })}>纠错</a>
  270. </div>
  271. </div>
  272. <UserTable size="small" columns={dataHistoryColumns} data={list} />
  273. </div>
  274. </div>
  275. </div>,
  276. <FeedbackErrorDataModal
  277. show={showFeedbackError}
  278. defaultData={feedbackError}
  279. onConfirm={() => this.setState({ showFeedbackError: false, feedbackError: {}, showFinish: true })}
  280. onCancel={() => this.setState({ showFeedbackError: false, feedbackError: {} })}
  281. />,
  282. <FinishModal show={showFinish} onConfirm={() => this.setState({ showFinish: false })} />,
  283. ];
  284. }
  285. }