github.com/minio/console@v1.4.1/web-app/src/screens/Console/Buckets/BucketDetails/DeleteEvent.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 } from "react";
    18  import get from "lodash/get";
    19  import { ErrorResponseHandler } from "../../../../common/types";
    20  import useApi from "../../Common/Hooks/useApi";
    21  import ConfirmDialog from "../../Common/ModalWrapper/ConfirmDialog";
    22  import { ConfirmDeleteIcon } from "mds";
    23  import { setErrorSnackMessage } from "../../../../systemSlice";
    24  import { useAppDispatch } from "../../../../store";
    25  import { NotificationConfig } from "api/consoleApi";
    26  
    27  interface IDeleteEventProps {
    28    closeDeleteModalAndRefresh: (refresh: boolean) => void;
    29    deleteOpen: boolean;
    30    selectedBucket: string;
    31    bucketEvent: NotificationConfig | null;
    32  }
    33  
    34  const DeleteEvent = ({
    35    closeDeleteModalAndRefresh,
    36    deleteOpen,
    37    selectedBucket,
    38    bucketEvent,
    39  }: IDeleteEventProps) => {
    40    const dispatch = useAppDispatch();
    41    const onDelSuccess = () => closeDeleteModalAndRefresh(true);
    42    const onDelError = (err: ErrorResponseHandler) =>
    43      dispatch(setErrorSnackMessage(err));
    44    const onClose = () => closeDeleteModalAndRefresh(false);
    45  
    46    const [deleteLoading, invokeDeleteApi] = useApi(onDelSuccess, onDelError);
    47  
    48    if (!selectedBucket) {
    49      return null;
    50    }
    51  
    52    const onConfirmDelete = () => {
    53      if (bucketEvent === null) {
    54        return;
    55      }
    56  
    57      const events: string[] = get(bucketEvent, "events", []);
    58      const prefix = get(bucketEvent, "prefix", "");
    59      const suffix = get(bucketEvent, "suffix", "");
    60  
    61      const cleanEvents = events.reduce((acc: string[], currVal: string) => {
    62        if (!acc.includes(currVal)) {
    63          return [...acc, currVal];
    64        }
    65        return acc;
    66      }, []);
    67  
    68      invokeDeleteApi(
    69        "DELETE",
    70        `/api/v1/buckets/${selectedBucket}/events/${bucketEvent.arn}`,
    71        {
    72          events: cleanEvents,
    73          prefix,
    74          suffix,
    75        },
    76      );
    77    };
    78  
    79    return (
    80      <ConfirmDialog
    81        title={`Delete Event`}
    82        confirmText={"Delete"}
    83        isOpen={deleteOpen}
    84        titleIcon={<ConfirmDeleteIcon />}
    85        isLoading={deleteLoading}
    86        onConfirm={onConfirmDelete}
    87        onClose={onClose}
    88        confirmationContent={
    89          <Fragment>Are you sure you want to delete this event?</Fragment>
    90        }
    91      />
    92    );
    93  };
    94  
    95  export default DeleteEvent;