UNPKG

@wordpress/editor

Version:
95 lines (92 loc) 2.85 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = PostSwitchToDraftButton; var _components = require("@wordpress/components"); var _i18n = require("@wordpress/i18n"); var _data = require("@wordpress/data"); var _element = require("@wordpress/element"); var _deprecated = _interopRequireDefault(require("@wordpress/deprecated")); var _store = require("../../store"); var _jsxRuntime = require("react/jsx-runtime"); /** * WordPress dependencies */ /** * Internal dependencies */ /** * Renders a button component that allows the user to switch a post to draft status. * * @return {JSX.Element} The rendered component. */function PostSwitchToDraftButton() { (0, _deprecated.default)('wp.editor.PostSwitchToDraftButton', { since: '6.7', version: '6.9' }); const [showConfirmDialog, setShowConfirmDialog] = (0, _element.useState)(false); const { editPost, savePost } = (0, _data.useDispatch)(_store.store); const { isSaving, isPublished, isScheduled } = (0, _data.useSelect)(select => { const { isSavingPost, isCurrentPostPublished, isCurrentPostScheduled } = select(_store.store); return { isSaving: isSavingPost(), isPublished: isCurrentPostPublished(), isScheduled: isCurrentPostScheduled() }; }, []); const isDisabled = isSaving || !isPublished && !isScheduled; let alertMessage; let confirmButtonText; if (isPublished) { alertMessage = (0, _i18n.__)('Are you sure you want to unpublish this post?'); confirmButtonText = (0, _i18n.__)('Unpublish'); } else if (isScheduled) { alertMessage = (0, _i18n.__)('Are you sure you want to unschedule this post?'); confirmButtonText = (0, _i18n.__)('Unschedule'); } const handleConfirm = () => { setShowConfirmDialog(false); editPost({ status: 'draft' }); savePost(); }; return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, { children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, { __next40pxDefaultSize: true, className: "editor-post-switch-to-draft", onClick: () => { if (!isDisabled) { setShowConfirmDialog(true); } }, "aria-disabled": isDisabled, variant: "secondary", style: { flexGrow: '1', justifyContent: 'center' }, children: (0, _i18n.__)('Switch to draft') }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalConfirmDialog, { isOpen: showConfirmDialog, onConfirm: handleConfirm, onCancel: () => setShowConfirmDialog(false), confirmButtonText: confirmButtonText, children: alertMessage })] }); } //# sourceMappingURL=index.js.map