1
0

page.js 10 KB

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