import React from "react"; import { connect } from "react-redux"; import type { AppState } from "@appsmith/reducers"; import { Keys } from "@blueprintjs/core"; import { showActionConfirmationModal, cancelActionConfirmationModal, acceptActionConfirmationModal, } from "actions/pluginActionActions"; import { Button, Modal, ModalBody, ModalContent, ModalFooter, ModalHeader, } from "design-system"; import { createMessage, QUERY_CONFIRMATION_MODAL_MESSAGE, } from "@appsmith/constants/messages"; import type { ModalInfo } from "reducers/uiReducers/modalActionReducer"; type Props = { modals: ModalInfo[]; dispatch: any; }; class RequestConfirmationModal extends React.Component { addEventListener = () => { document.addEventListener("keydown", this.onKeyUp); }; removeEventListener = () => { document.removeEventListener("keydown", this.onKeyUp); }; onKeyUp = (event: KeyboardEvent) => { // Sometimes calling the shortcut keys "Cmd + Enter" also triggers the onConfirm function below // so We check if no multiple keys are being pressed currently before executing this block of code. if (!(event.metaKey || event.ctrlKey) && event.keyCode === Keys.ENTER) { // please note: due to the way the state is being updated, the last action will always correspond to the right Action Modal. // this is not a bug. this.onConfirm(this.props.modals[this.props.modals.length - 1]); } }; onConfirm = (modalInfo: ModalInfo) => { const { dispatch } = this.props; dispatch(acceptActionConfirmationModal(modalInfo.name)); this.handleClose(modalInfo); }; handleClose = (modalInfo: ModalInfo) => { const { dispatch } = this.props; dispatch(showActionConfirmationModal({ ...modalInfo, modalOpen: false })); dispatch(cancelActionConfirmationModal(modalInfo.name)); }; componentDidUpdate() { const { modals } = this.props; if (!!modals) { this.addEventListener(); } else { this.removeEventListener(); } } render() { const { dispatch, modals } = this.props; // making sure that only modals that are set to be open are eventually opened. // basically filters out modals that have already been opened and prevents it from flashing after other modals have been confirmed. const modalsToBeOpened = modals.filter((modal) => modal.modalOpen); return ( <> {modalsToBeOpened.map((modalInfo: ModalInfo) => ( this.handleClose(modalInfo)} open={modalInfo?.modalOpen} > Confirmation dialog {createMessage(QUERY_CONFIRMATION_MODAL_MESSAGE)}{" "} {modalInfo.name} ? ))} ); } } const mapStateToProps = (state: AppState) => ({ modals: state.ui.modalAction.modals, }); export default connect(mapStateToProps)(RequestConfirmationModal);