@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 5.11 kB
Source Map (JSON)
{"version":3,"file":"use-create-navigation-menu.cjs","names":["useState","useDispatch","coreStore","useGenerateDefaultNavigationTitle","useCallback","serialize"],"sources":["../../../src/navigation/edit/use-create-navigation-menu.js"],"sourcesContent":["import { serialize } from '@wordpress/blocks';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { useDispatch } from '@wordpress/data';\nimport { useState, useCallback } from '@wordpress/element';\nimport useGenerateDefaultNavigationTitle from './use-generate-default-navigation-title';\n\nexport const CREATE_NAVIGATION_MENU_SUCCESS = 'success';\nexport const CREATE_NAVIGATION_MENU_ERROR = 'error';\nexport const CREATE_NAVIGATION_MENU_PENDING = 'pending';\nexport const CREATE_NAVIGATION_MENU_IDLE = 'idle';\n\nexport default function useCreateNavigationMenu( clientId ) {\n\tconst [ status, setStatus ] = useState( CREATE_NAVIGATION_MENU_IDLE );\n\tconst [ value, setValue ] = useState( null );\n\tconst [ error, setError ] = useState( null );\n\n\tconst { saveEntityRecord, editEntityRecord } = useDispatch( coreStore );\n\tconst generateDefaultTitle = useGenerateDefaultNavigationTitle( clientId );\n\n\t// This callback uses data from the two placeholder steps and only creates\n\t// a new navigation menu when the user completes the final step.\n\tconst create = useCallback(\n\t\tasync ( title = null, blocks = [], postStatus ) => {\n\t\t\t// Guard against creating Navigations without a title.\n\t\t\t// Note you can pass no title, but if one is passed it must be\n\t\t\t// a string otherwise the title may end up being empty.\n\t\t\tif ( title && typeof title !== 'string' ) {\n\t\t\t\tsetError(\n\t\t\t\t\t'Invalid title supplied when creating Navigation Menu.'\n\t\t\t\t);\n\t\t\t\tsetStatus( CREATE_NAVIGATION_MENU_ERROR );\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Value of supplied title argument was not a string.`\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tsetStatus( CREATE_NAVIGATION_MENU_PENDING );\n\t\t\tsetValue( null );\n\t\t\tsetError( null );\n\n\t\t\tif ( ! title ) {\n\t\t\t\ttitle = await generateDefaultTitle().catch( ( err ) => {\n\t\t\t\t\tsetError( err?.message );\n\t\t\t\t\tsetStatus( CREATE_NAVIGATION_MENU_ERROR );\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t'Failed to create title when saving new Navigation Menu.',\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcause: err,\n\t\t\t\t\t\t}\n\t\t\t\t\t);\n\t\t\t\t} );\n\t\t\t}\n\t\t\tconst record = {\n\t\t\t\ttitle,\n\t\t\t\tcontent: serialize( blocks ),\n\t\t\t\tstatus: postStatus,\n\t\t\t};\n\n\t\t\t// Return affords ability to await on this function directly\n\t\t\treturn saveEntityRecord( 'postType', 'wp_navigation', record )\n\t\t\t\t.then( ( response ) => {\n\t\t\t\t\tsetValue( response );\n\t\t\t\t\tsetStatus( CREATE_NAVIGATION_MENU_SUCCESS );\n\n\t\t\t\t\t// Set the status to publish so that the Navigation block\n\t\t\t\t\t// shows up in the multi entity save flow.\n\t\t\t\t\tif ( postStatus !== 'publish' ) {\n\t\t\t\t\t\teditEntityRecord(\n\t\t\t\t\t\t\t'postType',\n\t\t\t\t\t\t\t'wp_navigation',\n\t\t\t\t\t\t\tresponse.id,\n\t\t\t\t\t\t\t{ status: 'publish' }\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\treturn response;\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\tsetStatus( CREATE_NAVIGATION_MENU_ERROR );\n\t\t\t\t\tthrow new Error( 'Unable to save new Navigation Menu', {\n\t\t\t\t\t\tcause: err,\n\t\t\t\t\t} );\n\t\t\t\t} );\n\t\t},\n\t\t[ saveEntityRecord, editEntityRecord, generateDefaultTitle ]\n\t);\n\n\treturn {\n\t\tcreate,\n\t\tstatus,\n\t\tvalue,\n\t\terror,\n\t\tisIdle: status === CREATE_NAVIGATION_MENU_IDLE,\n\t\tisPending: status === CREATE_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