Merge pull request #405 from jorilallo/issue-282
Fixed: Unencoded search terms
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
// @flow
|
// @flow
|
||||||
import React from 'react';
|
import React, { Component } from 'react';
|
||||||
import ReactDOM from 'react-dom';
|
import ReactDOM from 'react-dom';
|
||||||
import keydown from 'react-keydown';
|
import keydown from 'react-keydown';
|
||||||
import { observable, action } from 'mobx';
|
import { observable, action } from 'mobx';
|
||||||
@@ -55,21 +55,21 @@ const StyledArrowKeyNavigation = styled(ArrowKeyNavigation)`
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
@observer
|
@observer
|
||||||
class Search extends React.Component {
|
class Search extends Component {
|
||||||
firstDocument: HTMLElement;
|
firstDocument: HTMLElement;
|
||||||
props: Props;
|
props: Props;
|
||||||
|
|
||||||
@observable resultIds: string[] = []; // Document IDs
|
@observable resultIds: string[] = []; // Document IDs
|
||||||
@observable searchTerm: ?string = null;
|
@observable query: string = '';
|
||||||
@observable isFetching = false;
|
@observable isFetching = false;
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
this.updateSearchResults();
|
this.handleQueryChange();
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidUpdate(prevProps) {
|
componentDidUpdate(prevProps) {
|
||||||
if (prevProps.match.params.query !== this.props.match.params.query) {
|
if (prevProps.match.params.query !== this.props.match.params.query) {
|
||||||
this.updateSearchResults();
|
this.handleQueryChange();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,24 +90,26 @@ class Search extends React.Component {
|
|||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
if (this.firstDocument) {
|
if (this.firstDocument) {
|
||||||
const element = ReactDOM.findDOMNode(this.firstDocument);
|
const element = ReactDOM.findDOMNode(this.firstDocument);
|
||||||
// $FlowFixMe
|
if (element instanceof HTMLElement) element.focus();
|
||||||
if (element && element.focus) element.focus();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
updateSearchResults = _.debounce(() => {
|
handleQueryChange = () => {
|
||||||
this.search(this.props.match.params.query);
|
const query = this.props.match.params.query;
|
||||||
}, 250);
|
this.query = query ? decodeURIComponent(query) : '';
|
||||||
|
this.fetchResultsDebounced();
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchResultsDebounced = _.debounce(this.fetchResults, 250);
|
||||||
|
|
||||||
@action
|
@action
|
||||||
search = async (query: string) => {
|
fetchResults = async () => {
|
||||||
this.searchTerm = query;
|
|
||||||
this.isFetching = true;
|
this.isFetching = true;
|
||||||
|
|
||||||
if (query) {
|
if (this.query) {
|
||||||
try {
|
try {
|
||||||
this.resultIds = await this.props.documents.search(query);
|
this.resultIds = await this.props.documents.search(this.query);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Something went wrong');
|
console.error('Something went wrong');
|
||||||
}
|
}
|
||||||
@@ -118,7 +120,7 @@ class Search extends React.Component {
|
|||||||
this.isFetching = false;
|
this.isFetching = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
updateQuery = query => {
|
updateLocation = query => {
|
||||||
this.props.history.replace(searchUrl(query));
|
this.props.history.replace(searchUrl(query));
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -127,7 +129,7 @@ class Search extends React.Component {
|
|||||||
};
|
};
|
||||||
|
|
||||||
get title() {
|
get title() {
|
||||||
const query = this.searchTerm;
|
const query = this.query;
|
||||||
const title = 'Search';
|
const title = 'Search';
|
||||||
if (query) return `${query} - ${title}`;
|
if (query) return `${query} - ${title}`;
|
||||||
return title;
|
return title;
|
||||||
@@ -135,9 +137,8 @@ class Search extends React.Component {
|
|||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { documents, notFound } = this.props;
|
const { documents, notFound } = this.props;
|
||||||
const query = this.props.match.params.query;
|
|
||||||
const hasResults = this.resultIds.length > 0;
|
const hasResults = this.resultIds.length > 0;
|
||||||
const showEmpty = !this.isFetching && this.searchTerm && !hasResults;
|
const showEmpty = !this.isFetching && this.query && !hasResults;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Container auto>
|
<Container auto>
|
||||||
@@ -151,12 +152,11 @@ class Search extends React.Component {
|
|||||||
)}
|
)}
|
||||||
<ResultsWrapper pinToTop={hasResults} column auto>
|
<ResultsWrapper pinToTop={hasResults} column auto>
|
||||||
<SearchField
|
<SearchField
|
||||||
searchTerm={this.searchTerm}
|
|
||||||
onKeyDown={this.handleKeyDown}
|
onKeyDown={this.handleKeyDown}
|
||||||
onChange={this.updateQuery}
|
onChange={this.updateLocation}
|
||||||
value={query || ''}
|
value={this.query}
|
||||||
/>
|
/>
|
||||||
{showEmpty && <Empty>Oop, no matching documents.</Empty>}
|
{showEmpty && <Empty>No matching documents.</Empty>}
|
||||||
<ResultList visible={hasResults}>
|
<ResultList visible={hasResults}>
|
||||||
<StyledArrowKeyNavigation
|
<StyledArrowKeyNavigation
|
||||||
mode={ArrowKeyNavigation.mode.VERTICAL}
|
mode={ArrowKeyNavigation.mode.VERTICAL}
|
||||||
@@ -172,7 +172,7 @@ class Search extends React.Component {
|
|||||||
}
|
}
|
||||||
key={documentId}
|
key={documentId}
|
||||||
document={document}
|
document={document}
|
||||||
highlight={this.searchTerm}
|
highlight={this.query}
|
||||||
showCollection
|
showCollection
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ export function newDocumentUrl(collection: Collection): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function searchUrl(query?: string): string {
|
export function searchUrl(query?: string): string {
|
||||||
if (query) return `/search/${query}`;
|
if (query) return `/search/${encodeURIComponent(query)}`;
|
||||||
return `/search`;
|
return `/search`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -134,7 +134,7 @@
|
|||||||
"react-medium-image-zoom": "^3.0.2",
|
"react-medium-image-zoom": "^3.0.2",
|
||||||
"react-modal": "^3.1.2",
|
"react-modal": "^3.1.2",
|
||||||
"react-portal": "^4.0.0",
|
"react-portal": "^4.0.0",
|
||||||
"react-router-dom": "^4.1.1",
|
"react-router-dom": "^4.2.0",
|
||||||
"redis": "^2.6.2",
|
"redis": "^2.6.2",
|
||||||
"redis-lock": "^0.1.0",
|
"redis-lock": "^0.1.0",
|
||||||
"rimraf": "^2.5.4",
|
"rimraf": "^2.5.4",
|
||||||
|
|||||||
@@ -7331,9 +7331,9 @@ react-proxy@^1.1.7:
|
|||||||
lodash "^4.6.1"
|
lodash "^4.6.1"
|
||||||
react-deep-force-update "^1.0.0"
|
react-deep-force-update "^1.0.0"
|
||||||
|
|
||||||
react-router-dom@^4.1.1:
|
react-router-dom@^4.2.0:
|
||||||
version "4.2.2"
|
version "4.2.2"
|
||||||
resolved "https://registry.yarnpkg.com/react-router-dom/-/react-router-dom-4.2.2.tgz#c8a81df3adc58bba8a76782e946cbd4eae649b8d"
|
resolved "https://registry.npmjs.org/react-router-dom/-/react-router-dom-4.2.2.tgz#c8a81df3adc58bba8a76782e946cbd4eae649b8d"
|
||||||
dependencies:
|
dependencies:
|
||||||
history "^4.7.2"
|
history "^4.7.2"
|
||||||
invariant "^2.2.2"
|
invariant "^2.2.2"
|
||||||
|
|||||||
Reference in New Issue
Block a user