Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
subscribe(searchRouteWasUpdated$, ({ dispatch, getState }) => {
// Reset the current activeFilters object and update the search phrase with the new one
const searchPhrase = getSearchPhrase(getState());
dispatch(setActiveFilters({}, { searchPhrase }));
dispatch(getFilters());
});
subscribe(searchRouteDidEnter$, ({ dispatch, getState }) => {
const state = getState();
// If search input is empty, set it to the value of the search query param.
if (!state.navigator.searchPhrase) {
dispatch(setSearchPhrase(getSearchPhrase(state)));
}
});
let temporaryFilters;
if (roundDisplayAmounts) {
temporaryFilters = getTemporaryFiltersWithRoundedDisplayAmounts(state);
} else {
temporaryFilters = getTemporaryFilters(state);
}
if (!activeFilters.length) {
logger.error('Tried to submit temporary filters, but no active filter stack was created.');
return;
}
if (!shallowEqual(temporaryFilters, activeFilters[activeFilters.length - 1].filters)) {
const categoryId = getCurrentCategoryId(state);
const searchPhrase = getSearchPhrase(state);
dispatch(setActiveFilters(temporaryFilters, {
...categoryId && { categoryId },
...searchPhrase && { searchPhrase },
}));
const params = {
...categoryId && { categoryId },
...searchPhrase && { searchPhrase },
limit: ITEMS_PER_LOAD,
offset: 0,
sort: getSortOrder(state),
};
// Enrich the parameters with the getProducts related properties for the initial product "page".
setTimeout(() => {
const getSearchResults = (offset = 0) => (dispatch, getState) => {
const state = getState();
const limit = ITEMS_PER_LOAD;
const searchPhrase = getSearchPhrase(state);
if (!searchPhrase) {
return;
}
const sort = getSortOrder(state);
const promise = dispatch(getProducts({
params: {
searchPhrase,
offset,
limit,
sort,
},
onBeforeDispatch: () => {
// Dispatch the request action before the related pipeline request is executed.
if (!searchActive) {
// Reset search phrase.
dispatch(setNavigatorSearchPhrase(''));
// Show search input.
dispatch(toggleNavSearchField(true));
return;
} else if (!searchPhrase) {
// Hide search input.
dispatch(toggleNavSearchField(false));
return;
}
// Perform search by entering/updating search route.
const { history } = state;
const { sort, ...otherParams } = history.queryParams;
const prevSearchPhrase = getSearchPhrase(state);
dispatch(setViewTop(true));
// Set up next history location.
const historyLocation = {
pathname: SEARCH_PATH,
params: {
...otherParams,
s: searchPhrase,
},
};
// Check if we are already on the search route.
if (history.pathname.startsWith(SEARCH_PATH)) {
if (searchPhrase !== prevSearchPhrase) {
// Preserve current history state
if (!searchActive) {
// Reset search phrase
dispatch(setSearchPhrase(''));
// Show search input
dispatch(toggleSearch(true));
return;
} else if (!searchPhrase) {
// Hide search input
dispatch(toggleSearch(false));
return;
}
// Perform search by entering/updating search route
const { history } = state;
const { sort, ...otherParams } = history.queryParams;
const prevSearchPhrase = getSearchPhrase(state);
// Set up next history location.
const historyLocation = {
pathname: SEARCH_PATH,
params: {
...otherParams,
s: searchPhrase,
},
state: {
// Force view to scroll back to top
viewTop: true,
},
};
// Check if we are already on the search route
if (history.pathname.startsWith(SEARCH_PATH)) {
const buildFilterParams = (state) => {
const categoryId = getCurrentCategoryId(state);
const searchPhrase = getSearchPhrase(state);
return {
...categoryId && { categoryId },
...searchPhrase && { searchPhrase },
};
};
const setSearchPhrase = query => (dispatch, getState) => {
const searchPhrase = getSearchPhrase(getState()) || '';
if (query === searchPhrase) {
return;
}
dispatch(setPhrase(query));
};
const mapStateToProps = state => ({
isLoading: isViewLoading(state, SEARCH_PATH),
isFilterBarShown: isFilterBarShown(state),
searchPhrase: getSearchPhrase(state),
...getProductsResult(state),
});