<%#
 Copyright 2013-2024 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, useState } from 'react';
import { useLocation, useNavigate, useParams } from 'react-router-dom';
import { Modal, ModalHeader, ModalBody, ModalFooter, Button } from 'reactstrap';
import { Translate } from 'react-jhipster';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';

import { useAppDispatch, useAppSelector } from 'app/config/store';
import { getEntity, deleteEntity } from './<%= entityFileName %>.reducer';

export const <%= entityReactName %>DeleteDialog = () => {
  const dispatch = useAppDispatch();

  const pageLocation = useLocation();
  const navigate = useNavigate();
  const { id } = useParams<'id'>();

  const [loadModal, setLoadModal] = useState(false);

  useEffect(() => {
    dispatch(getEntity(id));
    setLoadModal(true);
  }, []);

  const <%= entityInstance %>Entity = useAppSelector(state => state.<%= entityReactState %>.entity);
  const updateSuccess = useAppSelector(state => state.<%= entityReactState %>.updateSuccess);

  const handleClose = () => {
    navigate('/<%= entityPage %>'<%_ if (paginationPagination) { _%> + pageLocation.search<%_ } _%>);
  };

  useEffect(() => {
    if (updateSuccess && loadModal) {
      handleClose();
      setLoadModal(false);
    }
  }, [updateSuccess]);

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

  return (
    <a-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>
  </a-modal>
  );
};

export default <%= entityReactName %>DeleteDialog;
