Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ConfigMapSecret -update form submission handlers and improve error handling in dry run #2350

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ export const AppNavigation = () => {
<Switch>
<Route
path={[
`${path}/:resourceType(${Object.values(EnvResourceType).join('|')})/:envId(\\d+)?`,
`${path}/:resourceType(${Object.values(EnvResourceType).join('|')})`,
`${path}/${URLS.APP_ENV_OVERRIDE_CONFIG}/:envId(\\d+)/:resourceType(${Object.values(EnvResourceType).join('|')})`,
]}
>
{({ match }) => (
<EnvConfigurationsNav
key={`env-configurations-nav-${match.params.envId}`}
key={`env-configurations-nav-${'envId' in match.params ? match.params.envId : ''}`}
envConfig={envConfig}
fetchEnvConfig={fetchEnvConfig}
environments={environments.map(({ environmentName, environmentId }) => ({
Expand Down
27 changes: 20 additions & 7 deletions src/Pages/Shared/ConfigMapSecret/ConfigMapSecretContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {
usePrompt,
checkIfPathIsMatching,
useUrlFilters,
UseFormErrorHandler,
UseFormSubmitHandler,
} from '@devtron-labs/devtron-fe-common-lib'

import { URLS } from '@Config/routes'
Expand Down Expand Up @@ -682,7 +684,7 @@ export const ConfigMapSecretContainer = ({
}
}

const onSubmit: ConfigMapSecretFormProps['onSubmit'] = async (data) => {
const onSubmit: UseFormSubmitHandler<ConfigMapSecretUseFormProps> = async (data) => {
const payloadData = getConfigMapSecretPayload(data)

if (isApprovalPolicyConfigured) {
Expand Down Expand Up @@ -739,7 +741,7 @@ export const ConfigMapSecretContainer = ({
}
}

const onError: ConfigMapSecretFormProps['onError'] = (errors) => {
const onError: UseFormErrorHandler<ConfigMapSecretUseFormProps> = (errors) => {
if (errors.currentData?.[0] === CONFIG_MAP_SECRET_NO_DATA_ERROR) {
ToastManager.showToast({
variant: ToastVariantType.error,
Expand All @@ -755,6 +757,19 @@ export const ConfigMapSecretContainer = ({
}
}

const onDryRunError: UseFormErrorHandler<ConfigMapSecretUseFormProps> = (errors) => {
const hasErrors = Object.keys(errors).some((key) => !!errors[key])
if (hasErrors) {
ToastManager.showToast({
variant: ToastVariantType.error,
description: 'Please resolve form errors before saving.',
})
}
}

const formSubmitHandler = handleSubmit(onSubmit, onError)
const dryRunSubmitHandler = handleSubmit(onSubmit, onDryRunError)

// CONFIG TOOLBAR POPUP MENU
const toolbarPopupConfig: ConfigToolbarProps['popupConfig'] = {
menuConfig: getConfigToolbarPopupConfig({
Expand Down Expand Up @@ -802,8 +817,7 @@ export const ConfigMapSecretContainer = ({
draftData={resolvedDraftData ?? draftData}
inheritedConfigMapSecretData={resolvedInheritedConfigMapSecretData ?? inheritedConfigMapSecretData}
id={id}
onError={onError}
onSubmit={onSubmit}
onSubmit={formSubmitHandler}
selectedProtectionViewTab={selectedProtectionViewTab}
updateCMSecret={updateCMSecret}
componentType={componentType}
Expand All @@ -826,8 +840,7 @@ export const ConfigMapSecretContainer = ({
isApprovalPolicyConfigured={isApprovalPolicyConfigured}
isSubmitting={isSubmitting}
disableDataTypeChange={isDeleteDisabled}
onSubmit={onSubmit}
onError={onError}
onSubmit={formSubmitHandler}
onCancel={onCancel}
areScopeVariablesResolving={resolvedScopeVariablesResLoading}
appChartRef={appChartRef}
Expand Down Expand Up @@ -886,7 +899,7 @@ export const ConfigMapSecretContainer = ({
handleChangeDryRunEditorMode={setDryRunEditorMode}
showCrudButtons={!showNoOverride}
isSubmitting={isSubmitting}
onSubmit={onSubmit}
onSubmit={dryRunSubmitHandler}
parentName={parentName}
updateCMSecret={updateCMSecret}
formData={resolvedFormData ?? formData}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,6 @@ export const ConfigMapSecretDryRun = ({
isDryRunDataPresent && !hideManifest,
)

// METHODS
const handleSubmit = () => onSubmit(formData)

// RENDERERS
const renderLHSContent = () => {
if (publishedVersionDoesNotExist) {
Expand Down Expand Up @@ -264,7 +261,7 @@ export const ConfigMapSecretDryRun = ({
dataTestId="cm-secret-form-submit-btn"
text={`Save${!isCreateView ? ' Changes' : ''}${isApprovalPolicyConfigured ? '...' : ''}`}
size={ComponentSizeType.medium}
onClick={handleSubmit}
onClick={onSubmit}
isLoading={isSubmitting}
disabled={isSaveButtonDisabled}
/>
Expand Down
5 changes: 2 additions & 3 deletions src/Pages/Shared/ConfigMapSecret/ConfigMapSecretForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,13 @@ export const ConfigMapSecretForm = ({
areScopeVariablesResolving,
useFormProps,
onSubmit,
onError,
onCancel,
}: ConfigMapSecretFormProps) => {
// HOOKS
const location = useLocation()

// FORM PROPS
const { data, errors, formState, setValue, register, handleSubmit } = useFormProps
const { data, errors, formState, setValue, register } = useFormProps

// CONSTANTS
const isCreateView = id === null
Expand Down Expand Up @@ -329,7 +328,7 @@ export const ConfigMapSecretForm = ({
dataTestId="cm-secret-form-submit-btn"
text={`Save${!isCreateView ? ' Changes' : ''}${isApprovalPolicyConfigured ? '...' : ''}`}
size={ComponentSizeType.medium}
onClick={handleSubmit(onSubmit, onError)}
onClick={onSubmit}
isLoading={isSubmitting}
disabled={isSubmitting || areScopeVariablesResolving || isFormDisabled}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export const ConfigMapSecretProtected = ({
parentName,
inheritedConfigMapSecretData,
areScopeVariablesResolving,
onError,
onSubmit,
updateCMSecret,
shouldMergeTemplateWithPatches,
Expand Down Expand Up @@ -222,7 +221,6 @@ export const ConfigMapSecretProtected = ({
disableDataTypeChange={disableDataTypeChange}
isSubmitting={false}
onCancel={noop}
onError={onError}
onSubmit={onSubmit}
areScopeVariablesResolving={areScopeVariablesResolving}
useFormProps={useFormProps}
Expand Down
6 changes: 1 addition & 5 deletions src/Pages/Shared/ConfigMapSecret/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
ProtectConfigTabsType,
SelectPickerOptionType,
useForm,
UseFormErrorHandler,
UseFormSubmitHandler,
AppEnvDeploymentConfigDTO,
DryRunEditorMode,
ConfigHeaderTabType,
Expand Down Expand Up @@ -156,8 +154,7 @@ export interface ConfigMapSecretFormProps
areScopeVariablesResolving: boolean
isDraft?: boolean
disableDataTypeChange: boolean
onSubmit: UseFormSubmitHandler<ConfigMapSecretUseFormProps>
onError: UseFormErrorHandler<ConfigMapSecretUseFormProps>
onSubmit: () => void
onCancel: () => void
useFormProps: ReturnType<typeof useForm<ConfigMapSecretUseFormProps>>
}
Expand Down Expand Up @@ -217,7 +214,6 @@ export type ConfigMapSecretProtectedProps = Pick<ConfigMapSecretContainerProps,
| 'isJob'
| 'disableDataTypeChange'
| 'id'
| 'onError'
| 'onSubmit'
| 'areScopeVariablesResolving'
| 'appChartRef'
Expand Down
Loading