github.com/minio/console@v1.4.1/web-app/src/screens/Console/Users/AddUsersSlice.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 { createSlice, PayloadAction } from "@reduxjs/toolkit"; 18 import { createUserAsync, resetFormAsync } from "./thunk/AddUsersThunk"; 19 20 export interface ICreateUser { 21 userName: string; 22 secretKey: string; 23 selectedGroups: string[]; 24 selectedPolicies: string[]; 25 sendEnabled: boolean; 26 addLoading: boolean; 27 apinoerror: boolean; 28 secretKeylength: number; 29 } 30 31 const initialState: ICreateUser = { 32 addLoading: false, 33 sendEnabled: false, 34 apinoerror: false, 35 userName: "", 36 secretKey: "", 37 selectedGroups: [], 38 selectedPolicies: [], 39 secretKeylength: 0, 40 }; 41 42 export const createUserSlice = createSlice({ 43 name: "createUser", 44 initialState, 45 reducers: { 46 setAddLoading: (state, action: PayloadAction<boolean>) => { 47 state.addLoading = action.payload; 48 }, 49 setUserName: (state, action: PayloadAction<string>) => { 50 state.userName = action.payload; 51 }, 52 setSelectedGroups: (state, action: PayloadAction<string[]>) => { 53 state.selectedGroups = action.payload; 54 }, 55 setSecretKey: (state, action: PayloadAction<string>) => { 56 state.secretKey = action.payload; 57 state.secretKeylength = state.secretKey.length; 58 }, 59 setSelectedPolicies: (state, action: PayloadAction<string[]>) => { 60 state.selectedPolicies = action.payload; 61 }, 62 setSendEnabled: (state) => { 63 state.sendEnabled = state.userName.trim() !== ""; 64 }, 65 setApinoerror: (state, action: PayloadAction<boolean>) => { 66 state.apinoerror = action.payload; 67 }, 68 }, 69 extraReducers: (builder) => { 70 builder 71 .addCase(resetFormAsync.fulfilled, (state, action) => { 72 state.userName = ""; 73 state.selectedGroups = []; 74 state.secretKey = ""; 75 state.selectedPolicies = []; 76 }) 77 .addCase(createUserAsync.pending, (state, action) => { 78 state.addLoading = true; 79 }) 80 .addCase(createUserAsync.rejected, (state, action) => { 81 state.addLoading = false; 82 }) 83 .addCase(createUserAsync.fulfilled, (state, action) => { 84 state.apinoerror = true; 85 }); 86 }, 87 }); 88 89 export const { 90 setUserName, 91 setSelectedGroups, 92 setSecretKey, 93 setSelectedPolicies, 94 setAddLoading, 95 setSendEnabled, 96 setApinoerror, 97 } = createUserSlice.actions; 98 99 export default createUserSlice.reducer;