@shopgate/engage
Version:
Shopgate's ENGAGE library.
86 lines (85 loc) • 2.77 kB
JavaScript
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { css } from 'glamor';
import { isBeta, useWidgetSettings, useWidgetStyles } from '@shopgate/engage/core';
import { I18n, TimeBoundary, SurroundPortals } from '@shopgate/engage/components';
import { PRODUCT_EFFECTIVITY_DATES } from '@shopgate/pwa-common-commerce/product/constants/Portals';
import { showExpiringLabel, showScheduledLabel } from "./helpers";
import { hint, notAvailable } from "./style";
import connect from "./connector";
/**
* The Product Effectivity Dates component.
* @return {JSX}
*/
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
const EffectivityDates = ({
dates,
children,
productNotAvailable,
productId
}) => {
const settings = useWidgetSettings('@shopgate/engage/product/EffectivityDates');
const styles = useWidgetStyles('@shopgate/engage/product/EffectivityDates');
if (!isBeta() || !dates) {
return children;
}
const startDate = dates.startDate ? new Date(dates.startDate) : null;
const endDate = dates.endDate ? new Date(dates.endDate) : null;
const hintAddClass = styles && styles.hint ? css(styles.hint).toString() : null;
return /*#__PURE__*/_jsx(SurroundPortals, {
portalName: PRODUCT_EFFECTIVITY_DATES,
portalProps: {
dates,
productNotAvailable,
productId
},
children: /*#__PURE__*/_jsx(TimeBoundary, {
start: startDate,
end: endDate,
children: ({
before,
between,
after
}) => {
if (before) {
return showScheduledLabel(startDate, settings) ? /*#__PURE__*/_jsx(I18n.Text, {
string: "product.available.at",
params: {
startDate
},
className: classNames(hint, hintAddClass)
}) : children;
}
if (between) {
return /*#__PURE__*/_jsxs(_Fragment, {
children: [children, showExpiringLabel(endDate, settings) && /*#__PURE__*/_jsx(I18n.Text, {
string: "product.available.until",
params: {
endDate
},
className: classNames(hint, hintAddClass)
})]
});
}
if (after) {
productNotAvailable();
return showExpiringLabel(endDate, settings) ? /*#__PURE__*/_jsx(I18n.Text, {
string: "product.available.not",
className: classNames(hint, hintAddClass, notAvailable)
}) : children;
}
return children;
}
})
});
};
EffectivityDates.defaultProps = {
children: null,
dates: {
startDate: null,
endDate: null
},
productId: null
};
export default connect(EffectivityDates);