import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';

// Services
import { deleteDashboard } from 'services/dashboard';
import { toastr } from 'react-redux-toastr';

class DeleteAction extends PureComponent {
  static propTypes = {
    data: PropTypes.object.isRequired,
    authorization: PropTypes.string.isRequired,
    onRowDelete: PropTypes.func.isRequired
  }

  handleOnClickDelete = (e) => {
    if (e) {
      e.preventDefault();
      e.stopPropagation();
    }

    const {
      data: { name, id },
      authorization
    } = this.props;

    toastr.confirm(`Are you sure that you want to delete: "${name}"`, {
      onOk: () => {
        deleteDashboard(id, authorization)
          .then(() => {
            this.props.onRowDelete(id);
            toastr.success('Success', `The dashboard "${id}" - "${name}" has been removed correctly`);
          })
          .catch((err) => {
            toastr.error('Error', `The dashboard "${id}" - "${name}" was not deleted. Try again. ${err}`);
          });
      }
    });
  }

  render() {
    return (
      <span>
        <a className="c-btn" href="#delete-dataset" onClick={this.handleOnClickDelete}> Remove </a>
      </span>
    );
  }
}

export default DeleteAction;

@@ -76,16 +76,65 @@
 ypes';%0A%0A
+// Redux%0Aimport %7B connect %7D from 'react-redux';%0A%0A
 // Servi
@@ -137,16 +137,16 @@
 ervices%0A
-
 import %7B
@@ -346,29 +346,27 @@
 ed,%0A    
-authorization
+onRowDelete
 : PropTy
@@ -373,14 +373,12 @@
 pes.
-string
+func
 .isR
@@ -386,35 +386,28 @@
 quired,%0A    
-onRowDelete
+user
 : PropTypes.
@@ -398,36 +398,38 @@
 user: PropTypes.
-func
+object
 .isRequired%0A  %7D%0A
@@ -579,29 +579,31 @@
 ,%0A      
-authorization
+user: %7B token %7D
 %0A    %7D =
@@ -740,20 +740,12 @@
 id, 
-authorizatio
+toke
 n)%0A 
@@ -1264,23 +1264,112 @@
 %0A%7D%0A%0A
-export default 
+const mapStateToProps = state =%3E (%7B user: state.user %7D);%0A%0Aexport default connect(mapStateToProps, null)(
 Dele
@@ -1376,10 +1376,11 @@
 teAction
+)
 ;%0A

fix delete action for dashboards table in the back office