page.js 12 KB

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