Skip to content

Commit

Permalink
Update hook usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Jul 19, 2024
1 parent 6ca381b commit 70455fb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
22 changes: 8 additions & 14 deletions packages/block-library/src/navigation-link/link-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,18 @@ function LinkUIBlockInserter( { clientId, onBack, onSelectBlock } ) {
}

function UnforwardedLinkUI( props, ref ) {
const { label, url, opensInNewTab, type, kind } = props.link;
const postType = type || 'page';

const [ addingBlock, setAddingBlock ] = useState( false );
const [ focusAddBlockButton, setFocusAddBlockButton ] = useState( false );
const { saveEntityRecord } = useDispatch( coreStore );
const pagesPermissions = useResourcePermissions( 'pages' );
const postsPermissions = useResourcePermissions( 'posts' );
const permissions = useResourcePermissions( {
kind: 'postType',
name: postType,
} );

async function handleCreate( pageTitle ) {
const postType = props.link.type || 'page';

const page = await saveEntityRecord( 'postType', postType, {
title: pageTitle,
status: 'draft',
Expand All @@ -180,15 +183,6 @@ function UnforwardedLinkUI( props, ref ) {
};
}

const { label, url, opensInNewTab, type, kind } = props.link;

let userCanCreate = false;
if ( ! type || type === 'page' ) {
userCanCreate = pagesPermissions.canCreate;
} else if ( type === 'post' ) {
userCanCreate = postsPermissions.canCreate;
}

// Memoize link value to avoid overriding the LinkControl's internal state.
// This is a temporary fix. See https://github.com/WordPress/gutenberg/issues/50976#issuecomment-1568226407.
const link = useMemo(
Expand Down Expand Up @@ -241,7 +235,7 @@ function UnforwardedLinkUI( props, ref ) {
hasRichPreviews
value={ link }
showInitialSuggestions
withCreateSuggestion={ userCanCreate }
withCreateSuggestion={ permissions.canCreate }
createSuggestion={ handleCreate }
createSuggestionButtonText={ ( searchTerm ) => {
let format;
Expand Down
6 changes: 5 additions & 1 deletion packages/block-library/src/navigation/use-navigation-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import { useSelect } from '@wordpress/data';
import { PRELOADED_NAVIGATION_MENUS_QUERY } from './constants';

export default function useNavigationMenu( ref ) {
const permissions = useResourcePermissions( 'navigation', ref );
const permissions = useResourcePermissions( {
kind: 'postType',
name: 'wp_navigation',
id: ref,
} );

const {
navigationMenu,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import { SlotFillProvider } from '@wordpress/components';
import { useViewportMatch } from '@wordpress/compose';
import { uploadMedia } from '@wordpress/media-utils';
import { useDispatch, useSelect } from '@wordpress/data';
import {
useEntityBlockEditor,
store as coreStore,
useResourcePermissions,
} from '@wordpress/core-data';
import { useEntityBlockEditor, store as coreStore } from '@wordpress/core-data';
import { useMemo } from '@wordpress/element';
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
import { privateApis as editPatternsPrivateApis } from '@wordpress/patterns';
Expand Down Expand Up @@ -37,9 +33,9 @@ export default function WidgetAreasBlockEditorProvider( {
children,
...props
} ) {
const mediaPermissions = useResourcePermissions( 'media' );
const isLargeViewport = useViewportMatch( 'medium' );
const {
hasUploadPermissions,
reusableBlocks,
isFixedToolbarActive,
keepCaretInsideBlock,
Expand All @@ -55,6 +51,11 @@ export default function WidgetAreasBlockEditorProvider( {
? getEntityRecord( 'root', 'site' )
: undefined;
return {
hasUploadPermissions:
canUser( 'create', {
kind: 'root',
name: 'media',
} ) ?? true,
widgetAreas: select( editWidgetsStore ).getWidgetAreas(),
widgets: select( editWidgetsStore ).getWidgets(),
reusableBlocks: ALLOW_REUSABLE_BLOCKS
Expand All @@ -76,7 +77,7 @@ export default function WidgetAreasBlockEditorProvider( {

const settings = useMemo( () => {
let mediaUploadBlockEditor;
if ( mediaPermissions.canCreate ) {
if ( hasUploadPermissions ) {
mediaUploadBlockEditor = ( { onError, ...argumentsObject } ) => {
uploadMedia( {
wpAllowedMimeTypes: blockEditorSettings.allowedMimeTypes,
Expand All @@ -97,11 +98,11 @@ export default function WidgetAreasBlockEditorProvider( {
pageForPosts,
};
}, [
hasUploadPermissions,
blockEditorSettings,
isFixedToolbarActive,
isLargeViewport,
keepCaretInsideBlock,
mediaPermissions.canCreate,
reusableBlocks,
setIsInserterOpened,
pageOnFront,
Expand Down

0 comments on commit 70455fb

Please sign in to comment.