"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

var _react = _interopRequireWildcard(require("react"));

var _reactDom = require("react-dom");

var _button = _interopRequireDefault(require("../button"));

var ActionButton = /*#__PURE__*/function (_Component) {
  (0, _inherits2["default"])(ActionButton, _Component);

  var _super = (0, _createSuper2["default"])(ActionButton);

  function ActionButton(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, ActionButton);
    _this = _super.call(this, props);

    _this.onClick = function (props) {
      var actionFn = props.actionFn,
          closeModal = props.closeModal;

      if (actionFn) {
        var ret;

        if (actionFn.length) {
          ret = actionFn(closeModal);
        } else {
          ret = actionFn();

          if (!ret) {
            closeModal();
          }
        }

        if (ret && ret.then) {
          _this.setState({
            loading: true
          });

          ret.then(function () {
            // It's unnecessary to set loading=false, for the Modal will be unmounted after close.
            // this.setState({ loading: false });
            closeModal.apply(void 0, arguments);
          }, function () {
            _this.setState({
              loading: false
            });
          });
        }
      } else {
        closeModal();
      }
    };

    _this.state = {
      loading: false
    };
    return _this;
  }

  (0, _createClass2["default"])(ActionButton, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var autoFocus = this.props.autoFocus;

      if (autoFocus) {
        var $this = (0, _reactDom.findDOMNode)(this);
        this.timeoutId = window.setTimeout(function () {
          return $this.focus();
        });
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      clearTimeout(this.timeoutId);
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          okProps = _this$props.okProps,
          cancelProps = _this$props.cancelProps;
      var loading = this.state.loading;

      var cancelButton = cancelProps && /*#__PURE__*/_react["default"].createElement(_button["default"], (0, _extends2["default"])({}, cancelProps.buttonProps, {
        type: cancelProps.type,
        disabled: loading,
        onClick: function onClick() {
          _this2.onClick(cancelProps);
        }
      }), cancelProps.text);

      return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, cancelButton, /*#__PURE__*/_react["default"].createElement(_button["default"], (0, _extends2["default"])({}, okProps.buttonProps, {
        loading: loading,
        type: okProps.type,
        onClick: function onClick() {
          _this2.onClick(okProps);
        }
      }), okProps.text));
    }
  }]);
  return ActionButton;
}(_react.Component);

exports["default"] = ActionButton;
//# sourceMappingURL=ActionButton.js.map
