"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const react_redux_1 = require("react-redux");
const react_core_1 = require("@patternfly/react-core");
const api_1 = require("../../../common/api");
const slice_1 = require("../../store/slices/current-cluster/slice");
const ModalDialogsContext_1 = require("../hosts/ModalDialogsContext");
const common_1 = require("../../../common");
const apis_1 = require("../../services/apis");
const CancelInstallationModal = () => {
    const dispatch = (0, react_redux_1.useDispatch)();
    const [isSubmitting, setIsSubmitting] = react_1.default.useState(false);
    const [error, setError] = react_1.default.useState(null);
    const { cancelInstallationDialog } = (0, ModalDialogsContext_1.useModalDialogsContext)();
    const { data, isOpen, close: onClose } = cancelInstallationDialog;
    const clusterId = data === null || data === void 0 ? void 0 : data.clusterId;
    if (!clusterId) {
        return null;
    }
    const handleSubmit = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        setIsSubmitting(true);
        try {
            setError(null);
            const { data } = yield apis_1.ClustersAPI.cancel(clusterId);
            dispatch((0, slice_1.updateCluster)(data));
            onClose();
        }
        catch (e) {
            (0, api_1.handleApiError)(e, () => {
                setError({ title: 'Failed to abort cluster installation', message: (0, api_1.getApiErrorMessage)(e) });
            });
        }
        setIsSubmitting(false);
    });
    const getModalContent = () => {
        if (isSubmitting) {
            return react_1.default.createElement(common_1.LoadingState, { content: "Aborting cluster installation..." });
        }
        if (error) {
            return react_1.default.createElement(common_1.ErrorState, { title: error.title, content: error.message });
        }
        return (react_1.default.createElement(react_core_1.Content, { component: "p" }, "This will abort cluster installation. Are you sure you want to proceed?"));
    };
    return (react_1.default.createElement(react_core_1.Modal, { isOpen: isOpen, variant: react_core_1.ModalVariant.small, onClose: onClose },
        react_1.default.createElement(react_core_1.ModalHeader, { title: "Abort Cluster Installation" }),
        react_1.default.createElement(react_core_1.ModalBody, null, getModalContent()),
        react_1.default.createElement(react_core_1.ModalFooter, null,
            react_1.default.createElement(react_core_1.Button, { key: "submit", variant: react_core_1.ButtonVariant.danger, onClick: () => {
                    void handleSubmit();
                }, isDisabled: isSubmitting }, "Abort installation"),
            react_1.default.createElement(react_core_1.Button, { key: "cancel", variant: react_core_1.ButtonVariant.link, onClick: onClose, isDisabled: isSubmitting }, "Cancel"))));
};
exports.default = CancelInstallationModal;
//# sourceMappingURL=CancelInstallationModal.js.map