UNPKG

@wordpress/block-library

Version:
153 lines (152 loc) 4.63 kB
// packages/block-library/src/navigation-link/link-ui/page-creator.js import { Button, TextControl, Notice, CheckboxControl, __experimentalVStack as VStack, __experimentalHStack as HStack } from "@wordpress/components"; import { __, sprintf } from "@wordpress/i18n"; import { useSelect, useDispatch } from "@wordpress/data"; import { store as coreStore } from "@wordpress/core-data"; import { store as noticesStore } from "@wordpress/notices"; import { decodeEntities } from "@wordpress/html-entities"; import { useState } from "@wordpress/element"; import DialogWrapper from "./dialog-wrapper"; import { jsx, jsxs } from "react/jsx-runtime"; function LinkUIPageCreator({ postType, onBack, onPageCreated, initialTitle = "" }) { const [title, setTitle] = useState(initialTitle); const [shouldPublish, setShouldPublish] = useState(false); const isTitleValid = title.trim().length > 0; const { lastError, isSaving } = useSelect( (select) => ({ lastError: select(coreStore).getLastEntitySaveError( "postType", postType ), isSaving: select(coreStore).isSavingEntityRecord( "postType", postType ) }), [postType] ); const { saveEntityRecord } = useDispatch(coreStore); const { createSuccessNotice, createErrorNotice } = useDispatch(noticesStore); async function createPage(event) { event.preventDefault(); if (isSaving || !isTitleValid) { return; } try { const savedRecord = await saveEntityRecord( "postType", postType, { title, status: shouldPublish ? "publish" : "draft" }, { throwOnError: true } ); if (savedRecord) { const pageLink = { id: savedRecord.id, type: postType, title: decodeEntities(savedRecord.title.rendered), url: savedRecord.link, kind: "post-type" }; createSuccessNotice( sprintf( // translators: %s: the name of the new page being created. __("%s page created successfully."), decodeEntities(savedRecord.title.rendered) ), { type: "snackbar", id: "page-created-success" } ); onPageCreated(pageLink); } } catch (error) { createErrorNotice( __("Failed to create page. Please try again."), { type: "snackbar", id: "page-created-error" } ); } } const isSubmitDisabled = isSaving || !isTitleValid; return /* @__PURE__ */ jsx( DialogWrapper, { className: "link-ui-page-creator", title: __("Create page"), description: __("Create a new page to add to your Navigation."), onBack, children: /* @__PURE__ */ jsx(VStack, { className: "link-ui-page-creator__inner", spacing: 4, children: /* @__PURE__ */ jsx("form", { onSubmit: createPage, children: /* @__PURE__ */ jsxs(VStack, { spacing: 4, children: [ /* @__PURE__ */ jsx( TextControl, { __next40pxDefaultSize: true, __nextHasNoMarginBottom: true, label: __("Title"), onChange: setTitle, placeholder: __("No title"), value: title } ), /* @__PURE__ */ jsx( CheckboxControl, { __nextHasNoMarginBottom: true, label: __("Publish immediately"), help: __( "If unchecked, the page will be created as a draft." ), checked: shouldPublish, onChange: setShouldPublish } ), lastError && /* @__PURE__ */ jsx(Notice, { status: "error", isDismissible: false, children: lastError.message }), /* @__PURE__ */ jsxs(HStack, { spacing: 2, justify: "flex-end", children: [ /* @__PURE__ */ jsx( Button, { __next40pxDefaultSize: true, variant: "tertiary", onClick: onBack, disabled: isSaving, accessibleWhenDisabled: true, children: __("Cancel") } ), /* @__PURE__ */ jsx( Button, { __next40pxDefaultSize: true, variant: "primary", type: "submit", isBusy: isSaving, "aria-disabled": isSubmitDisabled, children: __("Create page") } ) ] }) ] }) }) }) } ); } export { LinkUIPageCreator }; //# sourceMappingURL=page-creator.js.map