Skip to content

Commit c450b29

Browse files
Merge pull request #4092 from beckn/bugfix/krushna/3684/multiple_search_keyword_implementation_search_api
fix(retail): fixed search api should take multiple search keyword using promise.all
2 parents dfcfbe4 + 09001ce commit c450b29

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

apps/retail/components/selectDeliveryModal/SelectDeliveryModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ const SelectDeliveryModal: React.FC<SelectDeliveryModalProps> = props => {
8888
<BecknButton
8989
children={t.searchItems}
9090
handleClick={() => {
91-
const selectedItems = props.selectedValues.join(',').replace(',', ' ')
91+
const selectedItems = props.selectedValues.join(',')
9292
router.push(`/search?searchTerm=${selectedItems}&category=${convertTourismCategoryToRetail(category)}`)
9393
}}
9494
disabled={false}

apps/retail/pages/search.tsx

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,55 @@ const Search = () => {
2929
if (!searchKeyword) return
3030
setIsLoading(true)
3131

32-
const searchPayload = {
33-
context: {
34-
domain: DOMAIN
35-
},
36-
searchString: searchKeyword,
37-
category: {
38-
categoryCode: router.query.category || 'Retail'
39-
},
40-
fulfillment: {
41-
type: 'Delivery',
42-
stops: [
43-
{
44-
location: '28.4594965,77.0266383'
45-
}
46-
]
32+
// Split search keyword by comma and trim each keyword
33+
const searchKeywords = searchKeyword.split(',').map(keyword => keyword.trim())
34+
35+
// Create search payloads with different combinations
36+
const searchPromises = searchKeywords.map((keyword, index) => {
37+
const searchString = keyword
38+
39+
const searchPayload = {
40+
context: {
41+
domain: DOMAIN
42+
},
43+
searchString: searchString,
44+
category: {
45+
categoryCode: router.query.category || 'Retail'
46+
},
47+
fulfillment: {
48+
type: 'Delivery',
49+
stops: [
50+
{
51+
location: '28.4594965,77.0266383'
52+
}
53+
]
54+
}
4755
}
48-
}
4956

50-
axios
51-
.post(`${apiUrl}/search`, searchPayload)
52-
.then(res => {
53-
dispatch(discoveryActions.addTransactionId({ transactionId: res.data.data[0].context.transaction_id }))
54-
const parsedSearchItems = parseSearchlist(res.data.data)
57+
return axios.post(`${apiUrl}/search`, searchPayload)
58+
})
59+
60+
Promise.all(searchPromises)
61+
.then(responses => {
62+
// Process each response and extract items
63+
const allResults = responses
64+
.filter(res => res?.data?.data?.[0]?.message?.providers)
65+
.map(res => res.data.data[0]) // Keep the original response structure
66+
67+
console.log('allResults', allResults)
68+
69+
// Get transaction ID from first valid response
70+
const firstValidResponse = responses.find(res => res?.data?.data?.[0]?.context?.transaction_id)
71+
if (firstValidResponse) {
72+
dispatch(
73+
discoveryActions.addTransactionId({
74+
transactionId: firstValidResponse.data.data[0].context.transaction_id
75+
})
76+
)
77+
}
78+
79+
// Parse and combine all search items
80+
const parsedSearchItems = parseSearchlist(allResults)
5581
dispatch(discoveryActions.addProducts({ products: parsedSearchItems }))
5682
setItems(parsedSearchItems)
5783
setOriginalItems(parsedSearchItems)

0 commit comments

Comments
 (0)