diff --git a/.env b/.env index f2d0de5..3839137 100644 --- a/.env +++ b/.env @@ -31,3 +31,4 @@ ENTERPRISE_MARKETING_UTM_SOURCE='' ENTERPRISE_MARKETING_UTM_CAMPAIGN='' ENTERPRISE_MARKETING_FOOTER_UTM_MEDIUM='' LEARNING_BASE_URL='' +PERSONALIZED_RECOMMENDATION_COOKIE_NAME = 'edx-user-personalized-recommendation' diff --git a/.env.development b/.env.development index b6a612d..ce711ef 100644 --- a/.env.development +++ b/.env.development @@ -37,3 +37,5 @@ ENTERPRISE_MARKETING_UTM_SOURCE='example.com' ENTERPRISE_MARKETING_UTM_CAMPAIGN='example.com Referral' ENTERPRISE_MARKETING_FOOTER_UTM_MEDIUM='Footer' LEARNING_BASE_URL='http://localhost:2000' +SESSION_COOKIE_DOMAIN='localhost' +PERSONALIZED_RECOMMENDATION_COOKIE_NAME = 'edx-user-personalized-recommendation' diff --git a/package-lock.json b/package-lock.json index 9159134..789e37d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -55,6 +55,7 @@ "redux-thunk": "2.3.0", "regenerator-runtime": "^0.13.9", "reselect": "^4.0.0", + "universal-cookie": "^4.0.4", "util": "^0.12.4", "whatwg-fetch": "^3.6.2" }, diff --git a/package.json b/package.json index 2f9a9d6..24406d9 100755 --- a/package.json +++ b/package.json @@ -73,6 +73,7 @@ "redux-thunk": "2.3.0", "regenerator-runtime": "^0.13.9", "reselect": "^4.0.0", + "universal-cookie": "^4.0.4", "util": "^0.12.4", "whatwg-fetch": "^3.6.2" }, diff --git a/src/config/index.js b/src/config/index.js index c494455..b993d37 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -10,6 +10,8 @@ const configuration = { // SEGMENT_KEY: process.env.SEGMENT_KEY, // ACCESS_TOKEN_COOKIE_NAME: process.env.ACCESS_TOKEN_COOKIE_NAME, LEARNING_BASE_URL: process.env.LEARNING_BASE_URL, + PERSONALIZED_RECOMMENDATION_COOKIE_NAME: process.env.PERSONALIZED_RECOMMENDATION_COOKIE_NAME || '', + SESSION_COOKIE_DOMAIN: process.env.SESSION_COOKIE_DOMAIN || '', }; const features = {}; diff --git a/src/utils/cookies.js b/src/utils/cookies.js new file mode 100644 index 0000000..6246cb8 --- /dev/null +++ b/src/utils/cookies.js @@ -0,0 +1,16 @@ +import Cookies from 'universal-cookie'; +import { configuration } from '../config'; + +export function setCookie(cookieName, cookieValue, cookieExpiry) { + const cookies = new Cookies(); + const options = { domain: configuration.SESSION_COOKIE_DOMAIN, path: '/' }; + if (cookieExpiry) { + options.expires = new Date(Date.now() + cookieExpiry * 864e5); + } + cookies.set(cookieName, cookieValue, options); +} + +export function getCookie(cookieName) { + const cookies = new Cookies(); + return cookies.get(cookieName); +} diff --git a/src/widgets/RecommendationsPanel/components/CourseCard.jsx b/src/widgets/RecommendationsPanel/components/CourseCard.jsx index 6604b6e..75ac2ee 100644 --- a/src/widgets/RecommendationsPanel/components/CourseCard.jsx +++ b/src/widgets/RecommendationsPanel/components/CourseCard.jsx @@ -4,12 +4,26 @@ import PropTypes from 'prop-types'; import { Card, Hyperlink, Truncate } from '@edx/paragon'; import { useIsCollapsed } from 'containers/CourseCard/hooks'; +import { configuration } from '../../../config'; +import { setCookie, getCookie } from '../../../utils/cookies'; import './index.scss'; export const CourseCard = ({ course }) => { const isCollapsed = useIsCollapsed(); + + const handleCourseClick = () => { + const cookieName = configuration.PERSONALIZED_RECOMMENDATION_COOKIE_NAME; + let recommendedCourses = getCookie(cookieName); + if (typeof recommendedCourses === 'undefined') { + recommendedCourses = { course_keys: [course.courseKey] }; + } else if (!recommendedCourses.course_keys.includes(course.courseKey)) { + recommendedCourses.course_keys.push(course.courseKey); + } + setCookie(cookieName, JSON.stringify(recommendedCourses), 365); + }; + return ( - +
{ } }); return () => { isMounted = false; }; - }); + /* eslint-disable */ + }, []); }; export const useRecommendationPanelData = () => { diff --git a/src/widgets/RecommendationsPanel/hooks.test.js b/src/widgets/RecommendationsPanel/hooks.test.js index 0b58d3b..fafcdeb 100644 --- a/src/widgets/RecommendationsPanel/hooks.test.js +++ b/src/widgets/RecommendationsPanel/hooks.test.js @@ -39,7 +39,7 @@ describe('RecommendationsPanel hooks', () => { expect(calls.length).toEqual(1); }); it('it is only run once (no prereqs)', () => { - expect(prereqs).toEqual(undefined); + expect(prereqs).toEqual([]); }); it('calls fetchRecommendedCourses', () => { api.fetchRecommendedCourses.mockReturnValueOnce(Promise.resolve(response));