page.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. import React, { Component } from 'react';
  2. import './index.less';
  3. import { Icon, Typography } from 'antd';
  4. import { Link } from 'react-router-dom';
  5. import Page from '@src/containers/Page';
  6. import Assets from '@src/components/Assets';
  7. import { getMap, formatPercent, formatSeconds } from '@src/services/Tools';
  8. import Button from '../../../components/Button';
  9. import Tabs from '../../../components/Tabs';
  10. import UserAction from '../../../components/UserAction';
  11. import UserPagination from '../../../components/UserPagination';
  12. import { refreshQuestionType, refreshStruct } from '../index';
  13. import { User } from '../../../stores/user';
  14. import { Question } from '../../../stores/question';
  15. import { QuestionType, QuestionDifficult } from '../../../../Constant';
  16. import { My } from '../../../stores/my';
  17. const QuestionTypeMap = getMap(QuestionType, 'value', 'label');
  18. // const QuestionDifficultMap = getMap(QuestionDifficult, 'value', 'label');
  19. export default class extends Page {
  20. initState() {
  21. this.searchNo = 0;
  22. return {
  23. list: [],
  24. searchList: [],
  25. keyword: '',
  26. subject: 'verbal',
  27. filterMap: {},
  28. sortMap: {},
  29. focus: false,
  30. difficultSelect: QuestionDifficult.map(row => {
  31. return {
  32. title: row.label,
  33. key: row.value,
  34. };
  35. }),
  36. defaultSortMap: {},
  37. };
  38. }
  39. initData() {
  40. const data = Object.assign(this.state, this.state.search);
  41. data.filterMap = this.state.search;
  42. if (data.order) {
  43. data.sortMap = { [data.order]: data.direction };
  44. } else if (data.direction == null || data.direction === '') {
  45. data.sortMap = Object.assign({}, this.state.defaultSortMap);
  46. }
  47. if (data.subject) {
  48. data.filterMap.subject = data.subject;
  49. }
  50. this.setState(data);
  51. refreshQuestionType(this, data.subject, data.questionType, {
  52. needSentence: false,
  53. allSubject: true,
  54. excludeAwa: true,
  55. }).then(({ questionTypes }) => {
  56. return refreshStruct(this, questionTypes, 'exercise', data.one, data.two, {
  57. all: true,
  58. }).then(({ structIds }) => {
  59. let handler = null;
  60. if (this.state.search.keyword) {
  61. handler = Question.searchStem({
  62. keyword: this.state.search.keyword,
  63. page: this.state.search.page,
  64. size: this.state.search.size,
  65. });
  66. } else {
  67. handler = Question.searchStem(
  68. Object.assign({ questionTypes, structIds, module: 'exercise' }, this.state.search, {
  69. order: Object.keys(data.sortMap)
  70. .map(key => {
  71. return `${key} ${data.sortMap[key]}`;
  72. })
  73. .join(','),
  74. }),
  75. );
  76. }
  77. handler.then(result => {
  78. const list = (this.state.search.keyword || '').split(' ').filter(row => row && row.length > 1);
  79. result.list = result.list.map(row => {
  80. if (list.length > 0 && row.question.description) {
  81. for (let i = 0; i < list.length; i += 1) {
  82. const reg = new RegExp(`(${list[i]})`, 'ig');
  83. row.question.description = row.question.description.replace(reg, '<span>$1</span>');
  84. }
  85. }
  86. return row;
  87. });
  88. this.setState({ list: result.list, total: result.total, page: data.page, searchResult: !!data.keyword });
  89. });
  90. });
  91. });
  92. }
  93. onRefreshFilter(query) {
  94. // this.changeQuery(query);
  95. // this.setState(query);
  96. this.refreshQuery(query);
  97. // this.initData();
  98. }
  99. onFilter(value) {
  100. this.search(value);
  101. // this.initData();
  102. }
  103. onSearch(value) {
  104. const { keyword } = this.state;
  105. User.addSearch(value || keyword);
  106. // this.search({ page: 1, keyword }, false);
  107. // this.changeQuery({ keyword });
  108. // this.setState({ keyword });
  109. this.refreshQuery({ keyword: value || keyword });
  110. // this.initData();
  111. }
  112. onSort(value) {
  113. const keys = Object.keys(value);
  114. const { sortMap, defaultSortMap } = this.state;
  115. const index = keys.length > 1 && sortMap[keys[0]] ? 1 : 0;
  116. let order = keys.length && value[keys[index]] ? keys[index] : null;
  117. let direction = keys.length ? value[keys[index]] : null;
  118. if (order == null) {
  119. const [prevOrder] = Object.keys(sortMap);
  120. if (!defaultSortMap[prevOrder]) {
  121. const sortKeys = Object.keys(defaultSortMap);
  122. if (sortKeys.length > 0) {
  123. [order] = sortKeys;
  124. direction = defaultSortMap[order];
  125. }
  126. }
  127. }
  128. this.search({ order, direction }, false);
  129. this.initData();
  130. }
  131. onChangePage(page) {
  132. this.search({ page }, false);
  133. this.initData();
  134. }
  135. onChangeSearch(keyword, force = false) {
  136. if (!force) {
  137. this.searchNo += 1;
  138. const no = this.searchNo;
  139. Question.searchNo({ keyword, module: 'exercise', page: 1, size: 5 }).then(result => {
  140. if (no !== this.searchNo) return;
  141. this.setState({ searchList: result.list.map(row => row.title) });
  142. });
  143. }
  144. this.setState({ keyword });
  145. }
  146. addSearchHistory(id) {
  147. My.addSearchHistory(id);
  148. }
  149. toggleCollect(index) {
  150. const { list } = this.props;
  151. const questionNo = list[index];
  152. if (!questionNo.collect) {
  153. My.addQuestionCollect(questionNo.id).then(() => {
  154. questionNo.collect = true;
  155. questionNo.collectNumber += 1;
  156. this.setState({ list });
  157. });
  158. } else {
  159. My.delQuestionCollect(questionNo.id).then(() => {
  160. questionNo.collect = false;
  161. questionNo.collectNumber -= 1;
  162. this.setState({ list });
  163. });
  164. }
  165. }
  166. renderView() {
  167. const { searchResult } = this.state;
  168. return (
  169. <div>
  170. {this.renderSearch()}
  171. {searchResult ? this.renderResult() : this.renderFilter()}
  172. </div>
  173. );
  174. }
  175. renderSearch() {
  176. const { searchHistoryList = [] } = this.props.user;
  177. const { searchList = [], keyword, focus, tip } = this.state;
  178. // console.log(focus, tip, searchHistoryList);
  179. return (
  180. <div className="search-layout">
  181. <div className="search-wrapper">
  182. <input
  183. value={keyword}
  184. onChange={e => this.onChangeSearch(e.target.value)}
  185. onFocus={() => this.setState({ focus: true })}
  186. onBlur={() => this.setState({ focus: false })}
  187. />
  188. <Button width={150} onClick={() => this.onSearch(keyword)}>
  189. <Icon className="m-r-5" type="search" />
  190. 搜索题目
  191. </Button>
  192. {(focus || tip) && (
  193. <div
  194. hidden={!keyword || searchList.length === 0}
  195. className="search-tip-wrapper"
  196. onMouseEnter={() => this.setState({ tip: true })}
  197. onMouseLeave={() => this.setState({ tip: false })}
  198. >
  199. {searchList.map(item => {
  200. return (
  201. <div
  202. className="t-2 t-s-16"
  203. onClick={() => {
  204. // this.onChangeSearch(item, true);
  205. this.onSearch(item);
  206. }}
  207. >
  208. {item}
  209. </div>
  210. );
  211. })}
  212. </div>
  213. )}
  214. {(focus || tip) && (
  215. <div
  216. hidden={keyword || searchHistoryList.length === 0}
  217. className="search-tip-wrapper"
  218. onMouseEnter={() => this.setState({ tip: true })}
  219. onMouseLeave={() => this.setState({ tip: false })}
  220. >
  221. {searchHistoryList.map((item, index) => {
  222. return (
  223. <div
  224. className="t-2 t-s-16"
  225. onClick={() => {
  226. // this.onChangeSearch(item, true);
  227. this.onSearch(item);
  228. }}
  229. >
  230. {item}
  231. <div
  232. className="f-r t-4 t-s-12 c-p"
  233. onClick={e => {
  234. e.stopPropagation();
  235. User.removeSearchIndex(index);
  236. }}
  237. >
  238. 删除
  239. </div>
  240. </div>
  241. );
  242. })}
  243. <div className="all-del t-r">
  244. <span className="t-4 t-s-12 c-p" onClick={() => User.clearSearch()}>
  245. 删除历史
  246. </span>
  247. </div>
  248. </div>
  249. )}
  250. </div>
  251. </div>
  252. );
  253. }
  254. renderFilter() {
  255. const { filterMap, sortMap } = this.state;
  256. const {
  257. subject,
  258. questionSubjectSelect,
  259. questionSubjectMap = {},
  260. difficultSelect,
  261. oneSelect,
  262. twoSelectMap = {},
  263. list = [],
  264. total,
  265. page,
  266. } = this.state;
  267. const { login } = this.props.user;
  268. return (
  269. <div className="filter-layout">
  270. <div className="content">
  271. <div style={{ right: 0, top: 0 }} className="p-a" hidden={!login}>
  272. <Link to="/question/search/history">
  273. <Assets name="history_time" className="m-r-5" /> 浏览历史 >
  274. </Link>
  275. </div>
  276. <Tabs
  277. border
  278. force
  279. type="division"
  280. theme="theme"
  281. size="small"
  282. space={5}
  283. width={220}
  284. active={subject}
  285. tabs={questionSubjectSelect}
  286. onChange={key => {
  287. this.onRefreshFilter({ subject: key, order: undefined, direction: undefined });
  288. }}
  289. />
  290. <UserAction
  291. selectList={[
  292. {
  293. key: 'questionType',
  294. placeholder: '题型',
  295. select: questionSubjectMap[subject] || [],
  296. },
  297. {
  298. label: '范围',
  299. children: [
  300. {
  301. key: 'one',
  302. placeholder: '全部',
  303. select: oneSelect,
  304. },
  305. {
  306. placeholder: '全部',
  307. key: 'two',
  308. be: 'one',
  309. selectMap: twoSelectMap,
  310. },
  311. ],
  312. },
  313. {
  314. right: true,
  315. placeholder: '难度',
  316. key: 'difficult',
  317. select: difficultSelect,
  318. },
  319. ]}
  320. sortList={[
  321. { key: 'time', label: '全站用时', fixed: true, right: true },
  322. { key: 'correct', label: '平均正确率', fixed: true, right: true },
  323. { key: 'collect_number', label: '收藏人数', fixed: true, right: true },
  324. ]}
  325. filterMap={filterMap}
  326. sortMap={sortMap}
  327. onSort={value => this.onSort(value)}
  328. onFilter={value => this.onFilter(value)}
  329. />
  330. {this.renderList()}
  331. {total > 0 && list.length > 0 && (
  332. <UserPagination
  333. total={total}
  334. current={page}
  335. pageSize={this.state.search.size}
  336. onChange={p => this.onChangePage(p)}
  337. />
  338. )}
  339. </div>
  340. </div>
  341. );
  342. }
  343. renderResult() {
  344. const { total, list, page } = this.state;
  345. return (
  346. <div className="result-layout">
  347. <div className="content">
  348. <div className="m-b-1">
  349. <span className="t-1 t-s-24">搜索结果:</span>
  350. <span className="t-2 t-s-18">共{total}条</span>
  351. </div>
  352. {this.renderList()}
  353. {total > 0 && list.length > 0 && (
  354. <UserPagination
  355. total={total}
  356. current={page}
  357. pageSize={this.state.search.size}
  358. onChange={p => this.onChangePage(p)}
  359. />
  360. )}
  361. </div>
  362. </div>
  363. );
  364. }
  365. renderList() {
  366. const { list } = this.state;
  367. return list.map(item => {
  368. return <SearchItem data={item} onClick={() => this.addSearchHistory(item.id)} />;
  369. });
  370. }
  371. }
  372. class SearchItem extends Component {
  373. render() {
  374. const { data = {}, onClick } = this.props;
  375. return (
  376. <div className="search-item">
  377. <div className="search-item-head">
  378. <span className="t-15 t-s-16 m-r-1">{QuestionTypeMap[data.question.questionType]}</span>
  379. <a className="t-1 t-s-16" href={`/question/detail/${data.id}`} target="_blank" onClick={() => onClick()}>
  380. {data.title}
  381. </a>
  382. <div className="f-r t-15 t-s-14">
  383. <span className="m-r-1">{data.question.difficult}</span>
  384. <span className="m-r-1">用时: {formatSeconds(data.totalTime / data.totalNumber)}</span>
  385. <span className="m-r-1">{formatPercent(data.totalCorrect, data.totalNumber, false)}</span>
  386. <span>收藏 {data.collectNumber || 0}</span>
  387. </div>
  388. </div>
  389. <div className="t-1 p-20" ><Typography.Paragraph ellipsis={{ rows: 3, expandable: false }}><div className="search-content" dangerouslySetInnerHTML={{ __html: data.question.description }} /></Typography.Paragraph></div>
  390. </div>
  391. );
  392. }
  393. }