Skip to content

Commit

Permalink
Merge pull request #216 from manu-unbxd/master
Browse files Browse the repository at this point in the history
fix(handling backbutton for queryparams): sdk was not re rendering wh…
  • Loading branch information
divyadaglia-unbxd authored Apr 28, 2022
2 parents adecbd7 + adc445a commit 99d6560
Show file tree
Hide file tree
Showing 4 changed files with 895 additions and 779 deletions.
10 changes: 8 additions & 2 deletions demo/src/pages/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ const Search = () => {
} else {
// if hash already exists, to retain the current state, push on history
if (routeLocation.hash && routeHistory.action !== 'POP') {
routeHistory.push(`${routeLocation.pathname}#${hash}`);
routeHistory.push(`${routeLocation.pathname}?${hash}`);
} else if (hash) {
routeHistory.replace(`${routeLocation.pathname}#${hash}`);
routeHistory.replace(`${routeLocation.pathname}?${hash}`);
}
return true;
}
Expand All @@ -105,6 +105,12 @@ const Search = () => {
loaderComponent={<LoaderComponent />}
errorComponent={<ErrorComponent />}
onRouteChange={handleRouteChange}
onUrlBack = {
function() {
this.state.unbxdCore.state.isBack = true;
this.state.unbxdCore.renderFromUrl(window.location.search.replace('?',''));
}
}
>
<MobileModal showFilters={showFilters} handleClose={handleClose} />
<SearchBar
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
"@semantic-release/git": "^9.0.0",
"@semantic-release/release-notes-generator": "^9.0.1",
"@storybook/theming": "^6.2.9",
"@unbxd-ui/unbxd-search-core": "^0.4.4",
"@unbxd-ui/unbxd-search-core": "^0.4.6",
"prop-types": "^15.7.2",
"semantic-release": "^17.3.0"
},
Expand Down
18 changes: 11 additions & 7 deletions src/UnbxdSearchWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ class UnbxdSearchWrapper extends Component {

componentDidMount() {
const { unbxdCore } = this.state;
const {
onUrlBack
} = this.props;
const { trackCategory } = this.getAnalytics();
const categoryId =
typeof unbxdCore.options.getCategoryId === 'function' &&
Expand Down Expand Up @@ -154,17 +157,18 @@ class UnbxdSearchWrapper extends Component {
};
});
unbxdCore.options.productType = productTypes.CATEGORY;
const urlParams = unbxdCore.getQueryParams();
if (Object.keys(urlParams).length > 1) {
unbxdCore.renderFromUrl();
} else {
unbxdCore.getResults();
}
unbxdCore.getResults();
trackCategory(window.UnbxdAnalyticsConf);
}

if (unbxdCore.options.hashMode) {
window.onhashchange = unbxdCore.onLocationChange.bind(unbxdCore);
window.onhashchange = onUrlBack ? onUrlBack.bind(this)(unbxdCore): unbxdCore.onLocationChange.bind(unbxdCore);
} else {
const backHandler = onUrlBack ? onUrlBack.bind(this): ()=>{
unbxdCore.state.isBack = true;
unbxdCore.renderFromUrl(window.location.search.replace('?',''));
}
window.addEventListener('popstate', backHandler);
}
}

Expand Down
Loading

0 comments on commit 99d6560

Please sign in to comment.