github.com/minio/console@v1.4.1/web-app/src/screens/Console/Users/thunk/AddUsersThunk.tsx (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2022 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 { createAsyncThunk } from "@reduxjs/toolkit";
    18  import {
    19    setAddLoading,
    20    setSecretKey,
    21    setSelectedGroups,
    22    setSelectedPolicies,
    23    setUserName,
    24  } from "../AddUsersSlice";
    25  import { AppState } from "../../../../store";
    26  import { ErrorResponseHandler } from "../../../../common/types";
    27  import { setErrorSnackMessage } from "../../../../systemSlice";
    28  import api from "../../../../common/api";
    29  
    30  export const resetFormAsync = createAsyncThunk(
    31    "resetForm/resetFormAsync",
    32    async (_, { dispatch }) => {
    33      dispatch(setSelectedGroups([]));
    34      dispatch(setUserName(""));
    35      dispatch(setSecretKey(""));
    36      dispatch(setSelectedPolicies([]));
    37    },
    38  );
    39  
    40  export const createUserAsync = createAsyncThunk(
    41    "createTenant/createNamespaceAsync",
    42    async (_, { getState, rejectWithValue, dispatch }) => {
    43      const state = getState() as AppState;
    44      const accessKey = state.createUser.userName;
    45      const secretKey = state.createUser.secretKey;
    46      const selectedGroups = state.createUser.selectedGroups;
    47      const selectedPolicies = state.createUser.selectedPolicies;
    48      return api
    49        .invoke("POST", "/api/v1/users", {
    50          accessKey,
    51          secretKey,
    52          groups: selectedGroups,
    53          policies: selectedPolicies,
    54        })
    55        .then(() => {
    56          dispatch(setAddLoading(false));
    57          dispatch(resetFormAsync());
    58        })
    59        .catch((err: ErrorResponseHandler) => {
    60          dispatch(setAddLoading(false));
    61          dispatch(setErrorSnackMessage(err));
    62        });
    63    },
    64  );