github.com/minio/console@v1.4.1/web-app/src/screens/Console/KMS/DeleteKMSModal.tsx (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2022 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  import React, { useState, Fragment } from "react";
    18  import { ConfirmDeleteIcon, Grid, InformativeMessage, InputBox } from "mds";
    19  import { setErrorSnackMessage } from "../../../systemSlice";
    20  import { useAppDispatch } from "../../../store";
    21  import ConfirmDialog from "../Common/ModalWrapper/ConfirmDialog";
    22  import { api } from "api";
    23  import { errorToHandler } from "api/errors";
    24  import { ApiError, HttpResponse } from "api/consoleApi";
    25  
    26  interface IDeleteKMSModalProps {
    27    closeDeleteModalAndRefresh: (refresh: boolean) => void;
    28    deleteOpen: boolean;
    29    selectedItem: string;
    30  }
    31  
    32  const DeleteKMSModal = ({
    33    closeDeleteModalAndRefresh,
    34    deleteOpen,
    35    selectedItem,
    36  }: IDeleteKMSModalProps) => {
    37    const dispatch = useAppDispatch();
    38    const onClose = () => closeDeleteModalAndRefresh(false);
    39    const [loadingDelete, setLoadingDelete] = useState<boolean>(false);
    40    const [retypeKey, setRetypeKey] = useState("");
    41  
    42    if (!selectedItem) {
    43      return null;
    44    }
    45  
    46    const onConfirmDelete = () => {
    47      setLoadingDelete(true);
    48      api.kms
    49        .kmsDeleteKey(selectedItem)
    50        .then((_) => {
    51          closeDeleteModalAndRefresh(true);
    52        })
    53        .catch(async (res: HttpResponse<void, ApiError>) => {
    54          const err = (await res.json()) as ApiError;
    55          dispatch(setErrorSnackMessage(errorToHandler(err)));
    56          closeDeleteModalAndRefresh(false);
    57        })
    58        .finally(() => setLoadingDelete(false));
    59    };
    60  
    61    return (
    62      <ConfirmDialog
    63        title={`Delete Key`}
    64        confirmText={"Delete"}
    65        isOpen={deleteOpen}
    66        titleIcon={<ConfirmDeleteIcon />}
    67        isLoading={loadingDelete}
    68        onConfirm={onConfirmDelete}
    69        onClose={onClose}
    70        confirmButtonProps={{
    71          disabled: retypeKey !== selectedItem || loadingDelete,
    72        }}
    73        confirmationContent={
    74          <Fragment>
    75            <Grid item xs={12}>
    76              <InformativeMessage
    77                variant={"error"}
    78                title={"WARNING"}
    79                message={
    80                  "Please note that this is a dangerous operation. Once a key has been deleted all data that has been encrypted with it cannot be decrypted anymore, and therefore, is lost."
    81                }
    82                sx={{ margin: "15px 0" }}
    83              />
    84            </Grid>
    85            To continue please type <b>{selectedItem}</b> in the box.
    86            <Grid item xs={12}>
    87              <InputBox
    88                id="retype-key"
    89                name="retype-key"
    90                onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
    91                  setRetypeKey(event.target.value);
    92                }}
    93                onPaste={(e) => e.preventDefault()}
    94                label=""
    95                value={retypeKey}
    96              />
    97            </Grid>
    98          </Fragment>
    99        }
   100      />
   101    );
   102  };
   103  
   104  export default DeleteKMSModal;