1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-20 19:08:21 +00:00

['search' page] bugfix for 'total', 'prev/next' & etc.

This commit is contained in:
llj
2021-12-17 15:14:51 +08:00
parent beea098437
commit ac45f2b1b1
2 changed files with 23 additions and 15 deletions

View File

@@ -1,5 +1,4 @@
import React from 'react'; import React from 'react';
import moment from 'moment';
import { gettext } from '../../utils/constants'; import { gettext } from '../../utils/constants';
import { seafileAPI } from '../../utils/seafile-api'; import { seafileAPI } from '../../utils/seafile-api';
import SearchResults from './search-results'; import SearchResults from './search-results';
@@ -52,17 +51,19 @@ class SearchViewPanel extends React.Component {
}); });
const stateHistory = _.cloneDeep(this.state); const stateHistory = _.cloneDeep(this.state);
seafileAPI.searchFiles(params, null).then(res => { seafileAPI.searchFiles(params, null).then(res => {
const { results, has_more, total } = res.data;
this.setState({ this.setState({
isLoading: false, isLoading: false,
isResultGot: true, isResultGot: true,
resultItems: res.data.results, resultItems: results,
hasMore: res.data.has_more, hasMore: has_more,
total: total,
page: params.page, page: params.page,
isShowSearchFilter: true, isShowSearchFilter: true,
}); });
this.stateHistory = stateHistory; this.stateHistory = stateHistory;
this.stateHistory.resultItems = res.data.results; this.stateHistory.resultItems = results;
this.stateHistory.hasMore = res.data.has_more; this.stateHistory.hasMore = has_more;
this.stateHistory.page = params.page; this.stateHistory.page = params.page;
}).catch((error) => { }).catch((error) => {
this.setState({ isLoading: false }); this.setState({ isLoading: false });
@@ -145,7 +146,8 @@ class SearchViewPanel extends React.Component {
}); });
} }
handlePrevious = () => { handlePrevious = (e) => {
e.preventDefault();
if (this.stateHistory && this.state.page > 1) { if (this.stateHistory && this.state.page > 1) {
this.setState(this.stateHistory,() => { this.setState(this.stateHistory,() => {
const params = this.handleSearchParams(this.state.page - 1); const params = this.handleSearchParams(this.state.page - 1);
@@ -156,7 +158,8 @@ class SearchViewPanel extends React.Component {
} }
}; };
handleNext = () => { handleNext = (e) => {
e.preventDefault();
if (this.stateHistory && this.state.hasMore) { if (this.stateHistory && this.state.hasMore) {
this.setState(this.stateHistory,() => { this.setState(this.stateHistory,() => {
const params = this.handleSearchParams(this.state.page + 1); const params = this.handleSearchParams(this.state.page + 1);
@@ -321,12 +324,17 @@ class SearchViewPanel extends React.Component {
/> />
</div> </div>
{this.state.isLoading && <Loading/>} {this.state.isLoading && <Loading/>}
{(!this.state.isLoading && this.state.isResultGot) && <SearchResults resultItems={this.state.resultItems}/>} {(!this.state.isLoading && this.state.isResultGot) &&
<SearchResults
resultItems={this.state.resultItems}
total={this.state.total}
/>
}
{(!this.state.isLoading && this.state.isResultGot) && {(!this.state.isLoading && this.state.isResultGot) &&
<div className="paginator"> <div className="paginator">
{this.state.page !== 1 && <a href="#" onClick={() => this.handlePrevious()}>{gettext('Previous')}</a>} {this.state.page !== 1 && <a href="#" onClick={this.handlePrevious}>{gettext('Previous')}</a>}
{(this.state.page !== 1 && this.state.hasMore) && <span> | </span>} {(this.state.page !== 1 && this.state.hasMore) && <span> | </span>}
{this.state.hasMore && <a href="#" onClick={() => this.handleNext()}>{gettext('Next')}</a>} {this.state.hasMore && <a href="#" onClick={this.handleNext}>{gettext('Next')}</a>}
</div> </div>
} }
</div> </div>

View File

@@ -39,10 +39,10 @@ class ResultsItem extends React.Component {
<img className={linkContent ? 'item-img' : 'lib-item-img'} src={fileIconUrl} alt=""/> <img className={linkContent ? 'item-img' : 'lib-item-img'} src={fileIconUrl} alt=""/>
<div className="item-content"> <div className="item-content">
<div className="item-name ellipsis"> <div className="item-name ellipsis">
<a href={this.handlerFileURL(item)} target="_blank" title={item.name}>{item.name}</a> <a href={this.handlerFileURL(item)} target="_blank" title={item.name} rel="noreferrer">{item.name}</a>
</div> </div>
<div className="item-link ellipsis"> <div className="item-link ellipsis">
<a href={this.handlerParentDirURL(item)} target="_blank" >{item.repo_name}{this.handlerParentDirPath(item)}</a> <a href={this.handlerParentDirURL(item)} target="_blank" rel="noreferrer" >{item.repo_name}{this.handlerParentDirPath(item)}</a>
</div> </div>
<div className="item-link ellipsis"> <div className="item-link ellipsis">
{Utils.bytesToSize(item.size) + ' ' + moment(item.last_modified * 1000).format('YYYY-MM-DD')} {Utils.bytesToSize(item.size) + ' ' + moment(item.last_modified * 1000).format('YYYY-MM-DD')}
@@ -67,12 +67,11 @@ class SearchResults extends React.Component {
} }
render() { render() {
const { resultItems } = this.props; const { resultItems, total } = this.props;
const total = resultItems.length;
return ( return (
<div className="search-result-container position-static"> <div className="search-result-container position-static">
<p className="tip">{total > 0 ? (total + ' ' + (total === 1 ? gettext('result') : gettext('results'))) : gettext('No result')}</p>
<ul className="search-result-list"> <ul className="search-result-list">
<p className="tip">{total > 0 ? (total + ' ' + (total === 1 ? gettext('result') : gettext('results'))) : gettext('No result')}</p>
{resultItems.map((item, index) => { {resultItems.map((item, index) => {
return <ResultsItem key={index} item={item}/>; return <ResultsItem key={index} item={item}/>;
})} })}
@@ -84,6 +83,7 @@ class SearchResults extends React.Component {
const searchResultsPropTypes = { const searchResultsPropTypes = {
resultItems: PropTypes.array.isRequired, resultItems: PropTypes.array.isRequired,
total: PropTypes.number.isRequired
}; };
SearchResults.propTypes = searchResultsPropTypes; SearchResults.propTypes = searchResultsPropTypes;