import React, { Component } from 'react'; import './index.less'; import { Icon } 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, }; }), }; } initData() { const data = Object.assign(this.state, this.state.search); data.filterMap = this.state.search; if (data.order) { data.sortMap = { [data.order]: data.direction }; } 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 => { 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({ keyword }, false); // this.changeQuery({ keyword }); // this.setState({ keyword }); this.refreshQuery({ keyword: value || keyword }); // this.initData(); } onSort(value) { const keys = Object.keys(value); // this.search({ order: keys.length ? keys.join('|') : null, direction: keys.length ? Object.values(value).join('|') : null }); const { sortMap } = this.state; const index = keys.length > 1 && sortMap[keys[0]] ? 1 : 0; this.search({ order: keys.length && value[keys[index]] ? keys[index] : null, direction: keys.length ? value[keys[index]] : null }, 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 })} /> 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.totalNumer)} {formatPercent(data.totalCorrect, data.totalNumber, false)} 收藏 {data.collectNumber || 0}
{data.question.description}
); } }