page.js 12 KB

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