@atlaskit/global-search
Version:
A cross-product search component (batteries included)
56 lines (47 loc) • 1.21 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import { DEFAULT_AB_TEST } from '../api/CrossProductSearchClient';
import memoizeOne from 'memoize-one';
export class ABTestProvider extends React.Component {
constructor(...args) {
super(...args);
_defineProperty(this, "state", {
abTest: null
});
_defineProperty(this, "fetchAbTestOnce", memoizeOne(() => {
const {
context,
crossProductSearchClient
} = this.props;
if (!this.state.abTest) {
crossProductSearchClient.getAbTestDataForProduct(context).then(abTest => {
this.setState({
abTest: abTest || DEFAULT_AB_TEST
});
}).catch(e => {
this.setState({
abTest: DEFAULT_AB_TEST
});
});
}
}));
}
componentDidMount() {
this.fetchAbTestOnce();
}
componentDidUpdate() {
this.fetchAbTestOnce();
}
render() {
const {
abTest
} = this.state;
const {
children
} = this.props;
if (!abTest) {
return null;
}
return /*#__PURE__*/React.createElement(React.Fragment, null, children(abTest));
}
}