Skip to content

Commit

Permalink
Add Navigation.DismissModal component
Browse files Browse the repository at this point in the history
  • Loading branch information
HorusGoul committed Jun 14, 2021
1 parent 7a21fbc commit 5c1003f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
31 changes: 30 additions & 1 deletion src/libs/Navigation/Navigation.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from 'underscore';
import React from 'react';
import {StackActions, DrawerActions} from '@react-navigation/native';

import PropTypes from 'prop-types';
import Onyx from 'react-native-onyx';
import linkTo from './linkTo';
import ROUTES from '../../ROUTES';
Expand Down Expand Up @@ -98,8 +98,37 @@ function dismissModal(shouldOpenDrawer = false) {
openDrawer();
}

/**
* Alternative to the `Navigation.dismissModal()` function that we can use inside
* the render function of other components to avoid breaking React rules about side-effects.
*
* Example:
* ```jsx
* if (!Permissions.canUseFreePlan(this.props.betas)) {
* return <Navigator.DismissModal />;
* }
* ```
*/
class DismissModal extends React.Component {
componentDidMount() {
dismissModal(this.props.shouldOpenDrawer);
}

render() {
return null;
}
}

DismissModal.propTypes = {
shouldOpenDrawer: PropTypes.bool,
};
DismissModal.defaultProps = {
shouldOpenDrawer: false,
};

export default {
navigate,
dismissModal,
goBack,
DismissModal,
};
3 changes: 1 addition & 2 deletions src/pages/BusinessBankAccount/NewPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ class BusinessBankAccountNewPage extends React.Component {
render() {
if (!Permissions.canUseFreePlan(this.props.betas)) {
console.debug('Not showing new bank account page because user is not on free plan beta');
Navigation.dismissModal();
return null;
return <Navigation.DismissModal />;
}

return (
Expand Down
3 changes: 1 addition & 2 deletions src/pages/workspace/NewWorkspacePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ class NewWorkspacePage extends React.Component {

if (!Permissions.canUseFreePlan(this.props.betas)) {
console.debug('Not showing new workspace page because user is not on free plan beta');
Navigation.dismissModal();
return null;
return <Navigation.DismissModal />;
}

return (
Expand Down

0 comments on commit 5c1003f

Please sign in to comment.