<%#
 Copyright 2013-2021 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
import React, { useEffect } from 'react';
import { connect } from 'react-redux';
import { RouteComponentProps } from 'react-router-dom';
import { Modal, ModalHeader, ModalBody, ModalFooter, Button } from 'reactstrap';
import { Translate } from 'react-jhipster';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';

import { IRootState } from 'app/shared/reducers';
import { getEntity, deleteEntity } from './<%= entityFileName %>.reducer';

export interface I<%= entityReactName %>DeleteDialogProps extends StateProps, DispatchProps, RouteComponentProps<{id: string}>  {}

export const <%= entityReactName %>DeleteDialog = (props: I<%= entityReactName %>DeleteDialogProps) => {
  useEffect(() => {
    props.getEntity(props.match.params.id);
  }, []);

  const handleClose = () => {
    props.history.push('/<%= entityFileName %>'<%_ if (pagination === 'pagination') { _%> + props.location.search<%_ } _%>);
  };

  useEffect(() => {
    if (props.updateSuccess) {
      handleClose();
    }
  }, [props.updateSuccess]);

  const confirmDelete = () => {
    props.deleteEntity(props.<%= entityInstance %>Entity.<%= primaryKey.name %>);
  }

  const { <%= entityInstance %>Entity } = props;
  return (
    <Modal isOpen toggle={handleClose}>
    <ModalHeader toggle={handleClose} data-cy="<%= entityInstance %>DeleteDialogHeading"><Translate contentKey="entity.delete.title">Confirm delete operation</Translate></ModalHeader>
    <ModalBody id="<%= i18nKeyPrefix %>.delete.question">
      <Translate contentKey="<%= i18nKeyPrefix %>.delete.question" interpolate={{ id: <%= entityInstance %>Entity.<%= primaryKey.name %> }}>
          Are you sure you want to delete this <%= entityClass %>?
      </Translate>
    </ModalBody>
    <ModalFooter>
      <Button color="secondary" onClick={handleClose}>
        <FontAwesomeIcon icon="ban" />&nbsp;
        <Translate contentKey="entity.action.cancel">Cancel</Translate>
      </Button>
      <Button id="<%= jhiPrefixDashed %>-confirm-delete-<%= entityInstance %>" data-cy="entityConfirmDeleteButton" color="danger" onClick={confirmDelete}>
        <FontAwesomeIcon icon="trash" />&nbsp;
        <Translate contentKey="entity.action.delete">Delete</Translate>
      </Button>
    </ModalFooter>
  </Modal>
  );
};

const mapStateToProps = ({ <%= entityInstance %> }: IRootState) => ({
  <%= entityInstance %>Entity: <%= entityInstance %>.entity,
  updateSuccess: <%= entityInstance %>.updateSuccess
});

const mapDispatchToProps = { getEntity, deleteEntity };

type StateProps = ReturnType<typeof mapStateToProps>;
type DispatchProps = typeof mapDispatchToProps;

export default connect(mapStateToProps, mapDispatchToProps)(<%= entityReactName %>DeleteDialog);
