Skip to content

Commit

Permalink
fix(read search query on back): fetch the current search query on bro…
Browse files Browse the repository at this point in the history
…wser back
  • Loading branch information
sanjithkumar017 committed Dec 29, 2020
1 parent 038b609 commit 687c77c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion demo/src/components/SearchBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const SearchBar = (props) => {
const handleSubmit = () => {
if (productType !== 'SEARCH') {
onSearch('SEARCH');
history.push('/');
}
history.push('/');
return true;
};
return (
Expand Down
3 changes: 2 additions & 1 deletion src/UnbxdSearchWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ const initialUnbxdState = {
selectedRangeFacets: { add: {}, remove: {}, list: {} },
applyMultiple: false,
pageSize: 10,
sort: ''
sort: '',
query: ''
};

class UnbxdSearchWrapper extends Component {
Expand Down
6 changes: 5 additions & 1 deletion src/modules/searchBox/SearchBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ const SearchBox = (props) => {
unbxdCore,
unbxdCoreStatus,
helpers,
productType
productType,
unbxdState
} = appState;

const { query } = unbxdState;

return (
<SearchBoxContainer
unbxdCore={unbxdCore}
unbxdCoreStatus={unbxdCoreStatus}
helpers={helpers}
productType={productType}
query={query}
{...props}
/>
);
Expand Down
15 changes: 15 additions & 0 deletions src/modules/searchBox/SearchBoxContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ class SearchBoxContainer extends React.PureComponent {
manageRangeFacets(null, null, null, manageStateTypes.RESET);
}

componentDidUpdate(prevProps) {
const { unbxdCore, unbxdCoreStatus, query } = this.props;
const { q: currentQuery } = unbxdCore.getQueryParams();
if (
unbxdCoreStatus !== prevProps.unbxdCoreStatus &&
unbxdCoreStatus === 'LOADING' &&
typeof currentQuery === 'string' &&
currentQuery.length > 0 &&
currentQuery !== query &&
prevProps.query === query
) {
this.setState({ query: currentQuery });
}
}

getSearchBoxProps() {
const {
unbxdCore,
Expand Down
3 changes: 2 additions & 1 deletion src/utils/setSearchBoxConfiguration.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ function setSearchBoxConfiguration(config) {
return {
...existingState,
productType: productTypes.SEARCH,
categoryId: ''
categoryId: '',
unbxdState: { ...existingState.unbxdState, query: config.query }
};
});
}
Expand Down

0 comments on commit 687c77c

Please sign in to comment.