Skip to content

Commit

Permalink
fix(handle redirects): use onRouteChange to handle redirects
Browse files Browse the repository at this point in the history
a customizable hook to handle redirects and url updates.
  • Loading branch information
sanjithkumar017 committed Jan 12, 2021
1 parent bf01834 commit d5328bc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
42 changes: 37 additions & 5 deletions demo/src/pages/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ const ErrorComponent = () => {
};

const Search = () => {
const [productType, setProductType] = useContext(ProductTypeContext);
const {
productType,
setProductType,
enableFilters,
setEnableFilters
} = useContext(ProductTypeContext);
const [categoryPathLinks, setCategoryPathLinks] = useState(categoryLinks);
const [refreshId, setRefreshId] = useState(1);
const [showFilters, setShowFilters] = useState(false);
Expand Down Expand Up @@ -61,12 +66,38 @@ const Search = () => {
setRefreshId(refreshId + 1);
};

const handleRouteChange = (hash) => {
const handleRouteChange = (searchObj, hash, refreshId) => {
scrollTop();
if (routeLocation.hash && routeHistory.action !== 'POP') {
routeHistory.push(`${routeLocation.pathname}#${hash}`);
setEnableFilters(true);
const { state = {} } = searchObj;
const { responseObj = {} } = state;
const { redirect = {} } = responseObj;
const { type = '', value } = redirect;
const urlParams = searchObj.getQueryParams();
if (
routeLocation.pathname === '/' &&
Object.keys(urlParams).length === 0 &&
refreshId
) {
return false;
} else if (
type === 'url' &&
typeof value === 'string' &&
value.length > 0
) {
if (routeLocation.pathname === '/' && routeLocation.hash) {
routeHistory.push(value);
} else {
routeHistory.replace(value);
}
return true;
} else {
routeHistory.replace(`${routeLocation.pathname}#${hash}`);
if (routeLocation.hash && routeHistory.action !== 'POP') {
routeHistory.push(`${routeLocation.pathname}#${hash}`);
} else {
routeHistory.replace(`${routeLocation.pathname}#${hash}`);
}
return true;
}
};

Expand Down Expand Up @@ -94,6 +125,7 @@ const Search = () => {
categoryPathLinks={categoryPathLinks}
handleCategoryLinkClick={handleCategoryLinkClick}
handleShow={handleShow}
enableFilters={enableFilters}
/>

<Route exact path="/">
Expand Down
16 changes: 10 additions & 6 deletions src/UnbxdSearchWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class UnbxdSearchWrapper extends Component {

componentDidUpdate(prevProps) {
const { refreshId } = prevProps;
const { productType } = this.props;
const { productType, onRouteChange } = this.props;
const {
unbxdCore,
categoryId,
Expand Down Expand Up @@ -215,11 +215,15 @@ class UnbxdSearchWrapper extends Component {
renderFromUrl();
} else if (refreshId !== this.props.refreshId) {
this.resetSearch();
unbxdCore.options.productType = productType;
if (productType === productTypes.SEARCH) {
getResults(query);
} else {
getResults();
if (onRouteChange(unbxdCore, '', refreshId)) {
unbxdCore.options.productType = productType;
const currentQuery =
urlParams[unbxdCore.options.searchQueryParam];
if (productType === productTypes.SEARCH) {
getResults(currentQuery);
} else {
getResults();
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/unbxdCallBack.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function unbxdCallBack(unbxdSearchObj, eventName, data) {
});
const { onRouteChange } = this.props;
if (typeof onRouteChange === 'function') {
onRouteChange(unbxdSearchObj.getStateString());
onRouteChange(unbxdSearchObj, unbxdSearchObj.getStateString());
}
}

Expand Down

0 comments on commit d5328bc

Please sign in to comment.