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, '$1'); } } 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 (
{this.renderSearch()} {searchResult ? this.renderResult() : this.renderFilter()}
); } renderSearch() { const { searchHistoryList = [] } = this.props.user; const { searchList = [], keyword, focus, tip } = this.state; // console.log(focus, tip, searchHistoryList); return (
this.onChangeSearch(e.target.value)} onFocus={() => this.setState({ focus: true })} onBlur={() => this.setState({ focus: false })} /> {(focus || tip) && ( )} {(focus || tip) && ( )}
); } renderFilter() { const { filterMap, sortMap } = this.state; const { subject, questionSubjectSelect, questionSubjectMap = {}, difficultSelect, oneSelect, twoSelectMap = {}, list = [], total, page, } = this.state; const { login } = this.props.user; return (
{ this.onRefreshFilter({ subject: key, order: undefined, direction: undefined }); }} /> this.onSort(value)} onFilter={value => this.onFilter(value)} /> {this.renderList()} {total > 0 && list.length > 0 && ( this.onChangePage(p)} /> )}
); } renderResult() { const { total, list, page } = this.state; return (
搜索结果: 共{total}条
{this.renderList()} {total > 0 && list.length > 0 && ( this.onChangePage(p)} /> )}
); } renderList() { const { list } = this.state; return list.map(item => { return this.addSearchHistory(item.id)} />; }); } } class SearchItem extends Component { render() { const { data = {}, onClick } = this.props; return (
{QuestionTypeMap[data.question.questionType]} onClick()}> {data.title}
{data.question.difficult} 用时: {formatSeconds(data.totalTime / data.totalNumber)} {formatPercent(data.totalCorrect, data.totalNumber, false)} 收藏 {data.collectNumber || 0}
); } }