github.com/minio/console@v1.4.1/web-app/src/screens/Console/Buckets/BucketDetails/AddKeyModal.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 React, { Fragment, useState } from "react"; 18 import { Grid, InputBox } from "mds"; 19 import { useAppDispatch } from "../../../../store"; 20 import { setErrorSnackMessage } from "../../../../systemSlice"; 21 import ConfirmDialog from "../../Common/ModalWrapper/ConfirmDialog"; 22 import KMSHelpBox from "../../KMS/KMSHelpbox"; 23 import { api } from "api"; 24 import { ApiError, HttpResponse } from "api/consoleApi"; 25 import { errorToHandler } from "api/errors"; 26 27 interface IAddKeyModalProps { 28 closeAddModalAndRefresh: (refresh: boolean) => void; 29 addOpen: boolean; 30 } 31 32 const AddKeyModal = ({ 33 closeAddModalAndRefresh, 34 addOpen, 35 }: IAddKeyModalProps) => { 36 const dispatch = useAppDispatch(); 37 const onClose = () => closeAddModalAndRefresh(false); 38 39 const [loadingAdd, setLoadingAdd] = useState<boolean>(false); 40 const [keyName, setKeyName] = useState<string>(""); 41 42 const onConfirmAdd = () => { 43 setLoadingAdd(true); 44 api.kms 45 .kmsCreateKey({ key: keyName }) 46 .then((_) => { 47 closeAddModalAndRefresh(true); 48 }) 49 .catch(async (res: HttpResponse<void, ApiError>) => { 50 const err = (await res.json()) as ApiError; 51 dispatch(setErrorSnackMessage(errorToHandler(err))); 52 closeAddModalAndRefresh(false); 53 }) 54 .finally(() => setLoadingAdd(false)); 55 }; 56 57 return ( 58 <ConfirmDialog 59 title={""} 60 confirmText={"Create"} 61 isOpen={addOpen} 62 isLoading={loadingAdd} 63 onConfirm={onConfirmAdd} 64 onClose={onClose} 65 confirmButtonProps={{ 66 disabled: keyName.indexOf(" ") !== -1 || keyName === "" || loadingAdd, 67 variant: "callAction", 68 }} 69 confirmationContent={ 70 <Fragment> 71 <KMSHelpBox 72 helpText={"Create Key"} 73 contents={[ 74 "Create a new cryptographic key in the Key Management Service server connected to MINIO.", 75 ]} 76 /> 77 78 <Grid item xs={12} sx={{ marginTop: 15 }}> 79 <InputBox 80 id="key-name" 81 name="key-name" 82 label="Key Name" 83 autoFocus={true} 84 value={keyName} 85 error={ 86 keyName.indexOf(" ") !== -1 87 ? "Key name cannot contain spaces" 88 : "" 89 } 90 onChange={(e: React.ChangeEvent<HTMLInputElement>) => { 91 setKeyName(e.target.value); 92 }} 93 /> 94 </Grid> 95 </Fragment> 96 } 97 /> 98 ); 99 }; 100 101 export default AddKeyModal;