Skip to content

Commit

Permalink
support other post types in create function
Browse files Browse the repository at this point in the history
  • Loading branch information
marekhrabe authored and getdave committed Feb 10, 2020
1 parent 7509796 commit 73eb46f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
29 changes: 18 additions & 11 deletions packages/block-editor/src/components/link-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ function LinkControl( {
onChange = noop,
showInitialSuggestions,
forceIsEditingLink,
createEmptyPage,
showCreateEntity,
createEntity,
} ) {
const wrapperNode = useRef();
const instanceId = useInstanceId( LinkControl );
Expand Down Expand Up @@ -277,7 +278,7 @@ function LinkControl( {

const directLinkEntryTypes = [ 'url', 'mailto', 'tel', 'internal' ];
const isSingleDirectEntryResult = suggestions.length === 1 && directLinkEntryTypes.includes( suggestions[ 0 ].type.toLowerCase() );
const shouldShowCreateEntity = showCreateEntity && createEmptyPage && ! isSingleDirectEntryResult;
const shouldShowCreateEntity = showCreateEntity && createEntity && ! isSingleDirectEntryResult;
: undefined;
const labelText = isInitialSuggestions
? __( 'Recently updated' )
Expand Down Expand Up @@ -310,16 +311,22 @@ function LinkControl( {
searchTerm={ inputValue }
onClick={ async () => {
setIsResolvingLink( true );
const newPage = await createEmptyPage( inputValue );
// TODO: handle error from API
let newEntity;
try {
newEntity = await createEntity( 'page', inputValue );
} catch ( error ) {
// console.log( error );
// TODO: state for error
}

setIsResolvingLink( false );
onChange( {
id: newPage.id,
title: newPage.title.raw, // TODO: use raw or rendered?
url: newPage.link,
type: newPage.type,
} );
setIsEditingLink( false );

if ( newEntity ) {
onChange( newEntity );
setIsEditingLink( false );
} else {
setIsEditingLink( true );
}
} }
key={ `${ suggestion.id }-${ suggestion.type }` }
itemProps={ buildSuggestionItemProps( suggestion, index ) }
Expand Down
10 changes: 5 additions & 5 deletions packages/block-editor/src/components/link-control/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,8 @@ describe( 'Creating Entities (eg: Posts, Pages)', () => {
onChange={ ( suggestion ) => {
setLink( suggestion );
} }
createEmptyPage={ ( title ) => Promise.resolve( {
type: 'page', // here we're returning a Page but any entity can be returned
createEntity={ ( type, title ) => Promise.resolve( {
type,
id: 123,
title: {
raw: title,
Expand Down Expand Up @@ -574,8 +574,8 @@ describe( 'Creating Entities (eg: Posts, Pages)', () => {
<LinkControl
showInitialSuggestions={ true } // should show even if we're not showing initial suggestions
showCreateEntity={ true }
createEmptyPage={ ( title = '(no title)' ) => Promise.resolve( {
type: 'page', // here we're returning a Page but any entity can be returned
createEntity={ ( type, title = '(no title)' ) => Promise.resolve( {
type,
id: 123,
title: {
raw: title,
Expand Down Expand Up @@ -613,7 +613,7 @@ describe( 'Creating Entities (eg: Posts, Pages)', () => {
render(
<LinkControl
showCreateEntity={ true }
createEmptyPage={ jest.fn() }
createEntity={ jest.fn() }
/>, container
);
} );
Expand Down
24 changes: 13 additions & 11 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function NavigationLinkEdit( {
backgroundColor,
rgbTextColor,
rgbBackgroundColor,
savePage,
saveEntityRecord,
} ) {
const { label, opensInNewTab, url, nofollow, description } = attributes;
const link = {
Expand Down Expand Up @@ -229,12 +229,16 @@ function NavigationLinkEdit( {
value={ link }
showInitialSuggestions={ true }
showCreateEntity={ true }
createEmptyPage={ ( pageTitle ) =>
savePage( {
title: pageTitle,
content: '',
status: 'publish', // TODO: use publish?
} )
createEntity={ ( type, entityTitle ) =>
saveEntityRecord( 'postType', type, {
title: entityTitle,
status: 'publish',
} ).then( ( entity ) => ( {
id: entity.id,
type,
title: entity.title.raw, // TODO: use raw or rendered?
url: entity.link,
} ) )
}
onChange={ ( {
title: newTitle = '',
Expand Down Expand Up @@ -349,7 +353,9 @@ export default compose( [
};
} ),
withDispatch( ( dispatch, ownProps, registry ) => {
const { saveEntityRecord } = dispatch( 'core' );
return {
saveEntityRecord,
insertLinkBlock() {
const { clientId } = ownProps;

Expand All @@ -365,10 +371,6 @@ export default compose( [

insertBlock( blockToInsert, insertionPoint, clientId );
},
savePage( page ) {
const { saveEntityRecord } = dispatch( 'core' );
return saveEntityRecord( 'postType', 'page', page );
},
};
} ),
] )( NavigationLinkEdit );

0 comments on commit 73eb46f

Please sign in to comment.