|
| 1 | +import { CustomInput, Drawer, InfoColourBar, Progressing } from '@devtron-labs/devtron-fe-common-lib' |
| 2 | +import React, { useEffect, useRef, useState } from 'react' |
| 3 | +import { ReactComponent as Close } from '../../../../assets/icons/ic-cross.svg' |
| 4 | +import { ReactComponent as Warn } from '../../../../assets/icons/ic-warning.svg' |
| 5 | +import SourceUpdateResponseModal from './SourceUpdateResponseModal' |
| 6 | + |
| 7 | +export default function BulkSourceChange({ closePopup, responseList, changeBranch, loading, selectedAppCount }) { |
| 8 | + const sourceChangeDetailRef = useRef<HTMLDivElement>(null) |
| 9 | + |
| 10 | + const [showResponseModal, setShowResponseModal] = useState(false) |
| 11 | + const [inputError, setInputError] = useState('') |
| 12 | + const [branchName, setBranchName] = useState('') |
| 13 | + |
| 14 | + const closeBulkCIModal = (evt) => { |
| 15 | + closePopup(evt) |
| 16 | + } |
| 17 | + |
| 18 | + const escKeyPressHandler = (evt): void => { |
| 19 | + if (evt && evt.key === 'Escape' && typeof closePopup === 'function') { |
| 20 | + evt.preventDefault() |
| 21 | + closeBulkCIModal(evt) |
| 22 | + } |
| 23 | + } |
| 24 | + const outsideClickHandler = (evt): void => { |
| 25 | + if ( |
| 26 | + sourceChangeDetailRef.current && |
| 27 | + !sourceChangeDetailRef.current.contains(evt.target) && |
| 28 | + typeof closePopup === 'function' |
| 29 | + ) { |
| 30 | + closeBulkCIModal(evt) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + document.addEventListener('keydown', escKeyPressHandler) |
| 36 | + return (): void => { |
| 37 | + document.removeEventListener('keydown', escKeyPressHandler) |
| 38 | + } |
| 39 | + }, [escKeyPressHandler]) |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + document.addEventListener('click', outsideClickHandler) |
| 43 | + return (): void => { |
| 44 | + document.removeEventListener('click', outsideClickHandler) |
| 45 | + } |
| 46 | + }, [outsideClickHandler]) |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + setShowResponseModal(responseList.length > 0) |
| 50 | + }, [responseList]) |
| 51 | + |
| 52 | + const updateBranch = () => { |
| 53 | + if (branchName.length === 0) { |
| 54 | + setInputError('This is required') |
| 55 | + return |
| 56 | + } |
| 57 | + changeBranch(branchName) |
| 58 | + } |
| 59 | + |
| 60 | + const renderHeaderSection = (): JSX.Element | null => { |
| 61 | + return ( |
| 62 | + <div className="flex flex-align-center flex-justify dc__border-bottom bcn-0 pt-16 pr-20 pb-16 pl-20"> |
| 63 | + <h2 className="fs-16 fw-6 lh-1-43 m-0">Change branch for {selectedAppCount} applications</h2> |
| 64 | + <button |
| 65 | + type="button" |
| 66 | + className="dc__transparent flex icon-dim-24" |
| 67 | + disabled={loading} |
| 68 | + onClick={closeBulkCIModal} |
| 69 | + > |
| 70 | + <Close className="icon-dim-24" /> |
| 71 | + </button> |
| 72 | + </div> |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + const renderInfoBar = (): JSX.Element => { |
| 77 | + return ( |
| 78 | + <InfoColourBar |
| 79 | + message="Branch will be changed only for build pipelines with source type as ‘Branch Fixed’ or ‘Branch Regex’." |
| 80 | + classname="warn dc__no-border-radius dc__no-top-border dc__no-left-border dc__no-right-border" |
| 81 | + Icon={Warn} |
| 82 | + iconClass="warning-icon" |
| 83 | + /> |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + const checkInput = (): boolean => { |
| 88 | + return branchName === '' |
| 89 | + } |
| 90 | + |
| 91 | + const handleInputChange = (e): void => { |
| 92 | + setBranchName(e.target.value) |
| 93 | + setInputError('') |
| 94 | + } |
| 95 | + |
| 96 | + const renderForm = (): JSX.Element => { |
| 97 | + const isDisabled = checkInput() |
| 98 | + |
| 99 | + return ( |
| 100 | + <div className="p-20"> |
| 101 | + <div className="form__row"> |
| 102 | + <CustomInput |
| 103 | + labelClassName="dc__required-field" |
| 104 | + autoComplete="off" |
| 105 | + name="branch_name" |
| 106 | + disabled={false} |
| 107 | + value={branchName} |
| 108 | + error={inputError} |
| 109 | + onChange={handleInputChange} |
| 110 | + label="Change to branch" |
| 111 | + placeholder="Enter branch name" |
| 112 | + /> |
| 113 | + </div> |
| 114 | + <div className="flexbox"> |
| 115 | + <button |
| 116 | + data-testid="cancel_button" |
| 117 | + className="cta cancel h-36 lh-36" |
| 118 | + type="button" |
| 119 | + onClick={closeBulkCIModal} |
| 120 | + > |
| 121 | + Cancel |
| 122 | + </button> |
| 123 | + <button |
| 124 | + data-testid="save_cluster_after_entering_cluster_details" |
| 125 | + className="cta ml-12 h-36 lh-36" |
| 126 | + onClick={updateBranch} |
| 127 | + disabled={isDisabled} |
| 128 | + > |
| 129 | + {loading ? <Progressing /> : 'Update branch'} |
| 130 | + </button> |
| 131 | + </div> |
| 132 | + </div> |
| 133 | + ) |
| 134 | + } |
| 135 | + return ( |
| 136 | + <Drawer position="right" width="75%" minWidth={showResponseModal ? '1024px' : '600px'} maxWidth={showResponseModal ? '1200px' : '600px'}> |
| 137 | + <div className="dc__window-bg h-100 bcn-0 bulk-ci-trigger-container" ref={sourceChangeDetailRef}> |
| 138 | + {renderHeaderSection()} |
| 139 | + {showResponseModal ? ( |
| 140 | + <SourceUpdateResponseModal closePopup={closePopup} isLoading={false} responseList={responseList} /> |
| 141 | + ) : ( |
| 142 | + <> |
| 143 | + {renderInfoBar()} |
| 144 | + {renderForm()} |
| 145 | + </> |
| 146 | + )} |
| 147 | + </div> |
| 148 | + </Drawer> |
| 149 | + ) |
| 150 | +} |
0 commit comments