/**
 * @function 节流按钮
 * @author Lyq
 */
import React, { Component } from 'react';
// import st from 'styled-components';
import {
  Button
} from 'antd';

export default class OnceButton extends Component {
  constructor(props, context) {
    super(props, context);
    this.state = {
      disabled: false,
      time: 0
    }
  }
  componentWillMount() {

  }
  componentWillReceiveProps(props) {

  }

  componentDidMount() {

  }


  render() {
    const { onClick } = this.props;
    Reflect.deleteProperty(this.props, 'onClick');
    Reflect.deleteProperty(this.props, 'disabled');
    return (
      <Button
        {...this.props}
        disabled={this.state.disabled}
        onClick={(e) => {
          const getType = obj => Object.prototype.toString.call(obj).slice(8, -1);
          // const now = new Date().getTime();
          if (!this.state.disabled) {
            this.setState({ disabled: true });
            if (getType(onClick) == 'Promise') {
              this.props.onClick(e).finally(() => {
                this.setState({ disabled: false });
              });
            } else if (getType(onClick) == 'Function') {
              this.setState({
                disabled: true
              });
              this.props.onClick(e);
              setTimeout(() => {
                this.setState({
                  disabled: false
                });
              }, 1000);
              // const lastTime = this.state.time;
              // if (now - lastTime > 1000) {
              //   this.setState({
              //     time: now
              //   });
              //   this.props.onClick(e);
              // }
            }
          }
        }}>{this.props.children}</Button>
    );
  }
}

