UNPKG

@wordpress/block-library

Version:
78 lines (77 loc) • 3.64 kB
import { useDispatch, useRegistry } from "@wordpress/data"; import { store } from "@wordpress/core-data"; import { useCallback, useState } from "@wordpress/element"; import { __, sprintf } from "@wordpress/i18n"; import menuItemsToBlocks from "../menu-items-to-blocks.mjs"; // packages/block-library/src/navigation/edit/use-convert-classic-menu-to-block-menu.js const CLASSIC_MENU_CONVERSION_SUCCESS = "success"; const CLASSIC_MENU_CONVERSION_ERROR = "error"; const CLASSIC_MENU_CONVERSION_PENDING = "pending"; const CLASSIC_MENU_CONVERSION_IDLE = "idle"; let classicMenuBeingConvertedId = null; function useConvertClassicToBlockMenu(createNavigationMenu, { throwOnError = false } = {}) { const registry = useRegistry(); const { editEntityRecord } = useDispatch(store); const [status, setStatus] = useState(CLASSIC_MENU_CONVERSION_IDLE); const [error, setError] = useState(null); const convertClassicMenuToBlockMenu = useCallback(async (menuId, menuName, postStatus = "publish") => { let navigationMenu; let classicMenuItems; try { classicMenuItems = await registry.resolveSelect(store).getMenuItems({ menus: menuId, per_page: -1, context: "view" }); } catch (err) { throw new Error(sprintf(__(`Unable to fetch classic menu "%s" from API.`), menuName), { cause: err }); } if (classicMenuItems === null) throw new Error(sprintf(__(`Unable to fetch classic menu "%s" from API.`), menuName)); const { innerBlocks } = menuItemsToBlocks(classicMenuItems); try { navigationMenu = await createNavigationMenu(menuName, innerBlocks, postStatus); /** * Immediately trigger editEntityRecord to change the wp_navigation post status to 'publish'. * This status change causes the menu to be displayed on the front of the site and sets the post state to be "dirty". * The problem being solved is if saveEditedEntityRecord was used here, the menu would be updated on the frontend and the editor _automatically_, * without user interaction. * If the user abandons the site editor without saving, there would still be a wp_navigation post created as draft. */ await editEntityRecord("postType", "wp_navigation", navigationMenu.id, { status: "publish" }, { throwOnError: true }); } catch (err) { throw new Error(sprintf(__(`Unable to create Navigation Menu "%s".`), menuName), { cause: err }); } return navigationMenu; }, [ createNavigationMenu, editEntityRecord, registry ]); return { convert: useCallback(async (menuId, menuName, postStatus) => { if (classicMenuBeingConvertedId === menuId) return; classicMenuBeingConvertedId = menuId; if (!menuId || !menuName) { setError("Unable to convert menu. Missing menu details."); setStatus(CLASSIC_MENU_CONVERSION_ERROR); return; } setStatus(CLASSIC_MENU_CONVERSION_PENDING); setError(null); return await convertClassicMenuToBlockMenu(menuId, menuName, postStatus).then((navigationMenu) => { setStatus(CLASSIC_MENU_CONVERSION_SUCCESS); classicMenuBeingConvertedId = null; return navigationMenu; }).catch((err) => { setError(err?.message); setStatus(CLASSIC_MENU_CONVERSION_ERROR); classicMenuBeingConvertedId = null; if (throwOnError) throw new Error(sprintf(__(`Unable to create Navigation Menu "%s".`), menuName), { cause: err }); }); }, [convertClassicMenuToBlockMenu, throwOnError]), status, error }; } export { CLASSIC_MENU_CONVERSION_ERROR, CLASSIC_MENU_CONVERSION_IDLE, CLASSIC_MENU_CONVERSION_PENDING, CLASSIC_MENU_CONVERSION_SUCCESS, useConvertClassicToBlockMenu as default }; //# sourceMappingURL=use-convert-classic-menu-to-block-menu.mjs.map