jiti-meet/react/features/base/dialog/components/AbstractDialog.js

152 lines
4.3 KiB
JavaScript
Raw Normal View History

2017-09-18 07:01:14 +00:00
import PropTypes from 'prop-types';
import { Component } from 'react';
2017-03-07 03:34:51 +00:00
import { hideDialog } from '../actions';
2017-06-07 16:45:04 +00:00
import { DIALOG_PROP_TYPES } from '../constants';
2017-03-07 03:34:51 +00:00
/**
2017-09-18 07:01:14 +00:00
* An abstract implementation of a dialog on Web/React and mobile/react-native.
2017-03-07 03:34:51 +00:00
*/
export default class AbstractDialog extends Component {
/**
* {@code AbstractDialog} React {@code Component}'s prop types.
2017-03-07 03:34:51 +00:00
*
* @static
*/
static propTypes = {
2017-06-07 16:45:04 +00:00
...DIALOG_PROP_TYPES,
2017-03-07 03:34:51 +00:00
2017-09-18 07:01:14 +00:00
/**
* The React {@code Component} children of {@code AbstractDialog}
2017-09-18 07:01:14 +00:00
* which represents the dialog's body.
*/
children: PropTypes.node,
2017-03-07 03:34:51 +00:00
/**
* Used to show/hide the dialog on cancel.
2017-03-07 03:34:51 +00:00
*/
2017-09-18 07:01:14 +00:00
dispatch: PropTypes.func
};
2017-03-07 03:34:51 +00:00
/**
* Initializes a new {@code AbstractDialog} instance.
2017-03-07 03:34:51 +00:00
*
* @param {Object} props - The read-only React {@code Component} props with
2017-09-18 07:01:14 +00:00
* which the new instance is to be initialized.
2017-03-07 03:34:51 +00:00
*/
constructor(props) {
super(props);
this.state = {
};
2017-03-07 03:34:51 +00:00
this._onCancel = this._onCancel.bind(this);
this._onSubmit = this._onSubmit.bind(this);
this._onSubmitFulfilled = this._onSubmitFulfilled.bind(this);
this._onSubmitRejected = this._onSubmitRejected.bind(this);
}
/**
* Implements React's {@link Component#componentWillMount()}. Invoked
* immediately before mounting occurs.
*
* @inheritdoc
*/
componentWillMount() {
this._mounted = true;
}
/**
* Implements React's {@link Component#componentWillUnmount()}. Invoked
* immediately before this component is unmounted and destroyed.
*
* @inheritdoc
*/
componentWillUnmount() {
this._mounted = false;
2017-03-07 03:34:51 +00:00
}
/**
2017-09-18 07:01:14 +00:00
* Dispatches a redux action to hide this dialog when it's canceled.
2017-03-07 03:34:51 +00:00
*
2017-09-18 07:01:14 +00:00
* @protected
2017-03-07 03:34:51 +00:00
* @returns {void}
*/
_onCancel() {
const { cancelDisabled, onCancel } = this.props;
2017-03-07 03:34:51 +00:00
if ((typeof cancelDisabled === 'undefined' || !cancelDisabled)
&& (!onCancel || onCancel())) {
2017-03-07 03:34:51 +00:00
this.props.dispatch(hideDialog());
}
}
/**
* Submits this dialog. If the React {@code Component} prop
* {@code onSubmit} is defined, the function that is the value of the prop
* is invoked. If the function returns a {@code thenable}, then the
* resolution of the {@code thenable} is awaited. If the submission
* completes successfully, a redux action will be dispatched to hide this
* dialog.
2017-03-07 03:34:51 +00:00
*
* @private
* @param {string} value - The submitted value if any.
* @returns {void}
*/
_onSubmit(value) {
const { okDisabled, onSubmit } = this.props;
2017-03-07 03:34:51 +00:00
if (typeof okDisabled === 'undefined' || !okDisabled) {
this.setState({ submitting: true });
// Invoke the React Compnent prop onSubmit if any.
const r = !onSubmit || onSubmit(value);
// If the invocation returns a thenable, await its resolution;
// otherwise, treat the return value as a boolean indicating whether
// the submission has completed successfully.
let then;
if (r) {
switch (typeof r) {
case 'function':
case 'object':
then = r.then;
break;
}
}
if (typeof then === 'function' && then.length === 2) {
then.call(r, this._onSubmitFulfilled, this._onSubmitRejected);
} else if (r) {
this._onSubmitFulfilled();
} else {
this._onSubmitRejected();
}
2017-03-07 03:34:51 +00:00
}
}
/**
* Notifies this {@code AbstractDialog} that it has been submitted
* successfully. Dispatches a redux action to hide this dialog after it has
* been submitted.
*
* @private
* @returns {void}
*/
_onSubmitFulfilled() {
this._mounted && this.setState({ submitting: false });
this.props.dispatch(hideDialog());
}
/**
* Notifies this {@code AbstractDialog} that its submission has failed.
*
* @private
* @returns {void}
*/
_onSubmitRejected() {
this._mounted && this.setState({ submitting: false });
}
2017-03-07 03:34:51 +00:00
}