github.com/minio/console@v1.4.1/web-app/src/screens/Console/Buckets/BucketDetails/EnableVersioningModal.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 { Box, Button, FormLayout, ModalBox, Switch } from "mds";
    19  import { BucketVersioningResponse } from "api/consoleApi";
    20  import { api } from "api";
    21  import { errorToHandler } from "api/errors";
    22  import { setErrorSnackMessage } from "../../../../systemSlice";
    23  import { useAppDispatch } from "../../../../store";
    24  import CSVMultiSelector from "../../Common/FormComponents/CSVMultiSelector/CSVMultiSelector";
    25  import { modalStyleUtils } from "../../Common/FormComponents/common/styleLibrary";
    26  
    27  interface IVersioningEventProps {
    28    closeVersioningModalAndRefresh: (refresh: boolean) => void;
    29    modalOpen: boolean;
    30    selectedBucket: string;
    31    versioningInfo: BucketVersioningResponse | undefined;
    32    objectLockingEnabled: boolean;
    33  }
    34  
    35  const parseExcludedPrefixes = (
    36    bucketVersioning: BucketVersioningResponse | undefined,
    37  ) => {
    38    const excludedPrefixes = bucketVersioning?.excludedPrefixes;
    39  
    40    if (excludedPrefixes) {
    41      return excludedPrefixes.map((item) => item.prefix).join(",");
    42    }
    43  
    44    return "";
    45  };
    46  
    47  const EnableVersioningModal = ({
    48    closeVersioningModalAndRefresh,
    49    modalOpen,
    50    selectedBucket,
    51    versioningInfo = {},
    52    objectLockingEnabled,
    53  }: IVersioningEventProps) => {
    54    const dispatch = useAppDispatch();
    55  
    56    const [versioningLoading, setVersioningLoading] = useState<boolean>(false);
    57    const [versionState, setVersionState] = useState<boolean>(
    58      versioningInfo?.status === "Enabled",
    59    );
    60    const [excludeFolders, setExcludeFolders] = useState<boolean>(
    61      !!versioningInfo?.excludeFolders,
    62    );
    63    const [excludedPrefixes, setExcludedPrefixes] = useState<string>(
    64      parseExcludedPrefixes(versioningInfo),
    65    );
    66  
    67    const enableVersioning = () => {
    68      if (versioningLoading) {
    69        return;
    70      }
    71      setVersioningLoading(true);
    72  
    73      api.buckets
    74        .setBucketVersioning(selectedBucket, {
    75          enabled: versionState,
    76          excludeFolders: versionState ? excludeFolders : false,
    77          excludePrefixes: versionState
    78            ? excludedPrefixes.split(",").filter((item) => item.trim() !== "")
    79            : [],
    80        })
    81        .then(() => {
    82          setVersioningLoading(false);
    83          closeVersioningModalAndRefresh(true);
    84        })
    85        .catch((err) => {
    86          setVersioningLoading(false);
    87          dispatch(setErrorSnackMessage(errorToHandler(err.error)));
    88        });
    89    };
    90  
    91    const resetForm = () => {
    92      setExcludedPrefixes("");
    93      setExcludeFolders(false);
    94      setVersionState(false);
    95    };
    96  
    97    return (
    98      <ModalBox
    99        onClose={() => closeVersioningModalAndRefresh(false)}
   100        open={modalOpen}
   101        title={`Versioning on Bucket`}
   102      >
   103        <FormLayout withBorders={false} containerPadding={false}>
   104          <Switch
   105            id={"activateVersioning"}
   106            label={"Versioning Status"}
   107            checked={versionState}
   108            onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
   109              setVersionState(e.target.checked);
   110            }}
   111            indicatorLabels={["Enabled", "Disabled"]}
   112          />
   113          {versionState && !objectLockingEnabled && (
   114            <Fragment>
   115              <Switch
   116                id={"excludeFolders"}
   117                label={"Exclude Folders"}
   118                checked={excludeFolders}
   119                onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
   120                  setExcludeFolders(e.target.checked);
   121                }}
   122                indicatorLabels={["Enabled", "Disabled"]}
   123              />
   124              <CSVMultiSelector
   125                elements={excludedPrefixes}
   126                label={"Excluded Prefixes"}
   127                name={"excludedPrefixes"}
   128                onChange={(value: string | string[]) => {
   129                  let valCh = "";
   130  
   131                  if (Array.isArray(value)) {
   132                    valCh = value.join(",");
   133                  } else {
   134                    valCh = value;
   135                  }
   136                  setExcludedPrefixes(valCh);
   137                }}
   138                withBorder={true}
   139              />
   140            </Fragment>
   141          )}
   142          <Box sx={modalStyleUtils.modalButtonBar}>
   143            <Button
   144              id={"clear"}
   145              type="button"
   146              variant="regular"
   147              color="primary"
   148              onClick={resetForm}
   149              label={"Clear"}
   150            />
   151            <Button
   152              type="submit"
   153              variant="callAction"
   154              onClick={enableVersioning}
   155              id="saveTag"
   156              label={"Save"}
   157            />
   158          </Box>
   159        </FormLayout>
   160      </ModalBox>
   161    );
   162  };
   163  
   164  export default EnableVersioningModal;