Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement infinite scrolling for document search #3855

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 42 additions & 26 deletions ui/src/viewers/PdfViewerSearch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { FormattedMessage } from 'react-intl';
import classNames from 'classnames';
import { connect } from 'react-redux';
import queryString from 'query-string';
import { SectionLoading, SearchHighlight } from 'components/common';
import {
SectionLoading,
SearchHighlight,
QueryInfiniteLoad,
} from 'components/common';
import { Classes } from '@blueprintjs/core';
import c from 'classnames';
import { queryEntities } from 'actions';
Expand All @@ -17,13 +21,13 @@ class PdfViewerSearch extends Component {
}

componentDidMount() {
this.fetchPage();
this.fetchSearchResults();
}

componentDidUpdate(prevProps) {
const { query } = this.props;
if (!query.sameAs(prevProps.query)) {
this.fetchPage();
this.fetchSearchResults();
}
}

Expand All @@ -40,47 +44,37 @@ class PdfViewerSearch extends Component {
return `#${queryString.stringify(hashQuery)}`;
}

fetchPage() {
fetchSearchResults() {
const { query, result } = this.props;
if (!!query.getString('q') && result.shouldLoad) {
this.props.queryEntities({ query });
}
}

render() {
const { page, dir, result } = this.props;
const { result } = this.props;

if (result.total === undefined) {
return <SectionLoading />;
}

return (
<div className="pages">
{result.total === 0 && (
<>
<div
className={c(
Classes.CALLOUT,
Classes.INTENT_WARNING,
`${Classes.ICON}-search`
)}
>
<FormattedMessage
id="document.search.no_match"
defaultMessage="No single page within this document matches all your search terms."
/>
</div>
{this.props.children}
</>
)}
{result.total === 0 ? this.renderEmptyState() : this.renderResults()}
</div>
);
}

renderResults() {
const { dir, result, query } = this.props;

return (
<>
<ul>
{result.results.map((res) => (
<li key={`page-${res.id}`}>
<p dir={dir}>
<Link
to={this.getResultLink(res)}
className={classNames({ active: page === res.index })}
>
<Link to={this.getResultLink(res)}>
<span
className={c(Classes.ICON, `${Classes.ICON}-document`)}
/>
Expand All @@ -97,6 +91,28 @@ class PdfViewerSearch extends Component {
</li>
))}
</ul>
<QueryInfiniteLoad
query={query}
result={result}
fetch={queryEntities}
/>
</>
);
}

renderEmptyState() {
return (
<div
className={c(
Classes.CALLOUT,
Classes.INTENT_WARNING,
`${Classes.ICON}-search`
)}
>
<FormattedMessage
id="document.search.no_match"
defaultMessage="No single page within this document matches all your search terms."
/>
</div>
);
}
Expand Down
Loading