From 24a4236232672702c2704e9636da9bbddaf81b78 Mon Sep 17 00:00:00 2001 From: mabashian Date: Thu, 24 Sep 2020 17:07:58 -0400 Subject: [PATCH 1/2] Wrap onChange functions passed to lookups in useCallback since these functions are eventually passed to a hook and used in a dependency array. --- .../InstanceGroup/shared/ContainerGroupForm.jsx | 16 +++++++++++----- .../shared/NotificationTemplateForm.jsx | 16 +++++++++++----- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/awx/ui_next/src/screens/InstanceGroup/shared/ContainerGroupForm.jsx b/awx/ui_next/src/screens/InstanceGroup/shared/ContainerGroupForm.jsx index 472e1b4ca2..9b55685a6d 100644 --- a/awx/ui_next/src/screens/InstanceGroup/shared/ContainerGroupForm.jsx +++ b/awx/ui_next/src/screens/InstanceGroup/shared/ContainerGroupForm.jsx @@ -1,6 +1,6 @@ -import React from 'react'; +import React, { useCallback } from 'react'; import { func, shape } from 'prop-types'; -import { Formik, useField } from 'formik'; +import { Formik, useField, useFormikContext } from 'formik'; import { withI18n } from '@lingui/react'; import { t } from '@lingui/macro'; import { Form, FormGroup } from '@patternfly/react-core'; @@ -22,11 +22,19 @@ import CredentialLookup from '../../../components/Lookup/CredentialLookup'; import { VariablesField } from '../../../components/CodeMirrorInput'; function ContainerGroupFormFields({ i18n }) { + const { setFieldValue } = useFormikContext(); const [credentialField, credentialMeta, credentialHelpers] = useField( 'credential' ); const [overrideField] = useField('override'); + const onCredentialChange = useCallback( + value => { + setFieldValue('credential', value); + }, + [setFieldValue] + ); + return ( <> credentialHelpers.setTouched()} - onChange={value => { - credentialHelpers.setValue(value); - }} + onChange={onCredentialChange} value={credentialField.value} required tooltip={i18n._( diff --git a/awx/ui_next/src/screens/NotificationTemplate/shared/NotificationTemplateForm.jsx b/awx/ui_next/src/screens/NotificationTemplate/shared/NotificationTemplateForm.jsx index bc668d7686..cba5534748 100644 --- a/awx/ui_next/src/screens/NotificationTemplate/shared/NotificationTemplateForm.jsx +++ b/awx/ui_next/src/screens/NotificationTemplate/shared/NotificationTemplateForm.jsx @@ -1,6 +1,6 @@ -import React from 'react'; +import React, { useCallback } from 'react'; import { shape, func } from 'prop-types'; -import { Formik, useField } from 'formik'; +import { Formik, useField, useFormikContext } from 'formik'; import { withI18n } from '@lingui/react'; import { t } from '@lingui/macro'; import { Form, FormGroup } from '@patternfly/react-core'; @@ -17,12 +17,20 @@ import hasCustomMessages from './hasCustomMessages'; import typeFieldNames, { initialConfigValues } from './typeFieldNames'; function NotificationTemplateFormFields({ i18n, defaultMessages }) { + const { setFieldValue } = useFormikContext(); const [orgField, orgMeta, orgHelpers] = useField('organization'); const [typeField, typeMeta] = useField({ name: 'notification_type', validate: required(i18n._(t`Select a value for this field`), i18n), }); + const onOrganizationChange = useCallback( + value => { + setFieldValue('organization', value); + }, + [setFieldValue] + ); + return ( <> orgHelpers.setTouched()} - onChange={value => { - orgHelpers.setValue(value); - }} + onChange={onOrganizationChange} value={orgField.value} touched={orgMeta.touched} error={orgMeta.error} From 56ed2c6afa1d8d7576aec1d5b45b408cb246909b Mon Sep 17 00:00:00 2001 From: mabashian Date: Thu, 24 Sep 2020 17:26:33 -0400 Subject: [PATCH 2/2] Autopopulate credential on container group form and organization on notification template form --- .../src/screens/InstanceGroup/shared/ContainerGroupForm.jsx | 5 +++-- .../NotificationTemplate/shared/NotificationTemplateForm.jsx | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/awx/ui_next/src/screens/InstanceGroup/shared/ContainerGroupForm.jsx b/awx/ui_next/src/screens/InstanceGroup/shared/ContainerGroupForm.jsx index 9b55685a6d..e8d734806f 100644 --- a/awx/ui_next/src/screens/InstanceGroup/shared/ContainerGroupForm.jsx +++ b/awx/ui_next/src/screens/InstanceGroup/shared/ContainerGroupForm.jsx @@ -21,7 +21,7 @@ import { import CredentialLookup from '../../../components/Lookup/CredentialLookup'; import { VariablesField } from '../../../components/CodeMirrorInput'; -function ContainerGroupFormFields({ i18n }) { +function ContainerGroupFormFields({ i18n, instanceGroup }) { const { setFieldValue } = useFormikContext(); const [credentialField, credentialMeta, credentialHelpers] = useField( 'credential' @@ -57,6 +57,7 @@ function ContainerGroupFormFields({ i18n }) { tooltip={i18n._( t`Credential to authenticate with Kubernetes or OpenShift. Must be of type "Kubernetes/OpenShift API Bearer Token”.` )} + autoPopulate={!instanceGroup?.id} /> (
- + {submitError && }