github.com/minio/console@v1.4.1/web-app/src/screens/Console/Buckets/BucketDetails/DeleteReplicationRule.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, Grid, InputBox } from "mds";
    19  import { ErrorResponseHandler } from "../../../../common/types";
    20  import { setErrorSnackMessage } from "../../../../systemSlice";
    21  import { useAppDispatch } from "../../../../store";
    22  import useApi from "../../Common/Hooks/useApi";
    23  import ConfirmDialog from "../../Common/ModalWrapper/ConfirmDialog";
    24  
    25  interface IDeleteReplicationProps {
    26    closeDeleteModalAndRefresh: (refresh: boolean) => void;
    27    deleteOpen: boolean;
    28    selectedBucket: string;
    29    ruleToDelete?: string;
    30    rulesToDelete?: string[];
    31    remainingRules: number;
    32    allSelected: boolean;
    33    deleteSelectedRules?: boolean;
    34  }
    35  
    36  const DeleteReplicationRule = ({
    37    closeDeleteModalAndRefresh,
    38    deleteOpen,
    39    selectedBucket,
    40    ruleToDelete,
    41    rulesToDelete,
    42    remainingRules,
    43    allSelected,
    44    deleteSelectedRules = false,
    45  }: IDeleteReplicationProps) => {
    46    const dispatch = useAppDispatch();
    47    const [confirmationText, setConfirmationText] = useState<string>("");
    48  
    49    const onDelSuccess = () => closeDeleteModalAndRefresh(true);
    50    const onDelError = (err: ErrorResponseHandler) =>
    51      dispatch(setErrorSnackMessage(err));
    52    const onClose = () => closeDeleteModalAndRefresh(false);
    53  
    54    const [deleteLoading, invokeDeleteApi] = useApi(onDelSuccess, onDelError);
    55  
    56    if (!selectedBucket) {
    57      return null;
    58    }
    59  
    60    const onConfirmDelete = () => {
    61      let url = `/api/v1/buckets/${selectedBucket}/replication/${ruleToDelete}`;
    62  
    63      if (deleteSelectedRules) {
    64        if (allSelected) {
    65          url = `/api/v1/buckets/${selectedBucket}/delete-all-replication-rules`;
    66        } else {
    67          url = `/api/v1/buckets/${selectedBucket}/delete-selected-replication-rules`;
    68          invokeDeleteApi("DELETE", url, { rules: rulesToDelete });
    69          return;
    70        }
    71      } else if (remainingRules === 1) {
    72        url = `/api/v1/buckets/${selectedBucket}/delete-all-replication-rules`;
    73      }
    74  
    75      invokeDeleteApi("DELETE", url);
    76    };
    77  
    78    return (
    79      <ConfirmDialog
    80        title={
    81          deleteSelectedRules
    82            ? "Delete Selected Replication Rules"
    83            : "Delete Replication Rule"
    84        }
    85        confirmText={"Delete"}
    86        isOpen={deleteOpen}
    87        titleIcon={<ConfirmDeleteIcon />}
    88        isLoading={deleteLoading}
    89        onConfirm={onConfirmDelete}
    90        onClose={onClose}
    91        confirmButtonProps={{
    92          disabled: deleteSelectedRules && confirmationText !== "Yes, I am sure",
    93        }}
    94        confirmationContent={
    95          <Fragment>
    96            {deleteSelectedRules ? (
    97              <Fragment>
    98                Are you sure you want to remove the selected replication rules for
    99                bucket <b>{selectedBucket}</b>?<br />
   100                <br />
   101                To continue please type <b>Yes, I am sure</b> in the box.
   102                <Grid item xs={12}>
   103                  <InputBox
   104                    id="retype-tenant"
   105                    name="retype-tenant"
   106                    onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
   107                      setConfirmationText(event.target.value);
   108                    }}
   109                    label=""
   110                    value={confirmationText}
   111                  />
   112                </Grid>
   113              </Fragment>
   114            ) : (
   115              <Fragment>
   116                Are you sure you want to delete replication rule{" "}
   117                <b>{ruleToDelete}</b>?
   118              </Fragment>
   119            )}
   120          </Fragment>
   121        }
   122      />
   123    );
   124  };
   125  
   126  export default DeleteReplicationRule;