-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Implemented product recommendations experiment #174
Changes from 4 commits
bc92a38
9f640e9
4790bf4
01a93f7
14b1a24
c0b7db9
ab3387f
85e98ab
e43dba6
d40245c
410dccd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { useWindowSize, breakpoints } from '@edx/paragon'; | ||
import { StrictDict } from 'utils'; | ||
import api from 'widgets/ProductRecommendations/api'; | ||
import * as module from './experimentContext'; | ||
|
||
export const state = StrictDict({ | ||
experiment: (val) => React.useState(val), // eslint-disable-line | ||
countryCode: (val) => React.useState(val), // eslint-disable-line | ||
}); | ||
|
||
export const useCountryCode = (setCountryCode) => { | ||
React.useEffect(() => { | ||
let isMounted = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this important? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True, I can't really think of a scenario where the component will be unmounted, I'll adjust it |
||
|
||
api | ||
.fetchRecommendationsContext() | ||
.then((response) => { | ||
if (isMounted) { | ||
setCountryCode(response.data.countryCode); | ||
} | ||
}) | ||
.catch(() => { | ||
if (isMounted) { | ||
setCountryCode(''); | ||
} | ||
}); | ||
|
||
return () => { | ||
isMounted = false; | ||
}; | ||
/* eslint-disable */ | ||
}, []); | ||
}; | ||
|
||
export const ExperimentContext = React.createContext(); | ||
|
||
export const ExperimentProvider = ({ children }) => { | ||
const [countryCode, setCountryCode] = module.state.countryCode(null); | ||
const [experiment, setExperiment] = module.state.experiment({ | ||
isExperimentActive: false, | ||
inRecommendationsVariant: true, | ||
}); | ||
|
||
module.useCountryCode(setCountryCode); | ||
const { width } = useWindowSize(); | ||
const isMobile = width < breakpoints.small.minWidth; | ||
|
||
const contextValue = React.useMemo( | ||
() => ({ | ||
experiment, | ||
countryCode, | ||
setExperiment, | ||
setCountryCode, | ||
isMobile, | ||
}), | ||
[experiment, countryCode, setExperiment, setCountryCode, isMobile] | ||
); | ||
|
||
return ( | ||
<ExperimentContext.Provider value={contextValue}> | ||
{children} | ||
</ExperimentContext.Provider> | ||
); | ||
}; | ||
|
||
export const useExperimentContext = () => React.useContext(ExperimentContext); | ||
|
||
ExperimentProvider.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
}; | ||
|
||
export default { useCountryCode, useExperimentContext }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My one request here is that this file get renamed to ExperimentContext, as is exporting a component provider.