page.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. placeholder="输入题干内容“According to some critics…”或题号“PREP07-1”"
  185. onChange={e => this.onChangeSearch(e.target.value)}
  186. onFocus={() => this.setState({ focus: true })}
  187. onBlur={() => this.setState({ focus: false })}
  188. />
  189. <Button width={150} onClick={() => this.onSearch(keyword)}>
  190. <Icon className="m-r-5" type="search" />
  191. 搜索题目
  192. </Button>
  193. {(focus || tip) && (
  194. <div
  195. hidden={!keyword || searchList.length === 0}
  196. className="search-tip-wrapper"
  197. onMouseEnter={() => this.setState({ tip: true })}
  198. onMouseLeave={() => this.setState({ tip: false })}
  199. >
  200. {searchList.map(item => {
  201. return (
  202. <div
  203. className="t-2 t-s-16"
  204. onClick={() => {
  205. // this.onChangeSearch(item, true);
  206. this.onSearch(item);
  207. }}
  208. >
  209. {item}
  210. </div>
  211. );
  212. })}
  213. </div>
  214. )}
  215. {(focus || tip) && (
  216. <div
  217. hidden={keyword || searchHistoryList.length === 0}
  218. className="search-tip-wrapper"
  219. onMouseEnter={() => this.setState({ tip: true })}
  220. onMouseLeave={() => this.setState({ tip: false })}
  221. >
  222. {searchHistoryList.map((item, index) => {
  223. return (
  224. <div
  225. className="t-2 t-s-16 p-r"
  226. onClick={() => {
  227. // this.onChangeSearch(item, true);
  228. this.onSearch(item);
  229. }}
  230. >
  231. <span className="nowrap" style={{ width: '95%', overflow: 'hidden', display: 'block' }}>{item}</span>
  232. <div
  233. className="t-4 t-s-12 c-p p-a"
  234. style={{ top: 0, right: 10 }}
  235. onClick={e => {
  236. e.stopPropagation();
  237. User.removeSearchIndex(index);
  238. }}
  239. >
  240. 删除
  241. </div>
  242. </div>
  243. );
  244. })}
  245. <div className="all-del t-r">
  246. <span className="t-4 t-s-12 c-p" onClick={() => User.clearSearch()}>
  247. 删除历史
  248. </span>
  249. </div>
  250. </div>
  251. )}
  252. </div>
  253. </div>
  254. );
  255. }
  256. renderFilter() {
  257. const { filterMap, sortMap } = this.state;
  258. const {
  259. subject,
  260. questionSubjectSelect,
  261. questionSubjectMap = {},
  262. difficultSelect,
  263. oneSelect,
  264. twoSelectMap = {},
  265. list = [],
  266. total,
  267. page,
  268. } = this.state;
  269. const { login } = this.props.user;
  270. return (
  271. <div className="filter-layout">
  272. <div className="content">
  273. <div style={{ right: 0, top: 0 }} className="p-a" hidden={!login}>
  274. <a href="/question/search/history" target="_blank">
  275. <Assets name="history_time" className="m-r-5" /> 浏览历史 >
  276. </a>
  277. </div>
  278. <Tabs
  279. border
  280. force
  281. type="division"
  282. theme="theme"
  283. size="small"
  284. space={5}
  285. width={220}
  286. active={subject}
  287. tabs={questionSubjectSelect}
  288. onChange={key => {
  289. this.onRefreshFilter({ subject: key, order: undefined, direction: undefined });
  290. }}
  291. />
  292. <UserAction
  293. selectList={[
  294. {
  295. key: 'questionType',
  296. placeholder: '题型',
  297. select: questionSubjectMap[subject] || [],
  298. },
  299. {
  300. label: '范围',
  301. children: [
  302. {
  303. key: 'one',
  304. placeholder: '全部',
  305. select: oneSelect,
  306. },
  307. {
  308. placeholder: '全部',
  309. key: 'two',
  310. be: 'one',
  311. selectMap: twoSelectMap,
  312. },
  313. ],
  314. },
  315. {
  316. right: true,
  317. placeholder: '难度',
  318. key: 'difficult',
  319. select: difficultSelect,
  320. },
  321. ]}
  322. sortList={[
  323. { key: 'time', label: '全站用时', fixed: true, right: true },
  324. { key: 'correct', label: '平均正确率', fixed: true, right: true },
  325. { key: 'collect_number', label: '收藏人数', fixed: true, right: true },
  326. ]}
  327. filterMap={filterMap}
  328. sortMap={sortMap}
  329. onSort={value => this.onSort(value)}
  330. onFilter={value => this.onFilter(value)}
  331. />
  332. {this.renderList()}
  333. {total > 0 && list.length > 0 && (
  334. <UserPagination
  335. total={total}
  336. current={page}
  337. pageSize={this.state.search.size}
  338. onChange={p => this.onChangePage(p)}
  339. />
  340. )}
  341. </div>
  342. </div>
  343. );
  344. }
  345. renderResult() {
  346. const { total, list, page } = this.state;
  347. return (
  348. <div className="result-layout">
  349. <div className="content">
  350. <div className="m-b-1">
  351. <span className="t-1 t-s-24">搜索结果:</span>
  352. <span className="t-2 t-s-18">共{total}条</span>
  353. </div>
  354. {this.renderList()}
  355. {total > 0 && list.length > 0 && (
  356. <UserPagination
  357. total={total}
  358. current={page}
  359. pageSize={this.state.search.size}
  360. onChange={p => this.onChangePage(p)}
  361. />
  362. )}
  363. </div>
  364. </div>
  365. );
  366. }
  367. renderList() {
  368. const { list } = this.state;
  369. return list.map(item => {
  370. return <SearchItem data={item} onClick={() => this.addSearchHistory(item.id)} />;
  371. });
  372. }
  373. }
  374. class SearchItem extends Component {
  375. render() {
  376. const { data = {}, onClick } = this.props;
  377. return (
  378. <div className="search-item">
  379. <div className="search-item-head">
  380. <span className="t-15 t-s-16 m-r-1">{QuestionTypeMap[data.question.questionType]}</span>
  381. <a className="t-1 t-s-16" href={`/question/detail/${data.id}`} target="_blank" onClick={() => onClick()}>
  382. {data.title}
  383. </a>
  384. <div className="f-r t-15 t-s-14">
  385. <span className="m-r-1">{data.question.difficult}</span>
  386. <span className="m-r-1">用时: {formatSeconds(data.totalTime / data.totalNumber)}</span>
  387. <span className="m-r-1">{formatPercent(data.totalCorrect, data.totalNumber, false)}</span>
  388. <span>收藏 {data.collectNumber || 0}</span>
  389. </div>
  390. </div>
  391. <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>
  392. </div>
  393. );
  394. }
  395. }