Skip to content

Commit

Permalink
[Mobile] Create endpoint to post notices from host apps (#25266)
Browse files Browse the repository at this point in the history
* Create endpoint to post notices from host apps

* Add subscription mock for mobile tests
  • Loading branch information
chipsnyder authored Oct 2, 2020
1 parent 755ef78 commit f31a260
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/editor/src/components/provider/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import RNReactNativeGutenbergBridge, {
subscribeReplaceBlock,
subscribeUpdateTheme,
subscribeUpdateCapabilities,
subscribeShowNotice,
} from '@wordpress/react-native-bridge';

/**
Expand Down Expand Up @@ -137,6 +138,12 @@ class NativeEditorProvider extends Component {
this.updateCapabilitiesAction( payload );
}
);

this.subscriptionParentShowNotice = subscribeShowNotice(
( payload ) => {
this.props.createInfoNotice( payload.message );
}
);
}

componentWillUnmount() {
Expand Down Expand Up @@ -171,6 +178,10 @@ class NativeEditorProvider extends Component {
if ( this.subscriptionParentUpdateCapabilities ) {
this.subscriptionParentUpdateCapabilities.remove();
}

if ( this.subscriptionParentShowNotice ) {
this.subscriptionParentShowNotice.remove();
}
}

componentDidUpdate( prevProps ) {
Expand Down Expand Up @@ -291,7 +302,9 @@ export default compose( [
};
} ),
withDispatch( ( dispatch ) => {
const { editPost, resetEditorBlocks } = dispatch( 'core/editor' );
const { editPost, resetEditorBlocks, createInfoNotice } = dispatch(
'core/editor'
);
const {
updateSettings,
clearSelectedBlock,
Expand All @@ -306,6 +319,7 @@ export default compose( [
addEntities,
clearSelectedBlock,
insertBlock,
createInfoNotice,
editTitle( title ) {
editPost( { title } );
},
Expand Down
4 changes: 4 additions & 0 deletions packages/react-native-bridge/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ export function subscribeUpdateCapabilities( callback ) {
return gutenbergBridgeEvents.addListener( 'updateCapabilities', callback );
}

export function subscribeShowNotice( callback ) {
return gutenbergBridgeEvents.addListener( 'showNotice', callback );
}

/**
* @callback FnReplaceBlockCompletion
* @param {string} html the HTML to replace the block.
Expand Down
4 changes: 4 additions & 0 deletions packages/react-native-bridge/ios/Gutenberg.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ public class Gutenberg: NSObject {

bridgeModule.sendEventIfNeeded(.updateTheme, body:themeUpdates)
}

public func showNotice(_ message: String) {
sendEvent(.showNotice, body: ["message": message])
}
}

extension Gutenberg: RCTBridgeDelegate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ extension RNReactNativeGutenbergBridge {
case updateTheme
case replaceBlock
case updateCapabilities
case showNotice
}

public override func supportedEvents() -> [String]! {
Expand Down
1 change: 1 addition & 0 deletions test/native/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jest.mock( '@wordpress/react-native-bridge', () => {
subscribeUpdateTheme: jest.fn(),
subscribePreferredColorScheme: () => 'light',
subscribeUpdateCapabilities: jest.fn(),
subscribeShowNotice: jest.fn(),
editorDidMount: jest.fn(),
editorDidAutosave: jest.fn(),
subscribeMediaUpload: jest.fn(),
Expand Down

0 comments on commit f31a260

Please sign in to comment.