UNPKG

@wordpress/block-library

Version:
82 lines (79 loc) 2.43 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = PostCommentsCountEdit; var _clsx = _interopRequireDefault(require("clsx")); var _blockEditor = require("@wordpress/block-editor"); var _element = require("@wordpress/element"); var _apiFetch = _interopRequireDefault(require("@wordpress/api-fetch")); var _url = require("@wordpress/url"); var _i18n = require("@wordpress/i18n"); var _jsxRuntime = require("react/jsx-runtime"); /** * External dependencies */ /** * WordPress dependencies */ function PostCommentsCountEdit({ attributes, context, setAttributes }) { const { textAlign } = attributes; const { postId } = context; const [commentsCount, setCommentsCount] = (0, _element.useState)(); const blockProps = (0, _blockEditor.useBlockProps)({ className: (0, _clsx.default)({ [`has-text-align-${textAlign}`]: textAlign }) }); (0, _element.useEffect)(() => { if (!postId) { return; } const currentPostId = postId; (0, _apiFetch.default)({ path: (0, _url.addQueryArgs)('/wp/v2/comments', { post: postId }), parse: false }).then(res => { // Stale requests will have the `currentPostId` of an older closure. if (currentPostId === postId) { setCommentsCount(res.headers.get('X-WP-Total')); } }); }, [postId]); const hasPostAndComments = postId && commentsCount !== undefined; const blockStyles = { ...blockProps.style, textDecoration: hasPostAndComments ? blockProps.style?.textDecoration : undefined }; return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, { children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.BlockControls, { group: "block", children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.AlignmentControl, { value: textAlign, onChange: nextAlign => { setAttributes({ textAlign: nextAlign }); } }) }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { ...blockProps, style: blockStyles, children: hasPostAndComments ? commentsCount : /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.Warning, { children: (0, _i18n.__)('Post Comments Count block: post not found.') }) })] }); } //# sourceMappingURL=edit.js.map