Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/components/ScreenWrapper/BaseScreenWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {KeyboardAvoidingView, View} from 'react-native';
import React from 'react';
import {SafeAreaInsetsContext} from 'react-native-safe-area-context';
import _ from 'underscore';
import {withOnyx} from 'react-native-onyx';
import CONST from '../../CONST';
import KeyboardShortcut from '../../libs/KeyboardShortcut';
import Navigation from '../../libs/Navigation/Navigation';
Expand All @@ -18,6 +17,7 @@ import withWindowDimensions from '../withWindowDimensions';
import ONYXKEYS from '../../ONYXKEYS';
import {withNetwork} from '../OnyxProvider';
import {propTypes, defaultProps} from './propTypes';
import onyxSubscribe from '../../libs/onyxSubscribe';

class BaseScreenWrapper extends React.Component {
constructor(props) {
Expand All @@ -29,9 +29,18 @@ class BaseScreenWrapper extends React.Component {
}

componentDidMount() {
let willAlertModalBecomeVisible = false;

this.unsubscribeOnyx = onyxSubscribe({
key: ONYXKEYS.MODAL,
callback: (object) => {
willAlertModalBecomeVisible = object.willAlertModalBecomeVisible;
},
});
Comment on lines +34 to +39

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be the first instance of such use of Onyx. What about moving this outside the component in lib and using the function to check the status of modal visibility?

There are many examples of it in actions. e.g.

Onyx.connect({

Because this is not an action, you can put it in a lib.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah i agree, that would be good, will reactor!


const shortcutConfig = CONST.KEYBOARD_SHORTCUTS.ESCAPE;
this.unsubscribeEscapeKey = KeyboardShortcut.subscribe(shortcutConfig.shortcutKey, () => {
if (this.props.modal.willAlertModalBecomeVisible) {
if (willAlertModalBecomeVisible) {
return;
}

Expand All @@ -51,6 +60,9 @@ class BaseScreenWrapper extends React.Component {
if (this.unsubscribeTransitionEnd) {
this.unsubscribeTransitionEnd();
}
if (this.unsubscribeOnyx) {
this.unsubscribeOnyx();
}
}

render() {
Expand Down Expand Up @@ -106,10 +118,5 @@ BaseScreenWrapper.defaultProps = defaultProps;
export default compose(
withNavigation,
withWindowDimensions,
withOnyx({
modal: {
key: ONYXKEYS.MODAL,
},
}),
withNetwork(),
)(BaseScreenWrapper);
12 changes: 12 additions & 0 deletions src/libs/onyxSubscribe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Onyx from 'react-native-onyx';

/**
* Connect to onyx data. Same params as Onyx.connect(), but returns a function to unsubscribe.
*
* @param {Object} mapping Same as for Onyx.connect()
* @return {function(): void} Unsubscribe callback
*/
export default (mapping) => {
const connectionId = Onyx.connect(mapping);
return () => Onyx.disconnect(connectionId);
};
Comment on lines +9 to +12

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to myself: refactor to be a function with a name that I then default export (code style)