/* * Copyright 2023 Harness, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { useToaster, type ButtonProps, Button, Dialog, Layout, Container, Formik, FormikForm, FormInput, FlexExpander, Text, Checkbox, ButtonVariation } from '@harnessio/uicore' import { Icon } from '@harnessio/icons' import { FontVariation, Intent } from '@harnessio/design-system' import React from 'react' import { useMutate } from 'restful-react' import * as yup from 'yup' import { useModalHook } from 'hooks/useModalHook' import { useStrings } from 'framework/strings' import type { EnumTriggerAction, OpenapiCreateTriggerRequest, TypesTrigger } from 'services/code' import { getErrorMessage } from 'utils/Utils' import { allActions } from 'components/PipelineTriggersTab/PipelineTriggersTab' import css from './NewTriggerModalButton.module.scss' export interface TriggerFormData { name: string actions: EnumTriggerAction[] } const formInitialValues: TriggerFormData = { name: '', actions: [] } export interface NewTriggerModalButtonProps extends Omit { repoPath: string pipeline: string modalTitle: string submitButtonTitle?: string cancelButtonTitle?: string onSuccess: () => void } export const NewTriggerModalButton: React.FC = ({ repoPath, pipeline, modalTitle, submitButtonTitle, cancelButtonTitle, onSuccess, ...props }) => { const ModalComponent: React.FC = () => { const { getString } = useStrings() const { showError, showSuccess, clear: clearToaster } = useToaster() const { mutate: createTrigger, loading } = useMutate({ verb: 'POST', path: `/api/v1/repos/${repoPath}/+/pipelines/${pipeline}/triggers` }) const handleSubmit = async (formData: TriggerFormData) => { try { const payload: OpenapiCreateTriggerRequest = { actions: formData.actions, identifier: formData.name } await createTrigger(payload) hideModal() clearToaster() showSuccess(getString('triggers.createSuccess')) onSuccess() } catch (exception) { clearToaster() showError(getErrorMessage(exception), 0, getString('triggers.failedToCreate')) } } return ( {formik => ( {getString('triggers.actions')} {allActions.map((actionGroup, index) => ( {actionGroup.map(action => ( { if (event.currentTarget.checked) { formik.setFieldValue('actions', [...formik.values.actions, action.value]) } else { formik.setFieldValue( 'actions', formik.values.actions.filter((value: string) => value !== action.value) ) } }} /> ))} ))} ) } const [openModal, hideModal] = useModalHook(ModalComponent, [onSuccess]) return