|
| 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 | +} |
0 commit comments