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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 53x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 1x 52x 1x 52x 1x 52x 52x 52x 52x 39x 52x 21x 52x 16x 52x 1x | /* * Copyright 2022 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 React from 'react' import { Dialog } from '@blueprintjs/core' import { Button, ButtonVariation } from '@wings-software/uicore' import { useModalHook } from '@harness/use-modal' import { defaultTo, get, isEmpty, merge, noop } from 'lodash-es' import { useParams } from 'react-router-dom' import type { FormikErrors } from 'formik' import { useStrings } from 'framework/strings' import type { ModulePathParams, TemplateStudioPathProps } from '@common/interfaces/RouteInterfaces' import { Fields, ModalProps, TemplateConfigModal } from 'framework/Templates/TemplateConfigModal/TemplateConfigModal' import { TemplateContext } from '@templates-library/components/TemplateStudio/TemplateContext/TemplateContext' import { TemplateMenuItem, TemplatesActionPopover } from '@templates-library/components/TemplatesActionPopover/TemplatesActionPopover' import { useSaveTemplate } from '@pipeline/utils/useSaveTemplate' import type { Failure } from 'services/template-ng' import { DefaultNewTemplateId } from 'framework/Templates/templates' import { AppStoreContext } from 'framework/AppStore/AppStoreContext' import useCommentModal from '@common/hooks/CommentModal/useCommentModal' import { TemplateType } from '@templates-library/utils/templatesUtils' import css from './SaveTemplatePopover.module.scss' export interface GetErrorResponse extends Omit<Failure, 'errors'> { errors?: FormikErrors<unknown> } export interface SaveTemplatePopoverProps { getErrors?: () => Promise<GetErrorResponse> } export function SaveTemplatePopover(props: SaveTemplatePopoverProps): React.ReactElement { const { state: { template, yamlHandler, gitDetails, isUpdated, stableVersion, lastPublishedVersion }, setLoading, fetchTemplate, deleteTemplateCache, view, isReadonly } = React.useContext(TemplateContext) const { getString } = useStrings() const { getErrors } = props const { templateIdentifier } = useParams<TemplateStudioPathProps & ModulePathParams>() const [modalProps, setModalProps] = React.useState<ModalProps>() const [menuOpen, setMenuOpen] = React.useState(false) const [saveOptions, setSaveOptions] = React.useState<TemplateMenuItem[]>([]) const [disabled, setDisabled] = React.useState<boolean>(false) const { isGitSyncEnabled } = React.useContext(AppStoreContext) const { getComments } = useCommentModal() const [showConfigModal, hideConfigModal] = useModalHook( () => ( <Dialog enforceFocus={false} isOpen={true} className={css.configDialog}> {modalProps && ( <TemplateConfigModal initialValues={merge(template, { repo: defaultTo(gitDetails.repoIdentifier, ''), branch: defaultTo(gitDetails.branch, '') })} onClose={hideConfigModal} modalProps={modalProps} /> )} </Dialog> ), [template, modalProps] ) const { saveAndPublish } = useSaveTemplate({ template, yamlHandler, gitDetails, setLoading, fetchTemplate, deleteTemplateCache, view, stableVersion }) const checkErrors = React.useCallback( (callback: () => void) => { getErrors?.().then(response => { if (response.status === 'SUCCESS' && isEmpty(response.errors)) { callback() } }) }, [getErrors] ) const onSubmit = React.useCallback( (isEdit: boolean) => { checkErrors(async () => { try { const comment = !isGitSyncEnabled ? await getComments( getString('pipeline.commentModal.heading', { name: template.name, version: template.versionLabel }), stableVersion === template.versionLabel ? getString('pipeline.commentModal.info') : undefined ) : '' await saveAndPublish(template, { isEdit, comment }) } catch (_err) { // do nothing as user has cancelled the save operation } }) }, [checkErrors, isGitSyncEnabled, template, stableVersion, saveAndPublish] ) const onSave = React.useCallback(() => { onSubmit(false) }, [onSubmit]) const onUpdate = React.useCallback(() => { onSubmit(true) }, [onSubmit]) const onSaveAsNewLabel = React.useCallback(() => { checkErrors(() => { setModalProps({ title: getString('templatesLibrary.saveAsNewLabelModal.heading'), promise: saveAndPublish, disabledFields: [Fields.Name, Fields.Identifier, Fields.Description, Fields.Tags], emptyFields: [Fields.VersionLabel], shouldGetComment: !isGitSyncEnabled, lastPublishedVersion }) showConfigModal() }) }, [checkErrors, setModalProps, saveAndPublish]) const onSaveAsNewTemplate = React.useCallback(() => { checkErrors(() => { setModalProps({ title: getString('common.template.saveAsNewTemplateHeading'), promise: saveAndPublish, emptyFields: [Fields.Name, Fields.Identifier, Fields.VersionLabel], shouldGetComment: !isGitSyncEnabled }) showConfigModal() }) }, [checkErrors, setModalProps, saveAndPublish]) React.useEffect(() => { setDisabled(saveOptions.filter(item => !item.disabled).length === 0) }, [saveOptions]) React.useEffect(() => { setSaveOptions( templateIdentifier === DefaultNewTemplateId ? [ { label: getString('save'), disabled: isEmpty(get(template.spec, 'type')) && template.type !== TemplateType.Pipeline, onClick: onSave } ] : [ { label: getString('save'), disabled: !isUpdated || isReadonly, onClick: onUpdate }, { label: getString('templatesLibrary.saveAsNewLabelModal.heading'), onClick: onSaveAsNewLabel, disabled: isReadonly }, { label: getString('common.template.saveAsNewTemplateHeading'), onClick: onSaveAsNewTemplate, disabled: isReadonly } ] ) }, [templateIdentifier, template.spec, onSave, onUpdate, onSaveAsNewLabel, onSaveAsNewTemplate]) React.useEffect(() => { setMenuOpen(false) }, [isUpdated]) return ( <TemplatesActionPopover open={menuOpen && saveOptions.length > 1} disabled={disabled} items={saveOptions} setMenuOpen={setMenuOpen} minimal={true} > <Button disabled={disabled} variation={ButtonVariation.PRIMARY} rightIcon={saveOptions.length > 1 ? 'chevron-down' : undefined} text={getString('save')} onClick={saveOptions.length === 1 ? () => saveOptions[0].onClick() : noop} icon="send-data" /> </TemplatesActionPopover> ) } |