Skip to content

Commit

Permalink
fix(Search): dont query below 3 chars
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Bouquillon committed Oct 3, 2018
1 parent f6ece01 commit 10d9893
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
11 changes: 6 additions & 5 deletions src/search/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const FormSearchButton = () => (
</button>
);

// Memoize fetch calls (fetch returns a promise).
const fetchResults = (query, endPoint = "search") => {
const url = `${process.env.API_URL}/${endPoint}?q=${query}`;
return fetch(url).then(response => {
Expand All @@ -44,17 +43,19 @@ const fetchResults = (query, endPoint = "search") => {
});
};

// memoize search results
const fetchResultsSearch = memoizee(
query => {
return fetchResults(query, "search");
},
{ promise: true }
);

// memoize suggestions results
const fetchResultsSuggest = memoizee(
query => {
return fetchResults(query, "suggest");
},
query =>
(query && query.length > 2 && fetchResults(query, "suggest")) ||
Promise.resolve(),
{ promise: true }
);

Expand Down Expand Up @@ -129,7 +130,7 @@ class Search extends React.Component {
<Suggester
onChange={this.onChange}
query={query}
fetch={() => fetchResultsSuggest(query)}
getResults={() => fetchResultsSuggest(query)}
/>
<FormSearchButton />
</form>
Expand Down
8 changes: 4 additions & 4 deletions src/search/Suggester.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class Suggester extends React.Component {
}

render() {
const { fetch, query, onChange } = this.props;
const { getResults, query, onChange } = this.props;
const inputProps = {
placeholder: "Posez votre question",
"aria-label": "Posez votre question",
Expand All @@ -129,7 +129,7 @@ class Suggester extends React.Component {
};
return (
<AsyncFetch
fetch={() => fetch(query)}
fetch={() => getResults(query)}
render={({ status, result, fetch, clear }) => (
<Autosuggest
theme={suggesterTheme}
Expand Down Expand Up @@ -159,13 +159,13 @@ Suggester.propTypes = {
// on input DOM change
onChange: PropTypes.func,
// the fetch call function
fetch: PropTypes.func.isRequired,
getResults: PropTypes.func.isRequired,
query: PropTypes.string
};

Suggester.defaultProps = {
query: "",
fetch: () => {},
getResults: () => {},
onChange: () => {}
};

Expand Down

0 comments on commit 10d9893

Please sign in to comment.