Skip to content

Commit

Permalink
Wizard: Add an ability to delete clusters from UI
Browse files Browse the repository at this point in the history
+ Added delete Button
+ Added useDeleteCluster hook

Resolves Issue #65
  • Loading branch information
Nilumilak committed Feb 19, 2024
1 parent 11a57d1 commit 32b90bf
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
48 changes: 48 additions & 0 deletions frontend/src/widgets/ClusterConfigForm/hooks/useDeleteCluster.ts
Original file line number Diff line number Diff line change
@@ -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<void, unknown, string, unknown>,
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,
];
}
25 changes: 25 additions & 0 deletions frontend/src/widgets/ClusterConfigForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,12 +55,23 @@ const ClusterConfigForm: React.FC<ClusterConfigFormProps> = ({

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 {
Expand Down Expand Up @@ -146,6 +160,17 @@ const ClusterConfigForm: React.FC<ClusterConfigFormProps> = ({
>
Submit
</Button>
{initialValues.name && (
<Button
type="button"
buttonSize="L"
buttonType="danger"
inProgress={isDeleting}
onClick={confirmClusterDelete}
>
Delete
</Button>
)}
</S.ButtonWrapper>
</FlexFieldset>
</StyledForm>
Expand Down

0 comments on commit 32b90bf

Please sign in to comment.