Revert "Gets rid of custom form reset function in favor of formik's resetForm in credential form"

This reverts commit 26fa94dc6f1f1c1306c32ba571eb2899e516b0a0.
This commit is contained in:
mabashian 2020-06-15 12:51:23 -04:00
parent 8aa6e9466f
commit 3c27e1ca12

View File

@ -12,9 +12,14 @@ import OrganizationLookup from '../../../components/Lookup/OrganizationLookup';
import { FormColumnLayout } from '../../../components/FormLayout';
import TypeInputsSubForm from './TypeInputsSubForm';
function CredentialFormFields({ i18n, credentialTypes, formik }) {
function CredentialFormFields({
i18n,
credentialTypes,
formik,
initialValues,
}) {
const [orgField, orgMeta, orgHelpers] = useField('organization');
const [credTypeField, credTypeMeta] = useField({
const [credTypeField, credTypeMeta, credTypeHelpers] = useField({
name: 'credential_type',
validate: required(i18n._(t`Select a value for this field`), i18n),
});
@ -29,6 +34,45 @@ function CredentialFormFields({ i18n, credentialTypes, formik }) {
})
.sort((a, b) => (a.label.toLowerCase() > b.label.toLowerCase() ? 1 : -1));
const resetSubFormFields = (newCredentialType, form) => {
const fields = credentialTypes[newCredentialType].inputs.fields || [];
fields.forEach(
({ ask_at_runtime, type, id, choices, default: defaultValue }) => {
if (
parseInt(newCredentialType, 10) === form.initialValues.credential_type
) {
form.setFieldValue(`inputs.${id}`, initialValues.inputs[id]);
if (ask_at_runtime) {
form.setFieldValue(
`passwordPrompts.${id}`,
initialValues.passwordPrompts[id]
);
}
} else {
switch (type) {
case 'string':
form.setFieldValue(`inputs.${id}`, defaultValue || '');
break;
case 'boolean':
form.setFieldValue(`inputs.${id}`, defaultValue || false);
break;
default:
break;
}
if (choices) {
form.setFieldValue(`inputs.${id}`, defaultValue);
}
if (ask_at_runtime) {
form.setFieldValue(`passwordPrompts.${id}`, false);
}
}
form.setFieldTouched(`inputs.${id}`, false);
}
);
};
return (
<>
<FormField
@ -76,16 +120,8 @@ function CredentialFormFields({ i18n, credentialTypes, formik }) {
...credentialTypeOptions,
]}
onChange={(event, value) => {
const { values, initialValues, resetForm } = formik;
resetForm({
values: {
...initialValues,
name: values.name,
description: values.description,
organization: values.organization,
credential_type: value,
},
});
credTypeHelpers.setValue(value);
resetSubFormFields(value, formik);
}}
/>
</FormGroup>
@ -171,6 +207,7 @@ function CredentialForm({
<FormColumnLayout>
<CredentialFormFields
formik={formik}
initialValues={initialValues}
credentialTypes={credentialTypes}
{...rest}
/>