diff --git a/frontend/src/widgets/ClusterConfigForm/hooks/useDeleteCluster.ts b/frontend/src/widgets/ClusterConfigForm/hooks/useDeleteCluster.ts new file mode 100644 index 000000000..5e9102456 --- /dev/null +++ b/frontend/src/widgets/ClusterConfigForm/hooks/useDeleteCluster.ts @@ -0,0 +1,48 @@ +import React from 'react'; +import { + useQueryClient, + useMutation, + UseMutationResult, +} from '@tanstack/react-query'; +import { appConfigApiClient as api } from 'lib/api'; +import { ApplicationConfigPropertiesKafkaClusters } from 'generated-sources'; + +export function useDeleteCluster(): [ + UseMutationResult, + boolean +] { + const client = useQueryClient(); + const [isDeleteing, setIsDeleting] = React.useState(false); + + return [ + useMutation( + async (clusterName: string) => { + setIsDeleting(true); + const existingConfig = await api.getCurrentConfig(); + const existingClusters = + existingConfig.properties?.kafka?.clusters || []; + + let clusters: ApplicationConfigPropertiesKafkaClusters[] = []; + + if (existingClusters.length > 0) { + clusters = existingClusters.filter((c) => c.name !== clusterName); + } + const config = { + ...existingConfig, + properties: { + ...existingConfig.properties, + kafka: { clusters }, + }, + }; + return api.restartWithConfig({ restartRequest: { config } }); + }, + { + onSuccess: () => { + client.invalidateQueries(['app', 'config']); + setIsDeleting(false); + }, + } + ), + isDeleteing, + ]; +} diff --git a/frontend/src/widgets/ClusterConfigForm/index.tsx b/frontend/src/widgets/ClusterConfigForm/index.tsx index d4007cf48..16e1509f4 100644 --- a/frontend/src/widgets/ClusterConfigForm/index.tsx +++ b/frontend/src/widgets/ClusterConfigForm/index.tsx @@ -22,6 +22,9 @@ import Metrics from 'widgets/ClusterConfigForm/Sections/Metrics'; import CustomAuthentication from 'widgets/ClusterConfigForm/Sections/CustomAuthentication'; import Authentication from 'widgets/ClusterConfigForm/Sections/Authentication/Authentication'; import KSQL from 'widgets/ClusterConfigForm/Sections/KSQL'; +import { useConfirm } from 'lib/hooks/useConfirm'; + +import { useDeleteCluster } from './hooks/useDeleteCluster'; interface ClusterConfigFormProps { hasCustomConfig?: boolean; @@ -52,12 +55,23 @@ const ClusterConfigForm: React.FC = ({ const validate = useValidateAppConfig(); const update = useUpdateAppConfig({ initialName: initialValues.name }); + const [deleteCluster, isDeleting] = useDeleteCluster(); + const confirm = useConfirm(true); + const { value: isFormDisabled, setTrue: disableForm, setFalse: enableForm, } = useBoolean(); + const confirmClusterDelete = () => + confirm('Are you sure want to delete this cluster?', async () => { + if (initialValues.name) { + await deleteCluster.mutateAsync(initialValues.name); + navigate('/'); + } + }); + const onSubmit = async (data: ClusterConfigFormValues) => { const config = transformFormDataToPayload(data); try { @@ -146,6 +160,17 @@ const ClusterConfigForm: React.FC = ({ > Submit + {initialValues.name && ( + + )}