From 718bda00fa210d8c670351141f6ddfedaa805d23 Mon Sep 17 00:00:00 2001 From: Raghavendra Date: Wed, 20 Sep 2023 17:49:21 +0530 Subject: [PATCH] FIX#PWA-3161 [Cloud] Search Term Redirect not working in PWA --- .../lib/talons/SearchPage/searchPage.gql.js | 11 ++++++++ .../lib/talons/SearchPage/useSearchPage.js | 25 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/peregrine/lib/talons/SearchPage/searchPage.gql.js b/packages/peregrine/lib/talons/SearchPage/searchPage.gql.js index 1c11774d16..3d67fed9a4 100644 --- a/packages/peregrine/lib/talons/SearchPage/searchPage.gql.js +++ b/packages/peregrine/lib/talons/SearchPage/searchPage.gql.js @@ -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 @@ -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 diff --git a/packages/peregrine/lib/talons/SearchPage/useSearchPage.js b/packages/peregrine/lib/talons/SearchPage/useSearchPage.js index 279fe12377..b4c0957733 100644 --- a/packages/peregrine/lib/talons/SearchPage/useSearchPage.js +++ b/packages/peregrine/lib/talons/SearchPage/useSearchPage.js @@ -26,6 +26,7 @@ export const useSearchPage = (props = {}) => { const { getFilterInputsQuery, getPageSize, + getSearchTermData, getProductFiltersBySearchQuery, getSearchAvailableSortMethods, productSearchQuery @@ -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 }); @@ -231,6 +244,16 @@ export const useSearchPage = (props = {}) => { }; }, [data, setTotalPages]); + useEffect(() => { + if (inputText) { + getSearchTermMethod({ + variables: { + search: inputText + } + }); + } + }, [inputText, getSearchTermMethod]); + useEffect(() => { if (inputText) { getSortMethods({ @@ -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 {