@wordpress/block-library
Version:
Block library for the WordPress editor.
117 lines (116 loc) • 4.24 kB
JavaScript
import { Button, CheckboxControl, Notice, TextControl, __experimentalHStack, __experimentalVStack } from "@wordpress/components";
import { __, sprintf } from "@wordpress/i18n";
import { useDispatch, useSelect } from "@wordpress/data";
import { store } from "@wordpress/core-data";
import { store as store$1 } from "@wordpress/notices";
import { decodeEntities } from "@wordpress/html-entities";
import { useState } from "@wordpress/element";
import DialogWrapper from "./dialog-wrapper.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/navigation-link/link-ui/page-creator.jsx
/**
* Component for creating new pages within the Navigation Link UI.
*
* @param {Object} props Component props.
* @param {string} props.postType The post type to create.
* @param {Function} props.onBack Callback when user wants to go back.
* @param {Function} props.onPageCreated Callback when page is successfully created.
* @param {string} [props.initialTitle] Initial title to pre-fill the form.
*/
function LinkUIPageCreator({ postType, onBack, onPageCreated, initialTitle = "" }) {
const [title, setTitle] = useState(initialTitle);
const [shouldPublish, setShouldPublish] = useState(true);
const isTitleValid = title.trim().length > 0;
const { lastError, isSaving } = useSelect((select) => ({
lastError: select(store).getLastEntitySaveError("postType", postType),
isSaving: select(store).isSavingEntityRecord("postType", postType)
}), [postType]);
const { saveEntityRecord } = useDispatch(store);
const { createSuccessNotice, createErrorNotice } = useDispatch(store$1);
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(__("%s page created successfully."), decodeEntities(savedRecord.title.rendered)), {
type: "snackbar",
id: "page-created-success"
});
onPageCreated(pageLink);
}
} catch {
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(__experimentalVStack, {
className: "link-ui-page-creator__inner",
spacing: 4,
children: /* @__PURE__ */ jsx("form", {
onSubmit: createPage,
children: /* @__PURE__ */ jsxs(__experimentalVStack, {
spacing: 4,
children: [
/* @__PURE__ */ jsx(TextControl, {
label: __("Title"),
onChange: setTitle,
placeholder: __("No title"),
value: title
}),
/* @__PURE__ */ jsx(CheckboxControl, {
label: __("Publish"),
help: __("Turn off to save as a draft. Drafts won't appear on your site until published."),
checked: shouldPublish,
onChange: setShouldPublish
}),
lastError && /* @__PURE__ */ jsx(Notice, {
status: "error",
isDismissible: false,
children: lastError.message
}),
/* @__PURE__ */ jsxs(__experimentalHStack, {
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.mjs.map