import React, { useRef, useState } from 'react' import * as yup from 'yup' import { useMutate } from 'restful-react' import { FontVariation, Intent } from '@harnessio/design-system' import { Button, Dialog, Layout, Heading, Container, Formik, FormikForm, FormInput, FlexExpander, useToaster, StringSubstitute, ButtonVariation } from '@harnessio/uicore' import { Icon } from '@harnessio/icons' import { useStrings } from 'framework/strings' import { useModalHook } from 'hooks/useModalHook' import { useGetSpaceParam } from 'hooks/useGetSpaceParam' import type { OpenapiUpdateSecretRequest, TypesSecret } from 'services/code' import type { SecretFormData } from 'components/NewSecretModalButton/NewSecretModalButton' import { getErrorMessage } from 'utils/Utils' const useUpdateSecretModal = () => { const { getString } = useStrings() const space = useGetSpaceParam() const { showError, showSuccess } = useToaster() const [secret, setSecret] = useState() const postUpdate = useRef<() => Promise>() const { mutate: updateSecret, loading } = useMutate({ verb: 'PATCH', path: `/api/v1/secrets/${space}/${secret?.uid}/+` }) const handleSubmit = async (formData: SecretFormData) => { try { const payload: OpenapiUpdateSecretRequest = { data: formData.value, description: formData.description, uid: formData.name } await updateSecret(payload) hideModal() showSuccess( ) postUpdate.current?.() } catch (exception) { showError(getErrorMessage(exception), 0, getString('secrets.failedToUpdateSecret')) } } const [openModal, hideModal] = useModalHook(() => { const onClose = () => { hideModal() } return ( {getString('secrets.updateSecret')} } style={{ width: 700, maxHeight: '95vh', overflow: 'auto' }}> ) }, [secret]) return { openModal: ({ secretToUpdate, openSecretUpdate }: { secretToUpdate: TypesSecret openSecretUpdate: () => Promise }) => { setSecret(secretToUpdate) postUpdate.current = openSecretUpdate openModal() } } } export default useUpdateSecretModal