page.js 11 KB

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