Skip to content

Commit

Permalink
Initialize the core data store with default entities so it does not f…
Browse files Browse the repository at this point in the history
…ail to load the current post
  • Loading branch information
Tug committed Sep 26, 2019
1 parent 05f7506 commit ad8d8d5
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 9 deletions.
10 changes: 10 additions & 0 deletions packages/api-fetch/src/index.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

function apiFetch( options ) {
// eslint-disable-next-line no-console
console.warn( 'apiFetch called with options', options );

// return a promise that never resolves
return new Promise( () => {} );
}

export default apiFetch;
1 change: 1 addition & 0 deletions packages/block-directory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"main": "build/index.js",
"module": "build-module/index.js",
"react-native": "src/index",
"dependencies": {
"@wordpress/api-fetch": "file:../api-fetch",
"@wordpress/block-editor": "file:../block-editor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { RawHTML } from '@wordpress/element';
import { withDispatch, withSelect } from '@wordpress/data';
import { pasteHandler, isUnmodifiedDefaultBlock } from '@wordpress/blocks';
import { withInstanceId, compose } from '@wordpress/compose';
import { RichText } from '@wordpress/rich-text';
import { __experimentalRichText as RichText } from '@wordpress/rich-text';

/**
* Internal dependencies
Expand Down
4 changes: 3 additions & 1 deletion packages/edit-post/src/editor.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ class Editor extends Component {
// For now, let's assume: serialize( parse( html ) ) !== html
raw: serialize( parse( props.initialHtml || '' ) ),
},
type: 'draft',
type: 'post',
status: 'auto-draft',
meta: [],
};

return (
Expand Down
7 changes: 7 additions & 0 deletions packages/edit-post/src/test/editor.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
import RNReactNativeGutenbergBridge from 'react-native-gutenberg-bridge';
import { mount } from 'enzyme';
import { act } from 'react-dom/test-utils';

/**
* WordPress dependencies
*/
Expand All @@ -28,9 +30,14 @@ describe( 'Editor', () => {
beforeAll( registerCoreBlocks );

it( 'detects unsupported block and sends hasUnsupportedBlocks true to native', () => {
jest.useFakeTimers();
RNReactNativeGutenbergBridge.editorDidMount = jest.fn();

const appContainer = renderEditorWith( unsupportedBlock );
// for some reason resetEditorBlocks() is asynchronous when dispatching editEntityRecord
act( () => {
jest.runAllTicks();
} );
appContainer.unmount();

expect( RNReactNativeGutenbergBridge.editorDidMount ).toHaveBeenCalledTimes( 1 );
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/components/post-title/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { isEmpty } from 'lodash';
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { RichText } from '@wordpress/rich-text';
import { __experimentalRichText as RichText } from '@wordpress/rich-text';
import { decodeEntities } from '@wordpress/html-entities';
import { withDispatch, withSelect } from '@wordpress/data';
import { withFocusOutside } from '@wordpress/components';
Expand Down
28 changes: 26 additions & 2 deletions packages/editor/src/components/provider/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,35 @@ import { parse, serialize, getUnregisteredTypeHandlerName } from '@wordpress/blo
import { withDispatch, withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';

const postTypeEntities = [
{ name: 'post', baseURL: '/wp/v2/posts' },
{ name: 'page', baseURL: '/wp/v2/pages' },
{ name: 'attachment', baseURL: '/wp/v2/media' },
{ name: 'wp_block', baseURL: '/wp/v2/blocks' },
].map( ( postTypeEntity ) => ( {
kind: 'postType',
...postTypeEntity,
transientEdits: {
blocks: true,
},
mergedEdits: {
meta: true,
},
} ) );

/**
* Internal dependencies
*/
import EditorProvider from './index.js';

class NativeEditorProvider extends Component {
constructor( props ) {
constructor() {
super( ...arguments );

// Keep a local reference to `post` to detect changes
this.post = props.post;
this.post = this.props.post;
this.props.addEntities( postTypeEntities );
this.props.receiveEntityRecords( 'postType', this.post.type, this.post );

this.setTitleRef = this.setTitleRef.bind( this );
}
Expand Down Expand Up @@ -169,12 +187,18 @@ export default compose( [
const {
switchEditorMode,
} = dispatch( 'core/edit-post' );
const {
addEntities,
receiveEntityRecords,
} = dispatch( 'core' );

return {
addEntities,
clearSelectedBlock,
editTitle( title ) {
editPost( { title } );
},
receiveEntityRecords,
resetEditorBlocksWithoutUndoLevel( blocks ) {
resetEditorBlocks( blocks, {
__unstableShouldCreateUndoLevel: false,
Expand Down
9 changes: 5 additions & 4 deletions packages/editor/src/store/reducer.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import {
preferences,
saving,
postLock,
postSavingLock,
reusableBlocks,
template,
postSavingLock,
isReady,
editorSettings,
} from './reducer.js';
Expand All @@ -29,8 +29,8 @@ export * from './reducer.js';
/**
* Reducer returning the post title state.
*
* @param {PostTitleState} state Current state.
* @param {Object} action Dispatched action.
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*
* @return {Object} Updated state.
*/
Expand All @@ -48,12 +48,13 @@ export const postTitle = combineReducers( {
export default optimist( combineReducers( {
postId,
postType,
postTitle,
preferences,
saving,
postLock,
postSavingLock,
reusableBlocks,
template,
postSavingLock,
isReady,
editorSettings,
} ) );

0 comments on commit ad8d8d5

Please sign in to comment.