Skip to content

Commit

Permalink
extract into hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras authored and vcanales committed Jun 4, 2021
1 parent 4735640 commit ff86d7a
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,34 @@ export const updateNavigationLinkBlockAttributes = (
} );
};

const useIsInvalidLink = ( kind, type, id ) => {
const isPostType = kind === 'post-type' || type === 'post';
const hasId = Number.isInteger( id );
const postStatus = useSelect(
( select ) => {
if ( ! isPostType ) {
return null;
}
const { getEntityRecord } = select( coreStore );
return getEntityRecord( 'postType', type, id )?.status;
},
[ isPostType, type, id ]
);

// Check Navigation Link validity if:
// 1. Link is 'post-type'.
// 2. It has an id.
// 3. It's neither null, nor undefined, as valid items might be either of those while loading.
// If those conditions are met, check if
// 1. The post status is published.
// 2. The Navigation Link item has no label.
// If either of those is true, invalidate.
const isInvalid =
isPostType && hasId && postStatus && 'publish' !== postStatus;

return isInvalid;
};

export default function NavigationLinkEdit( {
attributes,
isSelected,
Expand Down Expand Up @@ -242,35 +270,7 @@ export default function NavigationLinkEdit( {
const isDraggingWithin = useIsDraggingWithin( listItemRef );
const itemLabelPlaceholder = __( 'Add link…' );
const ref = useRef();

const isPostType =
( kind && 'post-type' === kind ) || ( type && 'post' === type );

const hasId = id && ! isNaN( id );
const postStatus = useSelect(
( select ) => {
if ( ! isPostType ) {
return null;
}

const { getEntityRecord } = select( coreStore );
const entityRecord = getEntityRecord( 'postType', type, id );

return entityRecord?.status;
},
[ isPostType, type, id ]
);

// Check Navigation Link validity if:
// 1. Link is 'post-type'.
// 2. It has an id.
// 3. It's neither null, nor undefined, as valid items might be either of those while loading.
// If those conditions are met, check if
// 1. The post status is published.
// 2. The Navigation Link item has no label.
// If either of those is true, invalidate.
const isInvalid =
isPostType && hasId && postStatus && 'publish' !== postStatus;
const isInvalid = useIsInvalidLink( kind, type, id );

const {
isAtMaxNesting,
Expand Down

0 comments on commit ff86d7a

Please sign in to comment.