github.com/minio/console@v1.4.1/web-app/src/screens/Console/Buckets/ListBuckets/Objects/ObjectDetails/SetLegalHoldModal.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, { useEffect, useState } from "react";
    18  import get from "lodash/get";
    19  import { Box, Button, FormLayout, Grid, Switch } from "mds";
    20  import { BucketObject, ObjectLegalHoldStatus } from "api/consoleApi";
    21  import { api } from "api";
    22  import { errorToHandler } from "api/errors";
    23  import { modalStyleUtils } from "../../../../Common/FormComponents/common/styleLibrary";
    24  import { encodeURLString } from "../../../../../../common/utils";
    25  import { setModalErrorSnackMessage } from "../../../../../../systemSlice";
    26  import { useAppDispatch } from "../../../../../../store";
    27  import ModalWrapper from "../../../../Common/ModalWrapper/ModalWrapper";
    28  
    29  interface ISetRetentionProps {
    30    open: boolean;
    31    closeModalAndRefresh: (reload: boolean) => void;
    32    objectName: string;
    33    bucketName: string;
    34    actualInfo: BucketObject;
    35  }
    36  
    37  const SetLegalHoldModal = ({
    38    open,
    39    closeModalAndRefresh,
    40    objectName,
    41    bucketName,
    42    actualInfo,
    43  }: ISetRetentionProps) => {
    44    const dispatch = useAppDispatch();
    45    const [legalHoldEnabled, setLegalHoldEnabled] = useState<boolean>(false);
    46    const [isSaving, setIsSaving] = useState<boolean>(false);
    47    const versionId = actualInfo.version_id;
    48  
    49    useEffect(() => {
    50      const status = get(actualInfo, "legal_hold_status", "OFF");
    51      setLegalHoldEnabled(status === "ON");
    52    }, [actualInfo]);
    53  
    54    const onSubmit = (e: React.FormEvent) => {
    55      e.preventDefault();
    56      setIsSaving(true);
    57  
    58      api.buckets
    59        .putObjectLegalHold(
    60          bucketName,
    61          {
    62            prefix: encodeURLString(objectName),
    63            version_id: versionId || "",
    64          },
    65          {
    66            status: legalHoldEnabled
    67              ? ObjectLegalHoldStatus.Enabled
    68              : ObjectLegalHoldStatus.Disabled,
    69          },
    70        )
    71        .then(() => {
    72          setIsSaving(false);
    73          closeModalAndRefresh(true);
    74        })
    75        .catch((err) => {
    76          dispatch(setModalErrorSnackMessage(errorToHandler(err.error)));
    77          setIsSaving(false);
    78        });
    79    };
    80  
    81    const resetForm = () => {
    82      setLegalHoldEnabled(false);
    83    };
    84  
    85    return (
    86      <ModalWrapper
    87        title="Set Legal Hold"
    88        modalOpen={open}
    89        onClose={() => {
    90          resetForm();
    91          closeModalAndRefresh(false);
    92        }}
    93      >
    94        <form
    95          noValidate
    96          autoComplete="off"
    97          onSubmit={(e: React.FormEvent<HTMLFormElement>) => {
    98            onSubmit(e);
    99          }}
   100        >
   101          <FormLayout withBorders={false} containerPadding={false}>
   102            <Box className={"inputItem"}>
   103              <strong>Object</strong>: {bucketName}
   104            </Box>
   105            <Switch
   106              value="legalhold"
   107              id="legalhold"
   108              name="legalhold"
   109              checked={legalHoldEnabled}
   110              onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
   111                setLegalHoldEnabled(!legalHoldEnabled);
   112              }}
   113              label={"Legal Hold Status"}
   114              indicatorLabels={["Enabled", "Disabled"]}
   115              tooltip={
   116                "To enable this feature you need to enable versioning on the bucket before creation"
   117              }
   118            />
   119            <Grid item xs={12} sx={modalStyleUtils.modalButtonBar}>
   120              <Button
   121                id={"clear"}
   122                type="button"
   123                variant="regular"
   124                onClick={resetForm}
   125                label={"Clear"}
   126              />
   127              <Button
   128                id={"save"}
   129                type="submit"
   130                variant="callAction"
   131                disabled={isSaving}
   132                label={" Save"}
   133              />
   134            </Grid>
   135          </FormLayout>
   136        </form>
   137      </ModalWrapper>
   138    );
   139  };
   140  
   141  export default SetLegalHoldModal;