-
Notifications
You must be signed in to change notification settings - Fork 0
/
NostoSearch.tsx
54 lines (50 loc) · 1.72 KB
/
NostoSearch.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { useNostoContext } from "./context"
import { useNostoApi } from "../utils/hooks"
/**
* You can personalise your search pages by using the NostoSearch component.
* The component requires that you provide it the current search term.
*
* By default, your account, when created, has two search-page placements named `searchpage-nosto-1` and `searchpage-nosto-2`.
* You may omit these and use any identifier you need. The identifiers used here are simply provided to illustrate the example.
*
* @example
* ```
* <div className="search-page">
* <NostoPlacement id="searchpage-nosto-1" />
* <NostoPlacement id="searchpage-nosto-2" />
* <NostoSearch query={"black shoes"} />
* </div>
* ```
*
* **Note:** Do not encode the search term in any way.
* It should be provided an unencoded string.
* A query for `black shoes` must be provided as-is and not as `black+shoes`.
* Doing so will lead to invalid results.
*
* @group Components
*/
export default function NostoSearch(props: { query: string; placements?: string[] }) {
const { query, placements } = props
const { recommendationComponent, useRenderCampaigns } = useNostoContext()
const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("search")
useNostoApi(
async (api) => {
const data = await api.defaultSession()
.viewSearch(query)
.setPlacements(placements || api.placements.getPlacements())
.load()
renderCampaigns(data, api)
},
[query, recommendationComponent, pageTypeUpdated]
)
return (
<>
<div className="nosto_page_type" style={{ display: "none" }}>
search
</div>
<div className="nosto_search" style={{ display: "none" }}>
{query}
</div>
</>
)
}