UNPKG

@woocommerce/data

Version:
42 lines (41 loc) 1.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.withCurrentUserHydration = void 0; /** * External dependencies */ const compose_1 = require("@wordpress/compose"); const data_1 = require("@wordpress/data"); const element_1 = require("@wordpress/element"); /** * Internal dependencies */ const constants_1 = require("./constants"); /** * Higher-order component used to hydrate current user data. * * @param {Object} currentUser Current user object in the same format as the WP REST API returns. */ const withCurrentUserHydration = (currentUser) => (0, compose_1.createHigherOrderComponent)((OriginalComponent) => (props) => { // Use currentUser to hydrate calls to @wordpress/core-data's getCurrentUser(). const shouldHydrate = (0, data_1.useSelect)((select) => { if (!currentUser) { return; } // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore const { isResolving, hasFinishedResolution } = select(constants_1.STORE_NAME); return (!isResolving('getCurrentUser') && !hasFinishedResolution('getCurrentUser')); }); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore const { startResolution, finishResolution, receiveCurrentUser } = (0, data_1.useDispatch)(constants_1.STORE_NAME); if (shouldHydrate) { startResolution('getCurrentUser', []); receiveCurrentUser(currentUser); finishResolution('getCurrentUser', []); } return (0, element_1.createElement)(OriginalComponent, { ...props }); }, 'withCurrentUserHydration'); exports.withCurrentUserHydration = withCurrentUserHydration;