github.com/minio/console@v1.4.1/web-app/src/screens/Console/Buckets/BucketDetails/SetRetentionConfig.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, Fragment } from "react";
    18  import {
    19    Button,
    20    Loader,
    21    Grid,
    22    FormLayout,
    23    RadioGroup,
    24    InputBox,
    25    ProgressBar,
    26  } from "mds";
    27  import { api } from "api";
    28  import { ObjectRetentionMode, ObjectRetentionUnit } from "api/consoleApi";
    29  import { errorToHandler } from "api/errors";
    30  
    31  import { modalStyleUtils } from "../../Common/FormComponents/common/styleLibrary";
    32  import { setModalErrorSnackMessage } from "../../../../systemSlice";
    33  import { useAppDispatch } from "../../../../store";
    34  import ModalWrapper from "../../Common/ModalWrapper/ModalWrapper";
    35  
    36  interface ISetRetentionConfigProps {
    37    open: boolean;
    38    bucketName: string;
    39    closeModalAndRefresh: () => void;
    40  }
    41  
    42  const SetRetentionConfig = ({
    43    open,
    44    bucketName,
    45    closeModalAndRefresh,
    46  }: ISetRetentionConfigProps) => {
    47    const dispatch = useAppDispatch();
    48    const [addLoading, setAddLoading] = useState<boolean>(false);
    49    const [loadingForm, setLoadingForm] = useState<boolean>(true);
    50    const [retentionMode, setRetentionMode] = useState<
    51      ObjectRetentionMode | undefined
    52    >(ObjectRetentionMode.Compliance);
    53    const [retentionUnit, setRetentionUnit] = useState<
    54      ObjectRetentionUnit | undefined
    55    >(ObjectRetentionUnit.Days);
    56    const [retentionValidity, setRetentionValidity] = useState<
    57      number | undefined
    58    >(1);
    59    const [valid, setValid] = useState<boolean>(false);
    60  
    61    const setRetention = (event: React.FormEvent) => {
    62      event.preventDefault();
    63      if (addLoading) {
    64        return;
    65      }
    66      setAddLoading(true);
    67      api.buckets
    68        .setBucketRetentionConfig(bucketName, {
    69          mode: retentionMode || ObjectRetentionMode.Compliance,
    70          unit: retentionUnit || ObjectRetentionUnit.Days,
    71          validity: retentionValidity || 1,
    72        })
    73        .then(() => {
    74          setAddLoading(false);
    75          closeModalAndRefresh();
    76        })
    77        .catch((err) => {
    78          setAddLoading(false);
    79          dispatch(setModalErrorSnackMessage(errorToHandler(err.error)));
    80        });
    81    };
    82  
    83    useEffect(() => {
    84      if (Number.isNaN(retentionValidity) || (retentionValidity || 1) < 1) {
    85        setValid(false);
    86        return;
    87      }
    88      setValid(true);
    89    }, [retentionValidity]);
    90  
    91    useEffect(() => {
    92      if (loadingForm) {
    93        api.buckets
    94          .getBucketRetentionConfig(bucketName)
    95          .then((res) => {
    96            setLoadingForm(false);
    97  
    98            // We set default values
    99            setRetentionMode(res.data.mode);
   100            setRetentionValidity(res.data.validity);
   101            setRetentionUnit(res.data.unit);
   102          })
   103          .catch(() => {
   104            setLoadingForm(false);
   105          });
   106      }
   107    }, [loadingForm, bucketName]);
   108  
   109    return (
   110      <ModalWrapper
   111        title="Set Retention Configuration"
   112        modalOpen={open}
   113        onClose={() => {
   114          closeModalAndRefresh();
   115        }}
   116      >
   117        {loadingForm ? (
   118          <Loader style={{ width: 16, height: 16 }} />
   119        ) : (
   120          <form
   121            noValidate
   122            autoComplete="off"
   123            onSubmit={(e: React.FormEvent<HTMLFormElement>) => {
   124              setRetention(e);
   125            }}
   126          >
   127            <FormLayout containerPadding={false} withBorders={false}>
   128              <RadioGroup
   129                currentValue={retentionMode as string}
   130                id="retention_mode"
   131                name="retention_mode"
   132                label="Retention Mode"
   133                onChange={(e: React.ChangeEvent<{ value: unknown }>) => {
   134                  setRetentionMode(e.target.value as ObjectRetentionMode);
   135                }}
   136                selectorOptions={[
   137                  { value: "compliance", label: "Compliance" },
   138                  { value: "governance", label: "Governance" },
   139                ]}
   140                helpTip={
   141                  <Fragment>
   142                    {" "}
   143                    <a
   144                      href="https://min.io/docs/minio/macos/administration/object-management/object-retention.html#minio-object-locking-compliance"
   145                      target="blank"
   146                    >
   147                      Compliance
   148                    </a>{" "}
   149                    lock protects Objects from write operations by all users,
   150                    including the MinIO root user.
   151                    <br />
   152                    <br />
   153                    <a
   154                      href="https://min.io/docs/minio/macos/administration/object-management/object-retention.html#minio-object-locking-governance"
   155                      target="blank"
   156                    >
   157                      Governance
   158                    </a>{" "}
   159                    lock protects Objects from write operations by non-privileged
   160                    users.
   161                  </Fragment>
   162                }
   163                helpTipPlacement="right"
   164              />
   165              <RadioGroup
   166                currentValue={retentionUnit as string}
   167                id="retention_unit"
   168                name="retention_unit"
   169                label="Retention Unit"
   170                onChange={(e: React.ChangeEvent<{ value: unknown }>) => {
   171                  setRetentionUnit(e.target.value as ObjectRetentionUnit);
   172                }}
   173                selectorOptions={[
   174                  { value: "days", label: "Days" },
   175                  { value: "years", label: "Years" },
   176                ]}
   177              />
   178              <InputBox
   179                type="number"
   180                id="retention_validity"
   181                name="retention_validity"
   182                onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
   183                  setRetentionValidity(e.target.valueAsNumber);
   184                }}
   185                label="Retention Validity"
   186                value={String(retentionValidity)}
   187                required
   188                min="1"
   189              />
   190              <Grid item xs={12} sx={modalStyleUtils.modalButtonBar}>
   191                <Button
   192                  id={"cancel"}
   193                  type="button"
   194                  variant="regular"
   195                  disabled={addLoading}
   196                  onClick={() => {
   197                    closeModalAndRefresh();
   198                  }}
   199                  label={"Cancel"}
   200                />
   201                <Button
   202                  id={"set"}
   203                  type="submit"
   204                  variant="callAction"
   205                  color="primary"
   206                  disabled={addLoading || !valid}
   207                  label={"Set"}
   208                />
   209              </Grid>
   210              {addLoading && (
   211                <Grid item xs={12}>
   212                  <ProgressBar />
   213                </Grid>
   214              )}
   215            </FormLayout>
   216          </form>
   217        )}
   218      </ModalWrapper>
   219    );
   220  };
   221  
   222  export default SetRetentionConfig;