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 17, 2024
1 parent f45b6b8 commit ec2abb7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 37 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
15 changes: 1 addition & 14 deletions packages/block-library/src/navigation-submenu/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
import { isURL, prependHTTP } from '@wordpress/url';
import { useState, useEffect, useRef } from '@wordpress/element';
import { link as linkIcon, removeSubmenu } from '@wordpress/icons';
import { useResourcePermissions } from '@wordpress/core-data';
import { speak } from '@wordpress/a11y';
import { createBlock } from '@wordpress/blocks';
import { useMergeRefs, usePrevious } from '@wordpress/compose';
Expand Down Expand Up @@ -134,8 +133,7 @@ export default function NavigationSubmenuEdit( {
context,
clientId,
} ) {
const { label, type, url, description, rel, title } = attributes;

const { label, url, description, rel, title } = attributes;
const { showSubmenuIcon, maxNestingLevel, openSubmenusOnClick } = context;

const {
Expand All @@ -154,9 +152,6 @@ export default function NavigationSubmenuEdit( {
const itemLabelPlaceholder = __( 'Add text…' );
const ref = useRef();

const pagesPermissions = useResourcePermissions( 'pages' );
const postsPermissions = useResourcePermissions( 'posts' );

const {
parentCount,
isParentOfSelectedBlock,
Expand Down Expand Up @@ -264,13 +259,6 @@ export default function NavigationSubmenuEdit( {
selection.addRange( range );
}

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

const {
textColor,
customTextColor,
Expand Down Expand Up @@ -499,7 +487,6 @@ export default function NavigationSubmenuEdit( {
}
} }
anchor={ popoverAnchor }
hasCreateSuggestion={ userCanCreate }
onRemove={ () => {
setAttributes( { url: '' } );
speak( __( 'Link removed.' ), 'assertive' );
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 ec2abb7

Please sign in to comment.