Skip to content

Commit

Permalink
Update KMC connection, fix click and update react demo
Browse files Browse the repository at this point in the history
  • Loading branch information
rallu committed Apr 20, 2022
1 parent 4ec6c63 commit e6d66b4
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 145 deletions.
4 changes: 3 additions & 1 deletion examples/react/src/components/product.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ export function Product(props: {
<Link
to={`/products/${p.id}`}
onClick={(event) => {
props.onClick()
if (props.onClick) {
props.onClick()
}
}}
css={containerCss}
>
Expand Down
115 changes: 76 additions & 39 deletions examples/react/src/components/quicksearch.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import {
KlevuDomEvents,
KlevuFetch,
KlevuKMCSettings,
KlevuLastSearches,
KlevuResultEvent,
KlevuTypeOfRecord,
personalisation,
search,
suggestions,
trendingProducts,
} from "@klevu/core"
import type { KlevuRecord, KlevuLastSearch } from "@klevu/core"
import type { KlevuRecord, KlevuLastSearch, KMCRootObject } from "@klevu/core"
import {
Grid,
IconButton,
InputAdornment,
Paper,
Popper,
Expand All @@ -20,14 +21,13 @@ import {
} from "@mui/material"
import debounce from "lodash.debounce"
import {
bindMenu,
bindTrigger,
bindPopper,
bindFocus,
usePopupState,
} from "material-ui-popup-state/hooks"
import { useNavigate } from "react-router-dom"
import { useLocation, useNavigate } from "react-router-dom"
import React, { useEffect, useMemo, useState } from "react"
import { Product } from "./product"
import { Close } from "@mui/icons-material"
import SearchIcon from "@mui/icons-material/Search"

let clickManager: ReturnType<KlevuResultEvent["getSearchClickSendEvent"]>
Expand All @@ -44,8 +44,17 @@ export function QuickSearch() {
const popupState = usePopupState({
variant: "popper",
popupId: "searchPopup",
disableAutoFocus: true,
})

const [kmcSettings, setKmcSettings] = useState<KMCRootObject>()

let location = useLocation()
React.useEffect(() => {
popupState.close()
setProducts([])
}, [location])

const doSearch = async (term: string) => {
if (term.length < 3) {
setProducts([])
Expand All @@ -54,10 +63,14 @@ export function QuickSearch() {
}

const result = await KlevuFetch(
search(term, {
limit: 9,
typeOfRecords: [KlevuTypeOfRecord.Product],
}),
search(
term,
{
limit: 6,
typeOfRecords: [KlevuTypeOfRecord.Product],
},
personalisation()
),
suggestions(term)
)

Expand All @@ -73,8 +86,7 @@ export function QuickSearch() {
}

const fetchEmptySuggestions = async () => {
popupState.open()
setLastSearches(KlevuLastSearches.get())
handleLastSearchesUpdate()

if (trendProducts.length > 0) {
return
Expand All @@ -91,6 +103,7 @@ export function QuickSearch() {
const onKeydown: React.KeyboardEventHandler<HTMLDivElement> = (event) => {
if (event.key === "Enter") {
navigate("/search?q=" + encodeURIComponent(searchValue))
popupState.close()
}
}

Expand All @@ -101,20 +114,20 @@ export function QuickSearch() {

const debouncedChangeHandler: any = useMemo(() => debounce(doSearch, 300), [])

const clickOnSearch = (suggestion: string) => {
const raw = suggestion.replace(/<[^>]*>?/gm, "")
setSearchValue(raw)
doSearch(raw)
const fetchKMCSettings = async () => {
const settings = await KlevuKMCSettings()
setKmcSettings(settings.root)
}

useEffect(() => {
fetchKMCSettings()
return () => {
debouncedChangeHandler.cancel()
}
}, [])

const handleLastSearchesUpdate = (event) => {
setLastSearches(KlevuLastSearches.get())
const handleLastSearchesUpdate = () => {
setLastSearches(Array.from(KlevuLastSearches.get()).reverse())
}

React.useEffect(() => {
Expand All @@ -132,6 +145,15 @@ export function QuickSearch() {
}
}, [])

const focusParams = bindFocus(popupState)
const params = {
...focusParams,
onFocus: (event) => {
focusParams.onFocus(event)
fetchEmptySuggestions()
},
}

return (
<React.Fragment>
<TextField
Expand All @@ -158,10 +180,10 @@ export function QuickSearch() {
</InputAdornment>
),
}}
{...bindTrigger(popupState)}
{...params}
/>
<Popper
{...bindMenu(popupState)}
{...bindPopper(popupState)}
popperOptions={{
placement: "bottom-end",
}}
Expand All @@ -173,22 +195,21 @@ export function QuickSearch() {
elevation={8}
style={{
padding: "12px",
width: "780px",
width: "792px",
display: "flex",
position: "relative",
marginTop: "16px",
}}
>
<IconButton
style={{ position: "absolute", top: "6px", right: "6px" }}
onClick={() => {
popupState.close()
setSearchValue("")
<div
style={{
width: "160px",
flexShrink: 0,
borderRight: "1px solid #d1d1d1",
paddingRight: "8px",
marginRight: "8px",
}}
>
<Close />
</IconButton>
<div style={{ width: "160px", flexShrink: 0 }}>
{sugs.length > 0 ? (
<React.Fragment>
<Typography variant="h6">Suggestions</Typography>
Expand All @@ -198,26 +219,43 @@ export function QuickSearch() {
{sugs.map((s, i) => (
<li
key={i}
onClick={() => clickOnSearch(s)}
style={{ padding: 0, cursor: "pointer" }}
style={{ padding: 0 }}
dangerouslySetInnerHTML={{ __html: s }}
></li>
))}
</ul>
</React.Fragment>
) : null}
) : (
<React.Fragment>
{kmcSettings &&
kmcSettings.klevu_webstorePopularTerms.length > 0 && (
<React.Fragment>
<Typography variant="h6">Popular searches</Typography>
<ul
style={{
margin: 0,
listStyleType: "none",
padding: "12px",
}}
>
{kmcSettings.klevu_webstorePopularTerms.map((s, i) => (
<li key={i} style={{ padding: 0 }}>
{s}
</li>
))}
</ul>
</React.Fragment>
)}
</React.Fragment>
)}
{lastSearches.length > 0 ? (
<React.Fragment>
<Typography variant="h6">Last searches</Typography>
<ul
style={{ margin: 0, listStyleType: "none", padding: "12px" }}
>
{lastSearches.map((s, i) => (
<li
key={i}
style={{ padding: 0, cursor: "pointer" }}
onClick={() => clickOnSearch(s.term)}
>
<li key={i} style={{ padding: 0 }}>
{s.term}
</li>
))}
Expand All @@ -235,8 +273,7 @@ export function QuickSearch() {
<Product
product={p}
onClick={() => {
clickManager(p.id)
popupState.close()
clickManager(p.id, p.itemGroupId)
}}
/>
</Grid>
Expand Down
4 changes: 3 additions & 1 deletion examples/react/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react"
import ReactDOM from "react-dom"
import { KlevuConfig } from "@klevu/core"
import { KlevuConfig, KlevuKMCSettings } from "@klevu/core"
import { App } from "./app"
import { BrowserRouter, Routes, Route } from "react-router-dom"
import { HomePage } from "./routes/HomePage"
Expand All @@ -15,6 +15,8 @@ KlevuConfig.init({
apiKey: "klevu-164651914788114877",
})

KlevuKMCSettings()

ReactDOM.render(
<BrowserRouter>
<Routes>
Expand Down
4 changes: 3 additions & 1 deletion examples/react/src/routes/CheckoutPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
KlevuFetch,
KlevuRecord,
kmcRecommendation,
personalisation,
trendingProducts,
} from "@klevu/core"
import { Button, Container, Grid, Typography } from "@mui/material"
Expand Down Expand Up @@ -32,7 +33,8 @@ export function CheckoutPage() {
id: "alsobought",
cartProductIds: cart.items.map((p) => p.id),
},
fallback(trendingProducts())
fallback(trendingProducts()),
personalisation()
)
)

Expand Down
4 changes: 3 additions & 1 deletion examples/react/src/routes/HomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
KlevuFetch,
KlevuRecord,
kmcRecommendation,
personalisation,
sendRecommendationViewEvent,
} from "@klevu/core"
import { Container, Typography } from "@mui/material"
Expand All @@ -22,7 +23,8 @@ export function HomePage() {
},
sendRecommendationViewEvent(
"Trending recommendations using KMC builder"
)
),
personalisation()
)
)

Expand Down
17 changes: 15 additions & 2 deletions examples/react/src/routes/SearchResultPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
KlevuDomEvents,
FilterManager,
search,
KlevuResultEvent,
sendSearchEvent,
personalisation
} from "@klevu/core"
import type {
KlevuRecord,
Expand Down Expand Up @@ -40,6 +43,7 @@ import FilterIcon from "@mui/icons-material/FilterAlt"
const drawerWidth = 240
const manager = new FilterManager()
let prevRes: KlevuFetchResponse
let clickManager: ReturnType<KlevuResultEvent["getSearchClickSendEvent"]>

function useQuery() {
const { search } = useLocation()
Expand Down Expand Up @@ -87,7 +91,9 @@ export function SearchResultPage() {
],
filterManager: manager,
}),
applyFilterWithManager(manager)
applyFilterWithManager(manager),
sendSearchEvent(),
personalisation()
),
]
const res = await KlevuFetch(...functions)
Expand All @@ -98,6 +104,8 @@ export function SearchResultPage() {
return
}

clickManager = searchResult.getSearchClickSendEvent()

setShowMore(Boolean(res.next))
setOptions(manager.options)
setSliders(manager.sliders)
Expand Down Expand Up @@ -257,7 +265,12 @@ export function SearchResultPage() {
>
{products.map((p, i) => (
<Grid item key={i}>
<Product product={p} />
<Product
product={p}
onClick={() => {
clickManager(p.id, p.itemGroupId)
}}
/>
</Grid>
))}
</Grid>
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e6d66b4

Please sign in to comment.