fix: search page is populated with relevant results when search bar is submitted

This commit is contained in:
Valeriy
2019-07-27 22:23:09 +03:00
committed by Kristofer Koishigawa
parent 72f6201bb7
commit c44891a4a2
2 changed files with 22 additions and 8 deletions

View File

@ -4,6 +4,7 @@ import PropTypes from 'prop-types';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { InstantSearch, Configure } from 'react-instantsearch-dom'; import { InstantSearch, Configure } from 'react-instantsearch-dom';
import qs from 'query-string'; import qs from 'query-string';
import { navigate } from 'gatsby';
import { import {
isSearchDropdownEnabledSelector, isSearchDropdownEnabledSelector,
@ -14,6 +15,8 @@ import {
import { createSelector } from 'reselect'; import { createSelector } from 'reselect';
const DEBOUNCE_TIME = 400;
const propTypes = { const propTypes = {
children: PropTypes.any, children: PropTypes.any,
isDropdownEnabled: PropTypes.bool, isDropdownEnabled: PropTypes.bool,
@ -56,7 +59,20 @@ class InstantSearchRoot extends Component {
if (location !== prevProps.location) { if (location !== prevProps.location) {
const { query, updateSearchQuery } = this.props; const { query, updateSearchQuery } = this.props;
if (this.isSearchPage()) { if (this.isSearchPage()) {
if (
location.state &&
typeof location.state === 'object' &&
location.state.hasOwnProperty('query')
) {
updateSearchQuery(location.state.query);
} else if (location.search) {
this.setQueryFromURL(); this.setQueryFromURL();
} else {
navigate(searchStateToUrl(this.props.location, query), {
state: { query },
replace: true
});
}
} else if (query) { } else if (query) {
updateSearchQuery(''); updateSearchQuery('');
} }
@ -90,13 +106,11 @@ class InstantSearchRoot extends Component {
this.debouncedSetState = setTimeout(() => { this.debouncedSetState = setTimeout(() => {
if (this.isSearchPage()) { if (this.isSearchPage()) {
window.history.pushState( navigate(searchStateToUrl(this.props.location, query), {
{ query }, state: { query }
null, });
searchStateToUrl(this.props.location, query)
);
} }
}, 400); }, DEBOUNCE_TIME);
} }
}; };

View File

@ -85,7 +85,7 @@ class SearchBar extends Component {
if (query) { if (query) {
updateSearchQuery(query); updateSearchQuery(query);
} }
return navigate(`/search${query ? `?query=${query}` : ''}`); return navigate('/search');
} }
render() { render() {