123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- import React, { Component } from 'react';
- import './index.less';
- import { Icon, Typography } from 'antd';
- import { Link } from 'react-router-dom';
- import Page from '@src/containers/Page';
- import Assets from '@src/components/Assets';
- import { getMap, formatPercent, formatSeconds } from '@src/services/Tools';
- import Button from '../../../components/Button';
- import Tabs from '../../../components/Tabs';
- import UserAction from '../../../components/UserAction';
- import UserPagination from '../../../components/UserPagination';
- import { refreshQuestionType, refreshStruct } from '../index';
- import { User } from '../../../stores/user';
- import { Question } from '../../../stores/question';
- import { QuestionType, QuestionDifficult } from '../../../../Constant';
- import { My } from '../../../stores/my';
- const QuestionTypeMap = getMap(QuestionType, 'value', 'label');
- // const QuestionDifficultMap = getMap(QuestionDifficult, 'value', 'label');
- export default class extends Page {
- initState() {
- this.searchNo = 0;
- return {
- list: [],
- searchList: [],
- keyword: '',
- subject: 'verbal',
- filterMap: {},
- sortMap: {},
- focus: false,
- difficultSelect: QuestionDifficult.map(row => {
- return {
- title: row.label,
- key: row.value,
- };
- }),
- defaultSortMap: {},
- };
- }
- initData() {
- const data = Object.assign(this.state, this.state.search);
- data.filterMap = this.state.search;
- if (data.order) {
- data.sortMap = { [data.order]: data.direction };
- } else if (data.direction == null || data.direction === '') {
- data.sortMap = Object.assign({}, this.state.defaultSortMap);
- }
- if (data.subject) {
- data.filterMap.subject = data.subject;
- }
- this.setState(data);
- refreshQuestionType(this, data.subject, data.questionType, {
- needSentence: false,
- allSubject: true,
- excludeAwa: true,
- }).then(({ questionTypes }) => {
- return refreshStruct(this, questionTypes, 'exercise', data.one, data.two, {
- all: true,
- }).then(({ structIds }) => {
- let handler = null;
- if (this.state.search.keyword) {
- handler = Question.searchStem({
- keyword: this.state.search.keyword,
- page: this.state.search.page,
- size: this.state.search.size,
- });
- } else {
- handler = Question.searchStem(
- Object.assign({ questionTypes, structIds, module: 'exercise' }, this.state.search, {
- order: Object.keys(data.sortMap)
- .map(key => {
- return `${key} ${data.sortMap[key]}`;
- })
- .join(','),
- }),
- );
- }
- handler.then(result => {
- const list = (this.state.search.keyword || '').split(' ').filter(row => row && row.length > 1);
- result.list = result.list.map(row => {
- if (list.length > 0 && row.question.description) {
- for (let i = 0; i < list.length; i += 1) {
- const reg = new RegExp(`(${list[i]})`, 'ig');
- row.question.description = row.question.description.replace(reg, '<span>$1</span>');
- }
- }
- return row;
- });
- this.setState({ list: result.list, total: result.total, page: data.page, searchResult: !!data.keyword });
- });
- });
- });
- }
- onRefreshFilter(query) {
- // this.changeQuery(query);
- // this.setState(query);
- this.refreshQuery(query);
- // this.initData();
- }
- onFilter(value) {
- this.search(value);
- // this.initData();
- }
- onSearch(value) {
- const { keyword } = this.state;
- User.addSearch(value || keyword);
- // this.search({ page: 1, keyword }, false);
- // this.changeQuery({ keyword });
- // this.setState({ keyword });
- this.refreshQuery({ keyword: value || keyword });
- // this.initData();
- }
- onSort(value) {
- const keys = Object.keys(value);
- const { sortMap, defaultSortMap } = this.state;
- const index = keys.length > 1 && sortMap[keys[0]] ? 1 : 0;
- let order = keys.length && value[keys[index]] ? keys[index] : null;
- let direction = keys.length ? value[keys[index]] : null;
- if (order == null) {
- const [prevOrder] = Object.keys(sortMap);
- if (!defaultSortMap[prevOrder]) {
- const sortKeys = Object.keys(defaultSortMap);
- if (sortKeys.length > 0) {
- [order] = sortKeys;
- direction = defaultSortMap[order];
- }
- }
- }
- this.search({ order, direction }, false);
- this.initData();
- }
- onChangePage(page) {
- this.search({ page }, false);
- this.initData();
- }
- onChangeSearch(keyword, force = false) {
- if (!force) {
- this.searchNo += 1;
- const no = this.searchNo;
- Question.searchNo({ keyword, module: 'exercise', page: 1, size: 5 }).then(result => {
- if (no !== this.searchNo) return;
- this.setState({ searchList: result.list.map(row => row.title) });
- });
- }
- this.setState({ keyword });
- }
- addSearchHistory(id) {
- My.addSearchHistory(id);
- }
- toggleCollect(index) {
- const { list } = this.props;
- const questionNo = list[index];
- if (!questionNo.collect) {
- My.addQuestionCollect(questionNo.id).then(() => {
- questionNo.collect = true;
- questionNo.collectNumber += 1;
- this.setState({ list });
- });
- } else {
- My.delQuestionCollect(questionNo.id).then(() => {
- questionNo.collect = false;
- questionNo.collectNumber -= 1;
- this.setState({ list });
- });
- }
- }
- renderView() {
- const { searchResult } = this.state;
- return (
- <div>
- {this.renderSearch()}
- {searchResult ? this.renderResult() : this.renderFilter()}
- </div>
- );
- }
- renderSearch() {
- const { searchHistoryList = [] } = this.props.user;
- const { searchList = [], keyword, focus, tip } = this.state;
- // console.log(focus, tip, searchHistoryList);
- return (
- <div className="search-layout">
- <div className="search-wrapper">
- <input
- value={keyword}
- onChange={e => this.onChangeSearch(e.target.value)}
- onFocus={() => this.setState({ focus: true })}
- onBlur={() => this.setState({ focus: false })}
- />
- <Button width={150} onClick={() => this.onSearch(keyword)}>
- <Icon className="m-r-5" type="search" />
- 搜索题目
- </Button>
- {(focus || tip) && (
- <div
- hidden={!keyword || searchList.length === 0}
- className="search-tip-wrapper"
- onMouseEnter={() => this.setState({ tip: true })}
- onMouseLeave={() => this.setState({ tip: false })}
- >
- {searchList.map(item => {
- return (
- <div
- className="t-2 t-s-16"
- onClick={() => {
- // this.onChangeSearch(item, true);
- this.onSearch(item);
- }}
- >
- {item}
- </div>
- );
- })}
- </div>
- )}
- {(focus || tip) && (
- <div
- hidden={keyword || searchHistoryList.length === 0}
- className="search-tip-wrapper"
- onMouseEnter={() => this.setState({ tip: true })}
- onMouseLeave={() => this.setState({ tip: false })}
- >
- {searchHistoryList.map((item, index) => {
- return (
- <div
- className="t-2 t-s-16"
- onClick={() => {
- // this.onChangeSearch(item, true);
- this.onSearch(item);
- }}
- >
- {item}
- <div
- className="f-r t-4 t-s-12 c-p"
- onClick={e => {
- e.stopPropagation();
- User.removeSearchIndex(index);
- }}
- >
- 删除
- </div>
- </div>
- );
- })}
- <div className="all-del t-r">
- <span className="t-4 t-s-12 c-p" onClick={() => User.clearSearch()}>
- 删除历史
- </span>
- </div>
- </div>
- )}
- </div>
- </div>
- );
- }
- renderFilter() {
- const { filterMap, sortMap } = this.state;
- const {
- subject,
- questionSubjectSelect,
- questionSubjectMap = {},
- difficultSelect,
- oneSelect,
- twoSelectMap = {},
- list = [],
- total,
- page,
- } = this.state;
- const { login } = this.props.user;
- return (
- <div className="filter-layout">
- <div className="content">
- <div style={{ right: 0, top: 0 }} className="p-a" hidden={!login}>
- <Link to="/question/search/history">
- <Assets name="history_time" className="m-r-5" /> 浏览历史 >
- </Link>
- </div>
- <Tabs
- border
- force
- type="division"
- theme="theme"
- size="small"
- space={5}
- width={220}
- active={subject}
- tabs={questionSubjectSelect}
- onChange={key => {
- this.onRefreshFilter({ subject: key, order: undefined, direction: undefined });
- }}
- />
- <UserAction
- selectList={[
- {
- key: 'questionType',
- placeholder: '题型',
- select: questionSubjectMap[subject] || [],
- },
- {
- label: '范围',
- children: [
- {
- key: 'one',
- placeholder: '全部',
- select: oneSelect,
- },
- {
- placeholder: '全部',
- key: 'two',
- be: 'one',
- selectMap: twoSelectMap,
- },
- ],
- },
- {
- right: true,
- placeholder: '难度',
- key: 'difficult',
- select: difficultSelect,
- },
- ]}
- sortList={[
- { key: 'time', label: '全站用时', fixed: true, right: true },
- { key: 'correct', label: '平均正确率', fixed: true, right: true },
- { key: 'collect_number', label: '收藏人数', fixed: true, right: true },
- ]}
- filterMap={filterMap}
- sortMap={sortMap}
- onSort={value => this.onSort(value)}
- onFilter={value => this.onFilter(value)}
- />
- {this.renderList()}
- {total > 0 && list.length > 0 && (
- <UserPagination
- total={total}
- current={page}
- pageSize={this.state.search.size}
- onChange={p => this.onChangePage(p)}
- />
- )}
- </div>
- </div>
- );
- }
- renderResult() {
- const { total, list, page } = this.state;
- return (
- <div className="result-layout">
- <div className="content">
- <div className="m-b-1">
- <span className="t-1 t-s-24">搜索结果:</span>
- <span className="t-2 t-s-18">共{total}条</span>
- </div>
- {this.renderList()}
- {total > 0 && list.length > 0 && (
- <UserPagination
- total={total}
- current={page}
- pageSize={this.state.search.size}
- onChange={p => this.onChangePage(p)}
- />
- )}
- </div>
- </div>
- );
- }
- renderList() {
- const { list } = this.state;
- return list.map(item => {
- return <SearchItem data={item} onClick={() => this.addSearchHistory(item.id)} />;
- });
- }
- }
- class SearchItem extends Component {
- render() {
- const { data = {}, onClick } = this.props;
- return (
- <div className="search-item">
- <div className="search-item-head">
- <span className="t-15 t-s-16 m-r-1">{QuestionTypeMap[data.question.questionType]}</span>
- <a className="t-1 t-s-16" href={`/question/detail/${data.id}`} target="_blank" onClick={() => onClick()}>
- {data.title}
- </a>
- <div className="f-r t-15 t-s-14">
- <span className="m-r-1">{data.question.difficult}</span>
- <span className="m-r-1">用时: {formatSeconds(data.totalTime / data.totalNumber)}</span>
- <span className="m-r-1">{formatPercent(data.totalCorrect, data.totalNumber, false)}</span>
- <span>收藏 {data.collectNumber || 0}</span>
- </div>
- </div>
- <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>
- </div>
- );
- }
- }
|