Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX#PWA-3161 [Cloud] Search Term Redirect not working in PWA #4169

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/peregrine/lib/talons/SearchPage/searchPage.gql.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ export const GET_PRODUCT_FILTERS_BY_SEARCH = gql`
}
`;

export const GET_SEARCH_TERM_DATA = gql`
query getSearchTermData($search: String) {
searchTerm(Search: $search) {
query_text
redirect
popularity
}
}
`;

export const PRODUCT_SEARCH = gql`
query ProductSearch(
$currentPage: Int = 1
Expand Down Expand Up @@ -106,6 +116,7 @@ export const GET_SEARCH_AVAILABLE_SORT_METHODS = gql`
export default {
getFilterInputsQuery: GET_FILTER_INPUTS,
getPageSize: GET_PAGE_SIZE,
getSearchTermData: GET_SEARCH_TERM_DATA,
getProductFiltersBySearchQuery: GET_PRODUCT_FILTERS_BY_SEARCH,
getSearchAvailableSortMethods: GET_SEARCH_AVAILABLE_SORT_METHODS,
productSearchQuery: PRODUCT_SEARCH
Expand Down
25 changes: 24 additions & 1 deletion packages/peregrine/lib/talons/SearchPage/useSearchPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const useSearchPage = (props = {}) => {
const {
getFilterInputsQuery,
getPageSize,
getSearchTermData,
getProductFiltersBySearchQuery,
getSearchAvailableSortMethods,
productSearchQuery
Expand All @@ -44,6 +45,18 @@ export const useSearchPage = (props = {}) => {
}
);

const [getSearchTermMethod, { data: SearchTermQueryData }] = useLazyQuery(
getSearchTermData
);

if (SearchTermQueryData !== undefined) {
const [...redirectData] = [SearchTermQueryData];
const redirectUrl = redirectData[0].searchTerm?.redirect;
if (redirectUrl !== null) {
window.location.replace(redirectUrl);
}
}

const pageSize = pageSizeData && pageSizeData.storeConfig.grid_per_page;

const sortProps = useSort({ sortFromSearch: true });
Expand Down Expand Up @@ -231,6 +244,16 @@ export const useSearchPage = (props = {}) => {
};
}, [data, setTotalPages]);

useEffect(() => {
if (inputText) {
getSearchTermMethod({
variables: {
search: inputText
}
});
}
}, [inputText, getSearchTermMethod]);

useEffect(() => {
if (inputText) {
getSortMethods({
Expand Down Expand Up @@ -273,7 +296,7 @@ export const useSearchPage = (props = {}) => {
useScrollTopOnChange(currentPage);

const availableSortMethods = sortData
? sortData.products.sort_fields.options
? sortData.products.sort_fields?.options
: null;

return {
Expand Down