github.com/minio/console@v1.4.1/web-app/src/screens/Console/Policies/DeletePolicy.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 { setErrorSnackMessage } from "../../../systemSlice";
    21  import { useAppDispatch } from "../../../store";
    22  import { api } from "api";
    23  import { ApiError, HttpResponse } from "api/consoleApi";
    24  import { errorToHandler } from "api/errors";
    25  import { encodeURLString } from "common/utils";
    26  
    27  interface IDeletePolicyProps {
    28    closeDeleteModalAndRefresh: (refresh: boolean) => void;
    29    deleteOpen: boolean;
    30    selectedPolicy: string;
    31  }
    32  
    33  const DeletePolicy = ({
    34    closeDeleteModalAndRefresh,
    35    deleteOpen,
    36    selectedPolicy,
    37  }: IDeletePolicyProps) => {
    38    const dispatch = useAppDispatch();
    39    const onClose = () => closeDeleteModalAndRefresh(false);
    40  
    41    const [loadingDelete, setLoadingDelete] = useState<boolean>(false);
    42  
    43    if (!selectedPolicy) {
    44      return null;
    45    }
    46  
    47    const onConfirmDelete = () => {
    48      setLoadingDelete(true);
    49      api.policy
    50        .removePolicy(encodeURLString(selectedPolicy))
    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 Policy`}
    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 policy <br />
    74            <b>{selectedPolicy}</b>?
    75          </Fragment>
    76        }
    77      />
    78    );
    79  };
    80  
    81  export default DeletePolicy;