github.com/minio/console@v1.4.1/web-app/src/screens/Console/Groups/DeleteGroup.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 { ConfirmDeleteIcon } from "mds";
    19  import { encodeURLString } from "../../../common/utils";
    20  import { setErrorSnackMessage } from "../../../systemSlice";
    21  import { useAppDispatch } from "../../../store";
    22  import ConfirmDialog from "../Common/ModalWrapper/ConfirmDialog";
    23  import { api } from "api";
    24  import { errorToHandler } from "api/errors";
    25  import { ApiError, HttpResponse } from "api/consoleApi";
    26  
    27  interface IDeleteGroup {
    28    selectedGroups: string[];
    29    deleteOpen: boolean;
    30    closeDeleteModalAndRefresh: any;
    31  }
    32  
    33  const DeleteGroup = ({
    34    selectedGroups,
    35    deleteOpen,
    36    closeDeleteModalAndRefresh,
    37  }: IDeleteGroup) => {
    38    const dispatch = useAppDispatch();
    39    const onClose = () => closeDeleteModalAndRefresh(false);
    40    const [loadingDelete, setLoadingDelete] = useState<boolean>(false);
    41  
    42    if (!selectedGroups) {
    43      return null;
    44    }
    45    const onDeleteGroups = () => {
    46      for (let group of selectedGroups) {
    47        setLoadingDelete(true);
    48        api.group
    49          .removeGroup(encodeURLString(group))
    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  
    62    const renderGroups = selectedGroups.map((group) => (
    63      <div key={group}>
    64        <b>{group}</b>
    65      </div>
    66    ));
    67  
    68    return (
    69      <ConfirmDialog
    70        title={`Delete Group${selectedGroups.length > 1 ? "s" : ""}`}
    71        confirmText={"Delete"}
    72        isOpen={deleteOpen}
    73        titleIcon={<ConfirmDeleteIcon />}
    74        isLoading={loadingDelete}
    75        onConfirm={onDeleteGroups}
    76        onClose={onClose}
    77        confirmationContent={
    78          <Fragment>
    79            Are you sure you want to delete the following{" "}
    80            {selectedGroups.length === 1 ? "" : selectedGroups.length} group
    81            {selectedGroups.length > 1 ? "s?" : "?"}
    82            {renderGroups}
    83          </Fragment>
    84        }
    85      />
    86    );
    87  };
    88  
    89  export default DeleteGroup;