UNPKG

@wordpress/block-library

Version:
1 lines • 7.21 kB
{"version":3,"file":"use-convert-classic-menu-to-block-menu.mjs","names":["coreStore"],"sources":["../../../src/navigation/edit/use-convert-classic-menu-to-block-menu.js"],"sourcesContent":["import { useRegistry, useDispatch } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { useState, useCallback } from '@wordpress/element';\nimport { __, sprintf } from '@wordpress/i18n';\nimport menuItemsToBlocks from '../menu-items-to-blocks';\n\nexport const CLASSIC_MENU_CONVERSION_SUCCESS = 'success';\nexport const CLASSIC_MENU_CONVERSION_ERROR = 'error';\nexport const CLASSIC_MENU_CONVERSION_PENDING = 'pending';\nexport const CLASSIC_MENU_CONVERSION_IDLE = 'idle';\n\n// This is needed to ensure that multiple components using this hook\n// do not import the same classic menu twice.\nlet classicMenuBeingConvertedId = null;\n\nfunction useConvertClassicToBlockMenu(\n\tcreateNavigationMenu,\n\t{ throwOnError = false } = {}\n) {\n\tconst registry = useRegistry();\n\tconst { editEntityRecord } = useDispatch( coreStore );\n\n\tconst [ status, setStatus ] = useState( CLASSIC_MENU_CONVERSION_IDLE );\n\tconst [ error, setError ] = useState( null );\n\n\tconst convertClassicMenuToBlockMenu = useCallback(\n\t\tasync ( menuId, menuName, postStatus = 'publish' ) => {\n\t\t\tlet navigationMenu;\n\t\t\tlet classicMenuItems;\n\n\t\t\t// 1. Fetch the classic Menu items.\n\t\t\ttry {\n\t\t\t\tclassicMenuItems = await registry\n\t\t\t\t\t.resolveSelect( coreStore )\n\t\t\t\t\t.getMenuItems( {\n\t\t\t\t\t\tmenus: menuId,\n\t\t\t\t\t\tper_page: -1,\n\t\t\t\t\t\tcontext: 'view',\n\t\t\t\t\t} );\n\t\t\t} catch ( err ) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\tsprintf(\n\t\t\t\t\t\t// translators: %s: The name of a menu (e.g. Header menu).\n\t\t\t\t\t\t__( `Unable to fetch classic menu \"%s\" from API.` ),\n\t\t\t\t\t\tmenuName\n\t\t\t\t\t),\n\t\t\t\t\t{\n\t\t\t\t\t\tcause: err,\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// Handle offline response which resolves to `null`.\n\t\t\tif ( classicMenuItems === null ) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\tsprintf(\n\t\t\t\t\t\t// translators: %s: The name of a menu (e.g. Header menu).\n\t\t\t\t\t\t__( `Unable to fetch classic menu \"%s\" from API.` ),\n\t\t\t\t\t\tmenuName\n\t\t\t\t\t)\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// 2. Convert the classic items into blocks.\n\t\t\tconst { innerBlocks } = menuItemsToBlocks( classicMenuItems );\n\n\t\t\t// 3. Create the `wp_navigation` Post with the blocks.\n\t\t\ttry {\n\t\t\t\tnavigationMenu = await createNavigationMenu(\n\t\t\t\t\tmenuName,\n\t\t\t\t\tinnerBlocks,\n\t\t\t\t\tpostStatus\n\t\t\t\t);\n\n\t\t\t\t/**\n\t\t\t\t * Immediately trigger editEntityRecord to change the wp_navigation post status to 'publish'.\n\t\t\t\t * This status change causes the menu to be displayed on the front of the site and sets the post state to be \"dirty\".\n\t\t\t\t * The problem being solved is if saveEditedEntityRecord was used here, the menu would be updated on the frontend and the editor _automatically_,\n\t\t\t\t * without user interaction.\n\t\t\t\t * If the user abandons the site editor without saving, there would still be a wp_navigation post created as draft.\n\t\t\t\t */\n\t\t\t\tawait editEntityRecord(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_navigation',\n\t\t\t\t\tnavigationMenu.id,\n\t\t\t\t\t{\n\t\t\t\t\t\tstatus: 'publish',\n\t\t\t\t\t},\n\t\t\t\t\t{ throwOnError: true }\n\t\t\t\t);\n\t\t\t} catch ( err ) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\tsprintf(\n\t\t\t\t\t\t// translators: %s: The name of a menu (e.g. Header menu).\n\t\t\t\t\t\t__( `Unable to create Navigation Menu \"%s\".` ),\n\t\t\t\t\t\tmenuName\n\t\t\t\t\t),\n\t\t\t\t\t{\n\t\t\t\t\t\tcause: err,\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn navigationMenu;\n\t\t},\n\t\t[ createNavigationMenu, editEntityRecord, registry ]\n\t);\n\n\tconst convert = useCallback(\n\t\tasync ( menuId, menuName, postStatus ) => {\n\t\t\t// Check whether this classic menu is being imported already.\n\t\t\tif ( classicMenuBeingConvertedId === menuId ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Set the ID for the currently importing classic menu.\n\t\t\tclassicMenuBeingConvertedId = menuId;\n\n\t\t\tif ( ! menuId || ! menuName ) {\n\t\t\t\tsetError( 'Unable to convert menu. Missing menu details.' );\n\t\t\t\tsetStatus( CLASSIC_MENU_CONVERSION_ERROR );\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsetStatus( CLASSIC_MENU_CONVERSION_PENDING );\n\t\t\tsetError( null );\n\n\t\t\treturn await convertClassicMenuToBlockMenu(\n\t\t\t\tmenuId,\n\t\t\t\tmenuName,\n\t\t\t\tpostStatus\n\t\t\t)\n\t\t\t\t.then( ( navigationMenu ) => {\n\t\t\t\t\tsetStatus( CLASSIC_MENU_CONVERSION_SUCCESS );\n\t\t\t\t\t// Reset the ID for the currently importing classic menu.\n\t\t\t\t\tclassicMenuBeingConvertedId = null;\n\t\t\t\t\treturn navigationMenu;\n\t\t\t\t} )\n\t\t\t\t.catch( ( err ) => {\n\t\t\t\t\tsetError( err?.message );\n\t\t\t\t\t// Reset the ID for the currently importing classic menu.\n\t\t\t\t\tsetStatus( CLASSIC_MENU_CONVERSION_ERROR );\n\n\t\t\t\t\t// Reset the ID for the currently importing classic menu.\n\t\t\t\t\tclassicMenuBeingConvertedId = null;\n\n\t\t\t\t\t// Rethrow error for debugging.\n\t\t\t\t\tif ( throwOnError ) {\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\tsprintf(\n\t\t\t\t\t\t\t\t// translators: %s: The name of a menu (e.g. Header menu).\n\t\t\t\t\t\t\t\t__( `Unable to create Navigation Menu \"%s\".` ),\n\t\t\t\t\t\t\t\tmenuName\n\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tcause: err,\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t} );\n\t\t},\n\t\t[ convertClassicMenuToBlockMenu, throwOnError ]\n\t);\n\n\treturn {\n\t\tconvert,\n\t\tstatus,\n\t\terror,\n\t};\n}\n\nexport default useConvertClassicToBlockMenu;\n"],"mappings":";;;;;;AAMA,MAAa,kCAAkC;AAC/C,MAAa,gCAAgC;AAC7C,MAAa,kCAAkC;AAC/C,MAAa,+BAA+B;AAI5C,IAAI,8BAA8B;AAElC,SAAS,6BACR,sBACA,EAAE,eAAe,UAAU,CAAC,GAC3B;CACD,MAAM,WAAW,YAAY;CAC7B,MAAM,EAAE,qBAAqB,YAAaA,KAAU;CAEpD,MAAM,CAAE,QAAQ,aAAc,SAAU,4BAA6B;CACrE,MAAM,CAAE,OAAO,YAAa,SAAU,IAAK;CAE3C,MAAM,gCAAgC,YACrC,OAAQ,QAAQ,UAAU,aAAa,cAAe;EACrD,IAAI;EACJ,IAAI;EAGJ,IAAI;GACH,mBAAmB,MAAM,SACvB,cAAeA,KAAU,CAAC,CAC1B,aAAc;IACd,OAAO;IACP,UAAU;IACV,SAAS;GACV,CAAE;EACJ,SAAU,KAAM;GACf,MAAM,IAAI,MACT,QAEC,GAAI,6CAA8C,GAClD,QACD,GACA,EACC,OAAO,IACR,CACD;EACD;EAGA,IAAK,qBAAqB,MACzB,MAAM,IAAI,MACT,QAEC,GAAI,6CAA8C,GAClD,QACD,CACD;EAID,MAAM,EAAE,gBAAgB,kBAAmB,gBAAiB;EAG5D,IAAI;GACH,iBAAiB,MAAM,qBACtB,UACA,aACA,UACD;;;;;;;;GASA,MAAM,iBACL,YACA,iBACA,eAAe,IACf,EACC,QAAQ,UACT,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