|
| 1 | +import React, { FunctionComponent, useMemo } from 'react' |
| 2 | +import { |
| 3 | + BUILD_INFRA_BREADCRUMB, |
| 4 | + BuildInfraDescriptor, |
| 5 | + BuildInfraFooter, |
| 6 | + useBreadcrumb, |
| 7 | + BuildInfraConfigForm, |
| 8 | + useBuildInfraForm, |
| 9 | + DEFAULT_PROFILE_NAME, |
| 10 | + APIResponseHandler, |
| 11 | + BUILD_INFRA_TEXT, |
| 12 | +} from '@devtron-labs/devtron-fe-common-lib' |
| 13 | +import './styles.scss' |
| 14 | + |
| 15 | +const ProfileForm: FunctionComponent = () => { |
| 16 | + const { breadcrumbs } = useBreadcrumb(BUILD_INFRA_BREADCRUMB) |
| 17 | + const { |
| 18 | + isLoading, |
| 19 | + profileResponse, |
| 20 | + responseError, |
| 21 | + reloadRequest, |
| 22 | + profileInput, |
| 23 | + profileInputErrors, |
| 24 | + handleProfileInputChange, |
| 25 | + loadingActionRequest, |
| 26 | + handleSubmit, |
| 27 | + } = useBuildInfraForm({ name: DEFAULT_PROFILE_NAME, editProfile: true }) |
| 28 | + // Would use this to disable the save button, if all keys in profileInputErrors are null or undefined, then there are no errors |
| 29 | + // Empty string means isRequired but no need to show error below input field |
| 30 | + const formErrorCount = useMemo( |
| 31 | + () => |
| 32 | + Object.keys(profileInputErrors).filter( |
| 33 | + (item) => profileInputErrors[item] !== null && profileInputErrors[item] !== undefined, |
| 34 | + ).length, |
| 35 | + [profileInputErrors], |
| 36 | + ) |
| 37 | + |
| 38 | + const handleKeyDown = (event: React.KeyboardEvent<HTMLFormElement>) => { |
| 39 | + if (event.key === 'Enter') { |
| 40 | + event.preventDefault() |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + const showActionItems = !isLoading && !responseError && profileInput?.configurations |
| 45 | + |
| 46 | + return ( |
| 47 | + <form |
| 48 | + className="h-100 flexbox-col build-infra pl pr pt pb dc__content-space bcn-0" |
| 49 | + onKeyDown={handleKeyDown} |
| 50 | + onSubmit={handleSubmit} |
| 51 | + > |
| 52 | + <div className="flexbox-col dc__gap-24 pt pr pb pl h-100 dc__overflow-scroll"> |
| 53 | + <BuildInfraDescriptor breadCrumbs={breadcrumbs} /> |
| 54 | + |
| 55 | + <APIResponseHandler |
| 56 | + isLoading={isLoading} |
| 57 | + progressingProps={{ |
| 58 | + pageLoader: true, |
| 59 | + }} |
| 60 | + error={responseError} |
| 61 | + reloadProps={{ |
| 62 | + reload: reloadRequest, |
| 63 | + }} |
| 64 | + notFoundText={{ |
| 65 | + title: BUILD_INFRA_TEXT.PROFILE_NOT_FOUND.title, |
| 66 | + subTitle: BUILD_INFRA_TEXT.PROFILE_NOT_FOUND.subTitle, |
| 67 | + }} |
| 68 | + > |
| 69 | + <BuildInfraConfigForm |
| 70 | + profileInput={profileInput} |
| 71 | + profileInputErrors={profileInputErrors} |
| 72 | + handleProfileInputChange={handleProfileInputChange} |
| 73 | + isDefaultProfile |
| 74 | + unitsMap={profileResponse?.configurationUnits} |
| 75 | + /> |
| 76 | + </APIResponseHandler> |
| 77 | + </div> |
| 78 | + |
| 79 | + {showActionItems && ( |
| 80 | + <BuildInfraFooter disabled={formErrorCount !== 0} editProfile loading={loadingActionRequest} /> |
| 81 | + )} |
| 82 | + </form> |
| 83 | + ) |
| 84 | +} |
| 85 | + |
| 86 | +export default ProfileForm |
0 commit comments