Skip to content

Commit

Permalink
🎨 [#4606] Apply fixes from ZGW to Objects API
Browse files Browse the repository at this point in the history
* setValues takes a callback for previous values, so that we can avoid
  form data races/stale values
* removed unused import
* fixed the handling for setting the default API group, the ternary
  was not being evaluated as expected (uncovered via ZGW options and
  this applies the fix here too)
  • Loading branch information
sergei-maertens committed Aug 19, 2024
1 parent 9c4b1ec commit c4f36f4
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const ObjectsApiOptionsForm = ({index, name, label, schema, formData, onChange})
const {objectsApiGroup} = schema.properties;
const apiGroupChoices = getChoicesFromSchema(objectsApiGroup.enum, objectsApiGroup.enumNames);
const numErrors = filterErrors(name, validationErrors).length;
const defaultGroup = apiGroupChoices.length === 1 ? apiGroupChoices[0][0] : undefined;

return (
<Field name={name} label={label}>
Expand Down Expand Up @@ -76,10 +77,7 @@ const ObjectsApiOptionsForm = ({index, name, label, schema, formData, onChange})
initialValues={{
...formData,
// Ensure that if there's only one option, it is automatically selected.
objectsApiGroup:
formData.objectsApiGroup ?? apiGroupChoices.length === 1
? apiGroupChoices[0][0]
: undefined,
objectsApiGroup: formData.objectsApiGroup ?? defaultGroup,
}}
onSubmit={(values, actions) => {
onChange({formData: values});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import {expect, fn, userEvent, waitFor, within} from '@storybook/test';
import {Form, Formik} from 'formik';
import selectEvent from 'react-select-event';

import {ValidationErrorsDecorator} from 'components/admin/form_design/story-decorators';
import {FeatureFlagsDecorator} from 'components/admin/form_design/story-decorators';
import {
FeatureFlagsDecorator,
ValidationErrorsDecorator,
} from 'components/admin/form_design/story-decorators';

import ObjectsApiOptionsFormFields from './ObjectsApiOptionsFormFields';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Fieldset from 'components/admin/forms/Fieldset';
import ErrorBoundary from 'components/errors/ErrorBoundary';

import {
CatalogueSelect,
DocumentTypesFieldet,
LegacyDocumentTypesFieldet,
ObjectTypeSelect,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ import ReactSelect from 'components/admin/forms/ReactSelect';

const ObjectsAPIGroup = ({apiGroupChoices, onChangeCheck}) => {
const [{onChange: onChangeFormik, ...fieldProps}, , {setValue}] = useField('objectsApiGroup');
const {values, setValues} = useFormikContext();
const {setValues} = useFormikContext();
const {value} = fieldProps;

// reset the objecttype specific-configuration whenever the API group changes
useUpdateEffect(() => {
const newValues = {
...values,
setValues(prevValues => ({
...prevValues,
objecttype: '',
objecttypeVersion: undefined,
variablesMapping: [],
};
setValues(newValues);
}, [setValues, value]); // deliberately excluding values!
}));
}, [setValues, value]);

const options = apiGroupChoices.map(([value, label]) => ({value, label}));
return (
Expand Down

0 comments on commit c4f36f4

Please sign in to comment.