/* * Copyright 2023 Harness, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { useToaster, type ButtonProps, Button, Dialog, Layout, Heading, Container, Formik, FormikForm, FormInput, FlexExpander, ButtonVariation } from '@harnessio/uicore' import { Icon } from '@harnessio/icons' import { Intent, FontVariation } from '@harnessio/design-system' import React from 'react' import { useMutate } from 'restful-react' import * as yup from 'yup' import { useModalHook } from 'hooks/useModalHook' import { useStrings } from 'framework/strings' import type { OpenapiCreateSecretRequest, TypesSecret } from 'services/code' import { getErrorMessage } from 'utils/Utils' import Config from 'Config' import css from './NewSecretModalButton.module.scss' export interface SecretFormData { value: string description: string name: string showValue: boolean } const formInitialValues: SecretFormData = { value: '', description: '', name: '', showValue: false } export interface NewSecretModalButtonProps extends Omit { space: string modalTitle: string submitButtonTitle?: string cancelButtonTitle?: string onSuccess: () => void } export const NewSecretModalButton: React.FC = ({ space, modalTitle, submitButtonTitle, cancelButtonTitle, onSuccess, ...props }) => { const ModalComponent: React.FC = () => { const { getString } = useStrings() const { showError, showSuccess } = useToaster() const { mutate: createSecret, loading } = useMutate({ verb: 'POST', path: `/api/v1/secrets` }) const handleSubmit = async (formData: SecretFormData) => { try { const payload: OpenapiCreateSecretRequest = { space_ref: space, data: formData.value, description: formData.description, identifier: formData.name } await createSecret(payload) hideModal() showSuccess(getString('secrets.createSuccess')) onSuccess() } catch (exception) { showError(getErrorMessage(exception), 0, getString('secrets.failedToCreate')) } } return ( {modalTitle} } style={{ width: 700, maxHeight: '95vh', overflow: 'auto' }}> {formik => ( ) } const [openModal, hideModal] = useModalHook(ModalComponent, [onSuccess]) return