2017-10-04 22:36:09 +00:00
|
|
|
// @flow
|
|
|
|
|
2017-11-17 19:06:47 +00:00
|
|
|
import { Component } from 'react';
|
2017-03-07 03:34:51 +00:00
|
|
|
|
|
|
|
import { hideDialog } from '../actions';
|
2017-11-17 19:06:47 +00:00
|
|
|
import type { DialogProps } from '../constants';
|
2017-03-07 03:34:51 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-17 19:06:47 +00:00
|
|
|
* The type of the React {@code Component} props of {@link AbstractDialog}.
|
2017-03-07 03:34:51 +00:00
|
|
|
*/
|
2017-11-17 19:06:47 +00:00
|
|
|
export type Props = {
|
|
|
|
...DialogProps,
|
2017-03-07 03:34:51 +00:00
|
|
|
|
2017-11-13 15:54:04 +00:00
|
|
|
/**
|
|
|
|
* Used to show/hide the dialog on cancel.
|
|
|
|
*/
|
|
|
|
dispatch: Dispatch<*>
|
2017-11-17 19:06:47 +00:00
|
|
|
};
|
2017-09-18 07:01:14 +00:00
|
|
|
|
2017-11-13 15:54:04 +00:00
|
|
|
/**
|
2017-11-17 19:06:47 +00:00
|
|
|
* The type of the React {@code Component} state of {@link AbstractDialog}.
|
2017-11-13 15:54:04 +00:00
|
|
|
*/
|
2017-11-17 19:06:47 +00:00
|
|
|
export type State = {
|
|
|
|
submitting: ?boolean
|
|
|
|
};
|
2017-03-07 03:34:51 +00:00
|
|
|
|
2017-11-13 15:54:04 +00:00
|
|
|
/**
|
|
|
|
* An abstract implementation of a dialog on Web/React and mobile/react-native.
|
|
|
|
*/
|
2017-11-17 19:06:47 +00:00
|
|
|
export default class AbstractDialog<P : Props, S : State>
|
|
|
|
extends Component<P, S> {
|
2017-10-04 22:36:09 +00:00
|
|
|
|
2017-11-17 19:06:47 +00:00
|
|
|
_mounted: boolean;
|
2017-10-04 22:36:09 +00:00
|
|
|
|
2017-03-07 03:34:51 +00:00
|
|
|
/**
|
2017-10-01 06:35:19 +00:00
|
|
|
* Initializes a new {@code AbstractDialog} instance.
|
2017-03-07 03:34:51 +00:00
|
|
|
*
|
2017-10-01 06:35:19 +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
|
|
|
*/
|
2017-11-17 19:06:47 +00:00
|
|
|
constructor(props: P) {
|
2017-03-07 03:34:51 +00:00
|
|
|
super(props);
|
|
|
|
|
2017-10-04 22:36:09 +00:00
|
|
|
// Bind event handlers so they are only bound once per instance.
|
2017-03-07 03:34:51 +00:00
|
|
|
this._onCancel = this._onCancel.bind(this);
|
|
|
|
this._onSubmit = this._onSubmit.bind(this);
|
2017-09-22 20:09:15 +00:00
|
|
|
this._onSubmitFulfilled = this._onSubmitFulfilled.bind(this);
|
|
|
|
this._onSubmitRejected = this._onSubmitRejected.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-29 21:40:14 +00:00
|
|
|
* Implements React's {@link Component#componentDidMount()}. Invoked
|
2017-09-22 20:09:15 +00:00
|
|
|
* immediately before mounting occurs.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2018-10-29 21:40:14 +00:00
|
|
|
componentDidMount() {
|
2017-09-22 20:09:15 +00:00
|
|
|
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-10-06 20:15:51 +00:00
|
|
|
/**
|
|
|
|
* Dispatches a redux action to hide this dialog.
|
|
|
|
*
|
|
|
|
* @returns {*} The return value of {@link hideDialog}.
|
|
|
|
*/
|
|
|
|
_hide() {
|
|
|
|
return this.props.dispatch(hideDialog());
|
|
|
|
}
|
|
|
|
|
2017-10-04 22:36:09 +00:00
|
|
|
_onCancel: () => void;
|
|
|
|
|
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() {
|
2018-09-07 17:40:10 +00:00
|
|
|
const { cancelDisabled = false, onCancel } = this.props;
|
2017-03-07 03:34:51 +00:00
|
|
|
|
2018-09-07 17:40:10 +00:00
|
|
|
if (!cancelDisabled && (!onCancel || onCancel())) {
|
2017-10-06 20:15:51 +00:00
|
|
|
this._hide();
|
2017-03-07 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 22:36:09 +00:00
|
|
|
_onSubmit: (?string) => void;
|
|
|
|
|
2017-03-07 03:34:51 +00:00
|
|
|
/**
|
2017-10-04 22:36:09 +00:00
|
|
|
* Submits this {@code Dialog}. If the React {@code Component} prop
|
2017-10-01 06:35:19 +00:00
|
|
|
* {@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
|
2017-09-22 20:09:15 +00:00
|
|
|
* completes successfully, a redux action will be dispatched to hide this
|
|
|
|
* dialog.
|
2017-03-07 03:34:51 +00:00
|
|
|
*
|
2017-10-04 22:36:09 +00:00
|
|
|
* @protected
|
|
|
|
* @param {string} [value] - The submitted value if any.
|
2017-03-07 03:34:51 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-10-04 22:36:09 +00:00
|
|
|
_onSubmit(value: ?string) {
|
2018-09-07 17:40:10 +00:00
|
|
|
const { okDisabled = false, onSubmit } = this.props;
|
2017-03-07 03:34:51 +00:00
|
|
|
|
2018-09-07 17:40:10 +00:00
|
|
|
if (!okDisabled) {
|
2017-09-22 20:09:15 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2017-09-22 20:09:15 +00:00
|
|
|
|
2017-10-04 22:36:09 +00:00
|
|
|
_onSubmitFulfilled: () => void;
|
|
|
|
|
2017-09-22 20:09:15 +00:00
|
|
|
/**
|
2017-10-01 06:35:19 +00:00
|
|
|
* Notifies this {@code AbstractDialog} that it has been submitted
|
2017-09-22 20:09:15 +00:00
|
|
|
* successfully. Dispatches a redux action to hide this dialog after it has
|
|
|
|
* been submitted.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onSubmitFulfilled() {
|
|
|
|
this._mounted && this.setState({ submitting: false });
|
|
|
|
|
2017-10-06 20:15:51 +00:00
|
|
|
this._hide();
|
2017-09-22 20:09:15 +00:00
|
|
|
}
|
|
|
|
|
2017-10-04 22:36:09 +00:00
|
|
|
_onSubmitRejected: () => void;
|
|
|
|
|
2017-09-22 20:09:15 +00:00
|
|
|
/**
|
2017-10-01 06:35:19 +00:00
|
|
|
* Notifies this {@code AbstractDialog} that its submission has failed.
|
2017-09-22 20:09:15 +00:00
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onSubmitRejected() {
|
|
|
|
this._mounted && this.setState({ submitting: false });
|
|
|
|
}
|
2017-03-07 03:34:51 +00:00
|
|
|
}
|