|
| 1 | +import { |
| 2 | + ProductRecommendation, |
| 3 | + loadClickAnalyticsActions, |
| 4 | + buildFrequentlyViewedTogetherList, |
| 5 | +} from "@coveo/headless/product-recommendation"; |
| 6 | +import { FC, useContext, useEffect, useMemo, useState } from "react"; |
| 7 | +import { ProductRecommendationEngineContext } from "../context/PREngine"; |
| 8 | +import { |
| 9 | + ComponentProps, |
| 10 | + registerUniformComponent, |
| 11 | +} from "@uniformdev/canvas-react"; |
| 12 | +import { DEFAULT_NUMBER_OF_RECOMMENDATIONS } from "../constants"; |
| 13 | + |
| 14 | +type RecommendationsProps = ComponentProps<{ |
| 15 | + maxNumberOfRecommendations?: string; |
| 16 | + title?: string; |
| 17 | +}>; |
| 18 | + |
| 19 | +export const Recommendations: FC<RecommendationsProps> = ({ |
| 20 | + maxNumberOfRecommendations, |
| 21 | + title, |
| 22 | +}) => { |
| 23 | + const productRecommendationsEngine = useContext( |
| 24 | + ProductRecommendationEngineContext |
| 25 | + ); |
| 26 | + |
| 27 | + const frequentlyViewedTogetherBuild = useMemo( |
| 28 | + () => |
| 29 | + buildFrequentlyViewedTogetherList(productRecommendationsEngine, { |
| 30 | + options: { |
| 31 | + maxNumberOfRecommendations: |
| 32 | + Number(maxNumberOfRecommendations) || |
| 33 | + DEFAULT_NUMBER_OF_RECOMMENDATIONS, |
| 34 | + }, |
| 35 | + }), |
| 36 | + [productRecommendationsEngine, maxNumberOfRecommendations] |
| 37 | + ); |
| 38 | + |
| 39 | + const [state, setState] = useState(frequentlyViewedTogetherBuild.state); |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + frequentlyViewedTogetherBuild.subscribe(() => |
| 43 | + setState(frequentlyViewedTogetherBuild.state) |
| 44 | + ); |
| 45 | + frequentlyViewedTogetherBuild.refresh(); |
| 46 | + }, [frequentlyViewedTogetherBuild]); |
| 47 | + |
| 48 | + if (state.error) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + const logClick = (recommendation: ProductRecommendation) => { |
| 52 | + if (!productRecommendationsEngine) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + const { logProductRecommendationOpen } = loadClickAnalyticsActions( |
| 57 | + productRecommendationsEngine |
| 58 | + ); |
| 59 | + productRecommendationsEngine.dispatch( |
| 60 | + logProductRecommendationOpen(recommendation) |
| 61 | + ); |
| 62 | + }; |
| 63 | + |
| 64 | + return ( |
| 65 | + <div className="recs-list"> |
| 66 | + <h2>{title || "People also viewed"}</h2> |
| 67 | + <ul> |
| 68 | + {state.recommendations.map((recommendation) => { |
| 69 | + return ( |
| 70 | + <li key={recommendation.permanentid}> |
| 71 | + <h2> |
| 72 | + <a |
| 73 | + onClick={() => logClick(recommendation)} |
| 74 | + onContextMenu={() => logClick(recommendation)} |
| 75 | + onMouseDown={() => logClick(recommendation)} |
| 76 | + onMouseUp={() => logClick(recommendation)} |
| 77 | + > |
| 78 | + {recommendation.ec_name} |
| 79 | + </a> |
| 80 | + </h2> |
| 81 | + </li> |
| 82 | + ); |
| 83 | + })} |
| 84 | + </ul> |
| 85 | + </div> |
| 86 | + ); |
| 87 | +}; |
| 88 | + |
| 89 | +registerUniformComponent({ |
| 90 | + type: "coveo-productRecommendations", |
| 91 | + component: Recommendations, |
| 92 | +}); |
| 93 | + |
| 94 | +export default Recommendations; |
0 commit comments