Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | 251x 251x 251x 251x 251x 251x 251x 251x 251x 251x 223x 223x 223x 223x 223x 223x 223x 223x 223x 223x 1x 1x 223x 27x 27x 27x 27x 27x 27x 27x 223x 223x 28x 28x 28x 28x 1x 27x 27x 5x 5x 5x 22x 22x | /* * Copyright 2021 Harness Inc. All rights reserved. * Use of this source code is governed by the PolyForm Shield 1.0.0 license * that can be found in the licenses directory at the root of this repository, also available at * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt. */ import { useState } from 'react' import { omit, noop } from 'lodash-es' import { shouldShowError } from '@wings-software/uicore' import { useToaster } from '@common/exports' import { UseSaveSuccessResponse, useSaveToGitDialog } from '@common/modals/SaveToGitDialog/useSaveToGitDialog' import type { SaveToGitFormInterface } from '@common/components/SaveToGitForm/SaveToGitForm' import { Connector, ConnectorConfigDTO, ConnectorInfoDTO, CreateConnectorQueryParams, EntityGitDetails, useCreateConnector, useUpdateConnector } from 'services/cd-ng' import type { ConnectorCreateEditProps } from '@connectors/constants' import { Entities } from '@common/interfaces/GitSyncInterface' import { useStrings } from 'framework/strings' import useRBACError from '@rbac/utils/useRBACError/useRBACError' export interface BuildPayloadProps { projectIdentifier?: string orgIdentifier?: string branch?: string repo?: string } interface UseCreateEditConnector { accountId: string isEditMode: boolean isGitSyncEnabled: boolean afterSuccessHandler: (data: UseSaveSuccessResponse) => void gitDetails?: EntityGitDetails } interface OnInitiateConnectorCreateEditProps<T> { buildPayload: (data: T & BuildPayloadProps) => Connector connectorFormData: T & BuildPayloadProps customHandleCreate?: (payload: ConnectorConfigDTO) => Promise<ConnectorInfoDTO | undefined> customHandleUpdate?: (payload: ConnectorConfigDTO) => Promise<ConnectorInfoDTO | undefined> } export default function useCreateEditConnector<T>(props: UseCreateEditConnector) { const { showError, showSuccess } = useToaster() const { getString } = useStrings() const { getRBACErrorMessage } = useRBACError() const [connectorPayload, setConnectorPayload] = useState<Connector>({}) const [connectorResponse, setConnectorResponse] = useState<UseSaveSuccessResponse>() const [connectorData, setConnectorData] = useState<T & BuildPayloadProps>({} as T & BuildPayloadProps) let gitDetails = props.gitDetails const { mutate: createConnector, loading: creating } = useCreateConnector({ queryParams: { accountIdentifier: props.accountId } }) const { mutate: updateConnector, loading: updating } = useUpdateConnector({ queryParams: { accountIdentifier: props.accountId } }) const handleGitSyncEnabled = ( gitDetailsObject: EntityGitDetails | undefined, connectorFormData: T & BuildPayloadProps, payload: Connector ) => { // Using git context set at 1st step while creating new connector Iif (!props.isEditMode) { gitDetails = { ...gitDetailsObject, branch: connectorFormData?.branch, repoIdentifier: connectorFormData?.repo } } openSaveToGitDialog({ isEditing: props.isEditMode, resource: { type: Entities.CONNECTORS, name: payload.connector?.name || '', identifier: payload.connector?.identifier || '', gitDetails: { ...gitDetailsObject, branch: connectorFormData?.branch, repoIdentifier: connectorFormData?.repo } }, payload }) } const handleCreateOrEdit = async ( connectorFormData: T & BuildPayloadProps, payload: ConnectorCreateEditProps, objectId?: EntityGitDetails['objectId'] ): Promise<UseSaveSuccessResponse> => { const { gitData } = payload const data = payload.payload || connectorPayload let queryParams: CreateConnectorQueryParams = {} Iif (gitData) { queryParams = { accountIdentifier: props.accountId, ...omit(gitData, 'sourceBranch') } if (gitData.isNewBranch) { queryParams.baseBranch = connectorFormData?.branch } } const response = props.isEditMode ? await updateConnector(data, { queryParams: { ...queryParams, lastObjectId: objectId ?? gitDetails?.objectId } }) : await createConnector(data, { queryParams: queryParams }) setConnectorResponse(response) return { status: response.status, nextCallback: props.afterSuccessHandler.bind(null, response) } } const { openSaveToGitDialog } = useSaveToGitDialog<Connector>({ onSuccess: ( gitData: SaveToGitFormInterface, payload?: Connector, objectId?: string ): Promise<UseSaveSuccessResponse> => handleCreateOrEdit(connectorData, { gitData, payload: payload || connectorPayload }, objectId), onClose: noop }) return { connectorResponse, gitDetails, connectorPayload, loading: creating || updating, onInitiate: ({ connectorFormData, buildPayload, customHandleCreate, customHandleUpdate }: OnInitiateConnectorCreateEditProps<T & BuildPayloadProps>) => { setConnectorData(connectorFormData) const payload = buildPayload(connectorFormData) setConnectorPayload(payload) if (props.isGitSyncEnabled) { handleGitSyncEnabled(gitDetails, connectorFormData, payload) } else { { Iif (customHandleUpdate || customHandleCreate) { props.isEditMode ? customHandleUpdate?.(payload) : customHandleCreate?.(payload) } else { handleCreateOrEdit(connectorFormData, { payload: payload }) /* Handling non-git flow */ .then(res => { Eif (res.status === 'SUCCESS') { props.isEditMode ? showSuccess(getString('connectors.updatedSuccessfully')) : showSuccess(getString('connectors.createdSuccessfully')) res.nextCallback?.() } }) .catch(e => { Eif (shouldShowError(e)) { showError(getRBACErrorMessage(e)) } }) } } } } } } |