page.js 12 KB

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