Skip to content

Commit c1fc776

Browse files
committed
[#4267] Add Catalog select component
1 parent ec19451 commit c1fc776

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

src/openforms/js/components/admin/form_design/constants.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export const FORM_DEFINITIONS_ENDPOINT = '/api/v2/form-definitions';
33
export const REGISTRATION_BACKENDS_ENDPOINT = '/api/v2/registration/plugins';
44
export const REGISTRATION_OBJECTTYPES_ENDPOINT =
55
'/api/v2/registration/plugins/objects-api/object-types';
6+
export const REGISTRATION_CATALOGUS_ENDPOINT = '/api/v2/registration/catalogus';
67
export const REGISTRATION_OBJECTS_TARGET_PATHS =
78
'/api/v2/registration/plugins/objects-api/target-paths';
89
export const AUTH_PLUGINS_ENDPOINT = '/api/v2/authentication/plugins';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import {produce} from 'immer';
2+
import PropTypes from 'prop-types';
3+
import React, {useEffect} from 'react';
4+
5+
import ReactSelect from 'components/admin/forms/ReactSelect';
6+
7+
const CatalogSelect = ({
8+
name,
9+
htmlId,
10+
availableCatalogsState,
11+
catalogDomein,
12+
catalogRsin,
13+
onChange,
14+
isClearable = true,
15+
}) => {
16+
const {loading, availableCatalogs, error} = availableCatalogsState;
17+
18+
const onCatalogChange = event => {
19+
const {name, value} = event.target;
20+
const splitValue = value.split('-');
21+
onChange(
22+
produce(formData, draft => {
23+
draft.catalogDomein = splitValue[0];
24+
draft.catalogRsin = splitValue[1];
25+
})
26+
);
27+
};
28+
29+
const options =
30+
loading || error
31+
? []
32+
: availableCatalogs.map(({domein, rsin}) => {
33+
return {value: `${domein}-${rsin}`, label: `${domein} (RSIN: ${rsin})`};
34+
});
35+
36+
// ensure that if no valid value is present, the first possible option is set (
37+
// synchronize the UI state back to the form state)
38+
useEffect(() => {
39+
// do nothing if no options have been loaded
40+
if (loading || !availableCatalogs.length) return;
41+
42+
// check if a valid option is selected, if this is the case -> do nothing
43+
const isOptionPresent = availableCatalogs.find(
44+
catalog => catalog.domein === catalogDomein && catalog.rsin === catalogRsin
45+
);
46+
if (isOptionPresent) return;
47+
48+
// otherwise select the first possible option and persist that back into the state
49+
onChange(
50+
produce(formData, draft => {
51+
draft.catalogDomein = availableCatalogs[0].domein;
52+
draft.catalogRsin = availableCatalogs[0].rsin;
53+
})
54+
);
55+
}, []);
56+
57+
return (
58+
<ReactSelect
59+
name={name}
60+
value={`${catalogDomein}-${catalogRsin}`}
61+
options={options}
62+
htmlId={htmlId}
63+
onChange={onCatalogChange}
64+
emptyValue=""
65+
isClearable={isClearable}
66+
/>
67+
);
68+
};
69+
70+
CatalogSelect.propTypes = {
71+
name: PropTypes.string.isRequired,
72+
availableCatalogsState: PropTypes.object.isRequired,
73+
catalogDomein: PropTypes.string.isRequired,
74+
catalogRsin: PropTypes.string.isRequired,
75+
onChange: PropTypes.func.isRequired,
76+
isClearable: PropTypes.bool,
77+
};
78+
79+
export default CatalogSelect;

src/openforms/js/components/admin/form_design/registrations/objectsapi/V2ConfigFields.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ import ErrorMessage from 'components/errors/ErrorMessage';
1313
import InformatieObjecttypeSelect from '../InformatieObjecttypeSelect';
1414
import ObjectTypeSelect from './ObjectTypeSelect';
1515
import ObjectTypeVersionSelect from './ObjectTypeVersionSelect';
16-
import {useGetAvailableInformatieObjecttypen, useGetAvailableObjectTypes} from './hooks';
16+
import {
17+
useGetAvailableCatalogs,
18+
useGetAvailableInformatieObjecttypen,
19+
useGetAvailableObjectTypes,
20+
} from './hooks';
1721
import {getErrorMarkup, getFieldErrors, getOptionsFromSchema} from './utils';
1822

1923
const V2ConfigFields = ({index, name, schema, formData, onFieldChange, onChange}) => {
@@ -35,6 +39,7 @@ const V2ConfigFields = ({index, name, schema, formData, onFieldChange, onChange}
3539
// Track available object types and versions in this component so the state can be
3640
// shared.
3741
const availableObjectTypesState = useGetAvailableObjectTypes(objectsApiGroup);
42+
const availableCatalogsState = useGetAvailableCatalogs(objectsApiGroup);
3843
const availableInformatieObjecttypenState = useGetAvailableInformatieObjecttypen(objectsApiGroup);
3944

4045
const buildErrorsComponent = field => {

src/openforms/js/components/admin/form_design/registrations/objectsapi/hooks.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import useAsync from 'react-use/esm/useAsync';
22

3-
import {REGISTRATION_OBJECTTYPES_ENDPOINT} from 'components/admin/form_design/constants';
3+
import {
4+
REGISTRATION_CATALOGUS_ENDPOINT,
5+
REGISTRATION_OBJECTTYPES_ENDPOINT,
6+
} from 'components/admin/form_design/constants';
47
import {getInformatieObjectTypen} from 'components/form/file';
58
import {get} from 'utils/fetch';
69

@@ -27,6 +30,29 @@ export const useGetAvailableObjectTypes = objectsApiGroup => {
2730
};
2831
};
2932

33+
export const useGetAvailableCatalogs = objectsApiGroup => {
34+
const {
35+
loading,
36+
value: availableCatalogs = [],
37+
error,
38+
} = useAsync(async () => {
39+
if (!objectsApiGroup) return [];
40+
const response = await get(REGISTRATION_CATALOGUS_ENDPOINT, {
41+
objects_api_group: objectsApiGroup,
42+
});
43+
if (!response.ok) {
44+
throw new Error('Loading available catalogs failed');
45+
}
46+
return response.data;
47+
}, [objectsApiGroup]);
48+
49+
return {
50+
loading,
51+
availableCatalogs,
52+
error,
53+
};
54+
};
55+
3056
export const useGetAvailableInformatieObjecttypen = objectsApiGroup => {
3157
const {
3258
loading,

0 commit comments

Comments
 (0)