UNPKG

@shopgate/engage

Version:
90 lines (85 loc) 2.57 kB
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose"; import React from 'react'; import PropTypes from 'prop-types'; import { CART_ITEM_TYPE_PRODUCT } from '@shopgate/engage/cart'; import { useCartItemProduct } from "./CartItem.hooks"; /** * Provides legacy context for CartItemProduct component and its children. * Within PWA7 the context was refactored to the new context API. To keep compatibility with * older extensions, this provider is used to provide the legacy context. * * Should be removed when PWA 7 is deployed to all of the shops and affected extensions can be * updated without the need to support older PWA versions. */ import { jsx as _jsx } from "react/jsx-runtime"; let LegacyProvider = /*#__PURE__*/function (_React$Component) { /** * @param {Object} props The component props. */ function LegacyProvider(props) { var _this; _this = _React$Component.call(this, props) || this; _this.state = { cartItemId: props.cartItemId, product: props.product }; return _this; } /** * @param {Object} nextProps Next props * @param {*} prevState Prev state * @returns {Object|null} */ _inheritsLoose(LegacyProvider, _React$Component); LegacyProvider.getDerivedStateFromProps = function getDerivedStateFromProps(nextProps, prevState) { if (nextProps.cartItemId !== prevState.cartItemId || nextProps.product !== prevState.product) { return { cartItemId: nextProps.cartItemId, product: nextProps.product }; } return null; } /** * @returns {Object} */; var _proto = LegacyProvider.prototype; _proto.getChildContext = function getChildContext() { return { type: CART_ITEM_TYPE_PRODUCT, cartItemId: this.state.cartItemId, product: this.state.product }; } /** * @returns {JSX.Element} */; _proto.render = function render() { return this.props.children; }; return LegacyProvider; }(React.Component); LegacyProvider.childContextTypes = { type: PropTypes.string, cartItemId: PropTypes.string, product: PropTypes.shape() }; /** * Bridges the CartItemProductContext value to the legacy CartItemProduct context. * @param {Object} props The component props. * @returns {JSX.Element} */ const CartItemProductProviderLegacy = ({ children }) => { const { cartItemId, product } = useCartItemProduct(); return /*#__PURE__*/_jsx(LegacyProvider, { cartItemId: cartItemId, product: product, children: children }); }; export default CartItemProductProviderLegacy;