@automattic/wpcom-checkout
Version:
Functions and components used by WordPress.com checkout.
92 lines • 4.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = useDisplayCartMessages;
const tslib_1 = require("tslib");
const debug_1 = tslib_1.__importDefault(require("debug"));
const react_1 = require("react");
const debug = (0, debug_1.default)('wpcom-checkout:use-display-cart-messages');
function useDisplayCartMessages({ cart, isLoadingCart, showErrorMessages, showSuccessMessages, shouldShowPersistentErrors, }) {
const previousCart = (0, react_1.useRef)(null);
(0, react_1.useEffect)(() => {
if (previousCart.current === cart) {
return;
}
displayCartMessages({
cart,
isLoadingCart,
previousCart: previousCart.current,
showErrorMessages,
showSuccessMessages,
shouldShowPersistentErrors,
});
previousCart.current = cart;
}, [cart, isLoadingCart, showErrorMessages, showSuccessMessages, shouldShowPersistentErrors]);
}
function displayCartMessages({ cart, isLoadingCart, previousCart, showErrorMessages, showSuccessMessages, shouldShowPersistentErrors, }) {
const newCart = cart;
if (isLoadingCart) {
return;
}
const messages = getNewMessages(previousCart, newCart);
if (messages.errors?.length) {
debug('showing errors', messages.errors);
showErrorMessages(messages.errors);
}
if (messages.success?.length) {
debug('showing success', messages.errors);
showSuccessMessages(messages.success);
}
if (messages.persistent_errors?.length && shouldShowPersistentErrors) {
debug('showing persistent errors', messages.persistent_errors);
showErrorMessages(messages.persistent_errors);
}
}
// Compare two different cart objects and get the messages of newest one
function getNewMessages(previousCartValue, nextCartValue) {
const nextCartMessages = nextCartValue.messages || {};
const previousCartTimestamp = previousCartValue?.cart_generated_at_timestamp;
const nextCartTimestamp = nextCartValue.cart_generated_at_timestamp;
debug('comparing previous cart', previousCartTimestamp, 'to new cart', nextCartTimestamp);
// If there is no previous cart then just return the messages for the new cart
if (!previousCartTimestamp || !nextCartTimestamp) {
debug('no previous cart; just returning new messages');
return nextCartMessages;
}
const doesNextCartHaveDifferentMessages = (() => {
const nextCartErrorCodes = nextCartMessages?.errors
?.map((message) => message.code)
.join(';');
const previousCartErrorCodes = previousCartValue.messages?.errors
?.map((message) => message.code)
.join(';');
if (nextCartErrorCodes !== previousCartErrorCodes) {
return true;
}
const nextCartSuccessCodes = nextCartMessages?.success
?.map((message) => message.code)
.join(';');
const previousCartSuccessCodes = previousCartValue.messages?.success
?.map((message) => message.code)
.join(';');
if (nextCartSuccessCodes !== previousCartSuccessCodes) {
return true;
}
return false;
})();
const nextCartHasNewData = (() => {
const isNextCartNewer = new Date(nextCartTimestamp) > new Date(previousCartTimestamp);
if (isNextCartNewer) {
return true;
}
// We need to actually check the messages themselves when the timestamp is
// the same because if the cart endpoint is fast enough you could get two
// different carts with the same timestamp.
if (nextCartTimestamp === previousCartTimestamp) {
return doesNextCartHaveDifferentMessages;
}
return false;
})();
debug('does cart have new data?', nextCartHasNewData);
return nextCartHasNewData ? nextCartMessages : {};
}
//# sourceMappingURL=use-display-cart-messages.js.map