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

BUG#PWA-3161 [Cloud] Search Term Redirect not working in PWA #4168

Merged
merged 7 commits into from
Oct 4, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

exports[`searchCategory returns the correct shape 1`] = `
Object {
"availableSortMethods": Array [
Object {
"label": "Position",
"value": "position",
},
],
"availableSortMethods": undefined,
"currentStoreName": "Store 1",
"data": Object {
"products": Object {
Expand Down
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 @@ -36,6 +37,18 @@ export const useSearchPage = (props = {}) => {
nextFetchPolicy: 'cache-first'
});

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 [getSortMethods, { data: sortData }] = useLazyQuery(
getSearchAvailableSortMethods,
{
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