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

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2021 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, { Fragment, useState } from "react";
    18  import ConfirmDialog from "../Common/ModalWrapper/ConfirmDialog";
    19  import { ConfirmDeleteIcon } from "mds";
    20  import { encodeURLString } from "../../../common/utils";
    21  import { setErrorSnackMessage } from "../../../systemSlice";
    22  import { useAppDispatch } from "../../../store";
    23  import { api } from "api";
    24  import { ApiError, HttpResponse } from "api/consoleApi";
    25  import { errorToHandler } from "api/errors";
    26  
    27  interface IDeleteServiceAccountProps {
    28    closeDeleteModalAndRefresh: (refresh: boolean) => void;
    29    deleteOpen: boolean;
    30    selectedServiceAccount: string | null;
    31  }
    32  
    33  const DeleteServiceAccount = ({
    34    closeDeleteModalAndRefresh,
    35    deleteOpen,
    36    selectedServiceAccount,
    37  }: IDeleteServiceAccountProps) => {
    38    const dispatch = useAppDispatch();
    39    const onClose = () => closeDeleteModalAndRefresh(false);
    40  
    41    const [loadingDelete, setLoadingDelete] = useState<boolean>(false);
    42  
    43    if (!selectedServiceAccount) {
    44      return null;
    45    }
    46  
    47    const onConfirmDelete = () => {
    48      setLoadingDelete(true);
    49      api.serviceAccounts
    50        .deleteServiceAccount(encodeURLString(selectedServiceAccount))
    51        .then((_) => {
    52          closeDeleteModalAndRefresh(true);
    53        })
    54        .catch(async (res: HttpResponse<void, ApiError>) => {
    55          const err = (await res.json()) as ApiError;
    56          dispatch(setErrorSnackMessage(errorToHandler(err)));
    57          closeDeleteModalAndRefresh(false);
    58        })
    59        .finally(() => setLoadingDelete(false));
    60    };
    61  
    62    return (
    63      <ConfirmDialog
    64        title={`Delete Access Key`}
    65        confirmText={"Delete"}
    66        isOpen={deleteOpen}
    67        titleIcon={<ConfirmDeleteIcon />}
    68        isLoading={loadingDelete}
    69        onConfirm={onConfirmDelete}
    70        onClose={onClose}
    71        confirmationContent={
    72          <Fragment>
    73            Are you sure you want to delete Access Key{" "}
    74            <b
    75              style={{
    76                maxWidth: "200px",
    77                whiteSpace: "normal",
    78                wordWrap: "break-word",
    79              }}
    80            >
    81              {selectedServiceAccount}
    82            </b>
    83            ?
    84          </Fragment>
    85        }
    86      />
    87    );
    88  };
    89  
    90  export default DeleteServiceAccount;