Skip to content

Commit 54bfbab

Browse files
committed
chore: delete ci node component
1 parent cf92ced commit 54bfbab

File tree

8 files changed

+191
-5
lines changed

8 files changed

+191
-5
lines changed

src/Assets/IconV2/ic-add.svg

Lines changed: 2 additions & 2 deletions
Loading

src/Assets/IconV2/ic-delete.svg

Lines changed: 3 additions & 3 deletions
Loading
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Copyright (c) 2024. Devtron Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { useState } from 'react'
18+
19+
import { ERROR_STATUS_CODE } from '@Common/Constants'
20+
import { Button, ButtonStyleType, ButtonVariantType, DeleteConfirmationModal } from '@Shared/Components'
21+
import { ComponentSizeType, DeleteComponentsName } from '@Shared/constants'
22+
import { ToastManager, ToastVariantType } from '@Shared/index'
23+
import { ReactComponent as ICDelete } from '@IconsV2/ic-delete.svg'
24+
import { deleteWorkflow, savePipeline } from './utils'
25+
import { DeleteCINodeButtonProps } from './types'
26+
import { preventDefault, showError, stopPropagation } from '..'
27+
28+
export const DeleteCINodeButton = ({
29+
testId,
30+
isCIPipeline,
31+
disabled,
32+
title,
33+
isJobView,
34+
deletePayloadConfig,
35+
onDelete,
36+
getWorkflows,
37+
}: DeleteCINodeButtonProps) => {
38+
const [showDeleteModal, setShowDeleteModal] = useState<boolean>(false)
39+
40+
const closeCIDeleteModal = (): void => {
41+
setShowDeleteModal(false)
42+
}
43+
44+
const onClickDeleteShowModal = (e) => {
45+
stopPropagation(e)
46+
preventDefault(e)
47+
setShowDeleteModal(true)
48+
}
49+
50+
const onDeleteWorkflow = async () => {
51+
await deleteWorkflow(String(deletePayloadConfig.appId), Number(deletePayloadConfig.appWorkflowId))
52+
.then((response) => {
53+
if (response.errors) {
54+
const { errors } = response
55+
const { userMessage } = errors[0]
56+
ToastManager.showToast({
57+
variant: ToastVariantType.error,
58+
description: userMessage,
59+
})
60+
return
61+
}
62+
63+
if (response.status.toLowerCase() === 'ok') {
64+
ToastManager.showToast({
65+
variant: ToastVariantType.success,
66+
description: 'Workflow Deleted',
67+
})
68+
getWorkflows()
69+
}
70+
})
71+
.catch((errors) => {
72+
showError(errors)
73+
})
74+
}
75+
76+
const onClickDelete = async () => {
77+
const deletePayload = {
78+
action: 2, // To delete the pipeline
79+
appId: Number(deletePayloadConfig.appId),
80+
appWorkflowId: Number(deletePayloadConfig.appWorkflowId),
81+
ciPipeline: {
82+
id: Number(deletePayloadConfig.pipelineId),
83+
name: deletePayloadConfig.pipelineName,
84+
},
85+
}
86+
await savePipeline(deletePayload)
87+
if (typeof onDelete === 'function') {
88+
onDelete()
89+
}
90+
91+
await onDeleteWorkflow()
92+
}
93+
94+
const renderDeleteButton = () =>
95+
isCIPipeline ? (
96+
<Button
97+
dataTestId={testId}
98+
disabled={disabled}
99+
onClick={onClickDeleteShowModal}
100+
text="Delete Pipeline"
101+
style={ButtonStyleType.negative}
102+
/>
103+
) : (
104+
<Button
105+
ariaLabel="Delete pipeline"
106+
variant={ButtonVariantType.borderLess}
107+
dataTestId={testId}
108+
size={ComponentSizeType.xs}
109+
showAriaLabelInTippy
110+
onClick={onClickDeleteShowModal}
111+
style={ButtonStyleType.negativeGrey}
112+
icon={<ICDelete />}
113+
disabled={disabled}
114+
/>
115+
)
116+
117+
const renderDeleteCIModal = () =>
118+
showDeleteModal && (
119+
<DeleteConfirmationModal
120+
title={title}
121+
component={isJobView ? DeleteComponentsName.Job : DeleteComponentsName.BuildPipeline}
122+
subtitle={`Are you sure you want to delete this pipeline from '${title}' ?`}
123+
closeConfirmationModal={closeCIDeleteModal}
124+
onDelete={onClickDelete}
125+
errorCodeToShowCannotDeleteDialog={ERROR_STATUS_CODE.BAD_REQUEST}
126+
renderCannotDeleteConfirmationSubTitle="Please delete deployment pipelines for this workflow first and try again."
127+
successToastMessage="Pipeline Deleted Successfully"
128+
/>
129+
)
130+
131+
return (
132+
<>
133+
{renderDeleteButton()}
134+
{renderDeleteCIModal()}
135+
</>
136+
)
137+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './DeleteCINodeButton'
2+
export * from './utils'
3+
export * from './types'
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
interface DeletePayloadConfig {
2+
appId: string
3+
appWorkflowId: number
4+
pipelineId: number
5+
pipelineName: string
6+
}
7+
8+
export interface DeleteCINodeButtonProps {
9+
testId: string
10+
isCIPipeline?: boolean
11+
disabled: boolean
12+
title: string
13+
isJobView?: boolean
14+
deletePayloadConfig: DeletePayloadConfig
15+
onDelete?: () => void
16+
getWorkflows: () => void
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Routes } from '@Shared/constants'
2+
import { post, trash } from '..'
3+
4+
export function savePipeline(request, isRegexMaterial = false): Promise<any> {
5+
let url
6+
if (isRegexMaterial) {
7+
url = `${Routes.CI_PIPELINE_PATCH}/regex`
8+
} else {
9+
url = `${Routes.CI_PIPELINE_PATCH}`
10+
}
11+
return post(url, request)
12+
}
13+
14+
export function deleteWorkflow(appId: string, workflowId: number) {
15+
const URL = `${Routes.WORKFLOW}/${appId}/${workflowId}`
16+
return trash(URL)
17+
}

src/Common/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,4 @@ export * from './SegmentedBarChart'
6666
export * from './CodeEditor/types'
6767
export * from './Tooltip'
6868
export * from './SegmentedControl'
69+
export * from './DeleteCINodeButton'

src/Shared/constants.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,3 +515,14 @@ export const DC_DELETE_SUBTITLES = {
515515
DELETE_ENVIRONMENT_SUBTITLE: 'Are you sure you want to delete this environment?',
516516
DELETE_CLUSTER_SUBTITLES: 'Are you sure you want to delete this cluster?',
517517
}
518+
519+
export const enum DeleteComponentsName {
520+
Cluster = 'cluster',
521+
Job = 'job pipeline',
522+
BuildPipeline = 'build pipeline',
523+
}
524+
525+
export const Routes = {
526+
CI_PIPELINE_PATCH: 'app/ci-pipeline/patch',
527+
WORKFLOW: 'app/app-wf',
528+
}

0 commit comments

Comments
 (0)