2020-08-27 15:39:16 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import { connect } from "react-redux";
|
2022-08-24 12:16:32 +00:00
|
|
|
import { AppState } from "@appsmith/reducers";
|
2021-12-15 11:51:57 +00:00
|
|
|
import { Keys } from "@blueprintjs/core";
|
2020-08-27 15:39:16 +00:00
|
|
|
import {
|
2022-02-18 06:58:36 +00:00
|
|
|
showActionConfirmationModal,
|
|
|
|
|
cancelActionConfirmationModal,
|
|
|
|
|
acceptActionConfirmationModal,
|
2021-08-27 09:25:28 +00:00
|
|
|
} from "actions/pluginActionActions";
|
2021-11-01 04:54:06 +00:00
|
|
|
import DialogComponent from "components/ads/DialogComponent";
|
|
|
|
|
import styled from "styled-components";
|
2022-08-22 05:09:39 +00:00
|
|
|
import { Button, Category, Size } from "design-system";
|
2021-11-01 04:54:06 +00:00
|
|
|
import {
|
|
|
|
|
createMessage,
|
|
|
|
|
QUERY_CONFIRMATION_MODAL_MESSAGE,
|
2022-02-11 18:08:46 +00:00
|
|
|
} from "@appsmith/constants/messages";
|
2022-02-28 17:37:21 +00:00
|
|
|
import { ModalInfo } from "reducers/uiReducers/modalActionReducer";
|
2020-08-27 15:39:16 +00:00
|
|
|
|
|
|
|
|
type Props = {
|
2022-02-28 17:37:21 +00:00
|
|
|
modals: ModalInfo[];
|
2020-08-27 15:39:16 +00:00
|
|
|
dispatch: any;
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-01 04:54:06 +00:00
|
|
|
const ModalBody = styled.div`
|
|
|
|
|
padding-bottom: 20px;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const ModalFooter = styled.div`
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: flex-end;
|
|
|
|
|
|
|
|
|
|
button {
|
|
|
|
|
margin-left: 12px;
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2022-02-18 06:58:36 +00:00
|
|
|
class RequestConfirmationModal extends React.Component<Props> {
|
2021-12-15 11:51:57 +00:00
|
|
|
addEventListener = () => {
|
|
|
|
|
document.addEventListener("keydown", this.onKeyUp);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
removeEventListener = () => {
|
|
|
|
|
document.removeEventListener("keydown", this.onKeyUp);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onKeyUp = (event: KeyboardEvent) => {
|
2022-03-08 13:55:13 +00:00
|
|
|
// 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) {
|
2022-02-28 17:37:21 +00:00
|
|
|
// 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]);
|
2021-12-15 11:51:57 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-28 17:37:21 +00:00
|
|
|
onConfirm = (modalInfo: ModalInfo) => {
|
2021-12-15 11:51:57 +00:00
|
|
|
const { dispatch } = this.props;
|
2022-02-28 17:37:21 +00:00
|
|
|
dispatch(acceptActionConfirmationModal(modalInfo.name));
|
|
|
|
|
this.handleClose(modalInfo);
|
2021-12-15 11:51:57 +00:00
|
|
|
};
|
|
|
|
|
|
2022-02-28 17:37:21 +00:00
|
|
|
handleClose = (modalInfo: ModalInfo) => {
|
2021-12-15 11:51:57 +00:00
|
|
|
const { dispatch } = this.props;
|
2022-02-28 17:37:21 +00:00
|
|
|
dispatch(showActionConfirmationModal({ ...modalInfo, modalOpen: false }));
|
|
|
|
|
dispatch(cancelActionConfirmationModal(modalInfo.name));
|
2021-12-15 11:51:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
2022-02-28 17:37:21 +00:00
|
|
|
const { modals } = this.props;
|
|
|
|
|
if (!!modals) {
|
2021-12-15 11:51:57 +00:00
|
|
|
this.addEventListener();
|
|
|
|
|
} else {
|
|
|
|
|
this.removeEventListener();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-27 15:39:16 +00:00
|
|
|
render() {
|
2022-02-28 17:37:21 +00:00
|
|
|
const { dispatch, modals } = this.props;
|
2020-08-27 15:39:16 +00:00
|
|
|
|
2022-03-25 08:22:23 +00:00
|
|
|
// 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 === true);
|
|
|
|
|
|
2020-08-27 15:39:16 +00:00
|
|
|
return (
|
2022-02-28 17:37:21 +00:00
|
|
|
<>
|
2022-03-25 08:22:23 +00:00
|
|
|
{modalsToBeOpened.map((modalInfo: ModalInfo, index: number) => (
|
2022-02-28 17:37:21 +00:00
|
|
|
<DialogComponent
|
|
|
|
|
canEscapeKeyClose
|
2022-05-10 12:39:03 +00:00
|
|
|
canOutsideClickClose
|
2022-02-28 17:37:21 +00:00
|
|
|
isOpen={modalInfo?.modalOpen}
|
|
|
|
|
key={index}
|
|
|
|
|
maxHeight={"80vh"}
|
2022-03-16 14:26:19 +00:00
|
|
|
noModalBodyMarginTop
|
2022-02-28 17:37:21 +00:00
|
|
|
onClose={() => this.handleClose(modalInfo)}
|
2022-03-16 14:26:19 +00:00
|
|
|
title="Confirmation Dialog"
|
2022-02-28 17:37:21 +00:00
|
|
|
width={"580px"}
|
|
|
|
|
>
|
|
|
|
|
<ModalBody>
|
2022-03-16 14:26:19 +00:00
|
|
|
{createMessage(QUERY_CONFIRMATION_MODAL_MESSAGE)}{" "}
|
|
|
|
|
<b>{modalInfo.name}</b> ?
|
2022-02-28 17:37:21 +00:00
|
|
|
</ModalBody>
|
|
|
|
|
<ModalFooter>
|
|
|
|
|
<Button
|
|
|
|
|
category={Category.tertiary}
|
|
|
|
|
cypressSelector="t--cancel-modal-btn"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
dispatch(cancelActionConfirmationModal(modalInfo.name));
|
|
|
|
|
this.handleClose(modalInfo);
|
|
|
|
|
}}
|
2022-03-16 14:26:19 +00:00
|
|
|
size={Size.large}
|
2022-02-28 17:37:21 +00:00
|
|
|
tag="button"
|
2022-03-16 14:26:19 +00:00
|
|
|
text="No"
|
2022-02-28 17:37:21 +00:00
|
|
|
type="button"
|
|
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
category={Category.primary}
|
|
|
|
|
cypressSelector="t--confirm-modal-btn"
|
|
|
|
|
onClick={() => this.onConfirm(modalInfo)}
|
2022-03-16 14:26:19 +00:00
|
|
|
size={Size.large}
|
2022-02-28 17:37:21 +00:00
|
|
|
tag="button"
|
2022-03-16 14:26:19 +00:00
|
|
|
text="Yes"
|
2022-02-28 17:37:21 +00:00
|
|
|
type="button"
|
|
|
|
|
/>
|
|
|
|
|
</ModalFooter>
|
|
|
|
|
</DialogComponent>
|
|
|
|
|
))}
|
|
|
|
|
</>
|
2020-08-27 15:39:16 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = (state: AppState) => ({
|
2022-02-28 17:37:21 +00:00
|
|
|
modals: state.ui.modalAction.modals,
|
2020-08-27 15:39:16 +00:00
|
|
|
});
|
|
|
|
|
|
2022-02-18 06:58:36 +00:00
|
|
|
export default connect(mapStateToProps)(RequestConfirmationModal);
|