-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(searchbox): Support search types: product, category, content, be…
…stseller
- Loading branch information
Showing
6 changed files
with
123 additions
and
59 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { | ||
BestsellerOptions, | ||
CategorySearchOptions, | ||
Configuration, | ||
ContentSearchOptions, | ||
ProductSearchOptions, | ||
SearchResponse, | ||
bestseller, | ||
categorySearch, | ||
contentSearch, | ||
productSearch, | ||
} from '@bloomreach/discovery-web-sdk'; | ||
import { useEffect, useState } from 'react'; | ||
import { SearchType } from '../search-box/search-box.types'; | ||
|
||
type SearchOptions = | ||
| ProductSearchOptions | ||
| CategorySearchOptions | ||
| ContentSearchOptions | ||
| BestsellerOptions; | ||
|
||
type UseSearch = { | ||
response: SearchResponse | null; | ||
loading: boolean; | ||
error: unknown; | ||
}; | ||
|
||
/** | ||
* Custom hook to perform a product search using Bloomreach Discovery Web SDK. | ||
* | ||
* @param query - The search query string. | ||
* @param configuration - The configuration object for the Bloomreach SDK. | ||
* @param searchOptions - Additional search options excluding the query. | ||
* @returns An object containing the search response, loading state, and any error encountered. | ||
*/ | ||
export function useSearch( | ||
searchType: SearchType, | ||
configuration: Configuration, | ||
searchOptions: SearchOptions, | ||
): UseSearch { | ||
const [response, setResponse] = useState<SearchResponse | null>(null); | ||
const [loading, setLoading] = useState<boolean>(true); | ||
const [error, setError] = useState<unknown>(null); | ||
|
||
useEffect(() => { | ||
const fetchData = async () => { | ||
setLoading(true); | ||
|
||
if (searchType === 'product') { | ||
const apiResponse = await productSearch(configuration, searchOptions as ProductSearchOptions); | ||
setResponse(apiResponse); | ||
} | ||
|
||
if (searchType === 'category') { | ||
const apiResponse = await categorySearch(configuration, searchOptions as CategorySearchOptions); | ||
setResponse(apiResponse); | ||
} | ||
|
||
if (searchType === 'content') { | ||
const apiResponse = await contentSearch(configuration, searchOptions as ContentSearchOptions); | ||
setResponse(apiResponse); | ||
} | ||
|
||
if (searchType === 'bestseller') { | ||
const apiResponse = await bestseller(configuration, searchOptions as BestsellerOptions); | ||
setResponse(apiResponse); | ||
} | ||
}; | ||
|
||
fetchData() | ||
.catch((e: unknown) => { | ||
setError(e); | ||
}) | ||
.finally(() => { | ||
setLoading(false); | ||
}); | ||
}, [searchType, configuration, searchOptions]); | ||
|
||
return { response, loading, error }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters