Skip to content

Commit

Permalink
🚸 [#4606] Present available role types in a dropdown
Browse files Browse the repository at this point in the history
Instead of having to enter the values in a text box, you can now
select them in a dropdown.
  • Loading branch information
sergei-maertens committed Nov 28, 2024
1 parent e61aeb4 commit 50879f0
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const OptionalOptionsFieldset = ({confidentialityLevelChoices, catalogueUrl}) =>
</div>
<OrganisationRSIN />
<ConfidentialityLevel options={confidentialityLevelChoices} />
<MedewerkerRoltype />
<MedewerkerRoltype catalogueUrl={catalogueUrl} />

<ErrorBoundary
errorMessage={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
mockCataloguesGetError,
mockDocumenTypesGet,
mockProductsGet,
mockRoleTypesGet,
} from './mocks';

const NAME = 'form.registrationBackends.0.options';
Expand Down Expand Up @@ -83,6 +84,7 @@ export default {
catalogues: [mockCataloguesGet()],
caseTypes: [mockCaseTypesGet()],
documentTypes: [mockDocumenTypesGet()],
roleTypes: [mockRoleTypesGet()],
products: [mockProductsGet()],
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,53 @@
import {useField} from 'formik';
import {useField, useFormikContext} from 'formik';
import PropTypes from 'prop-types';
import {FormattedMessage} from 'react-intl';
import useAsync from 'react-use/esm/useAsync';

import Field from 'components/admin/forms/Field';
import FormRow from 'components/admin/forms/FormRow';
import {TextInput} from 'components/admin/forms/Inputs';
import ReactSelect from 'components/admin/forms/ReactSelect';
import {get} from 'utils/fetch';

// Data fetching

const ROLE_TYPES_ENDPOINT = '/api/v2/registration/plugins/zgw-api/role-types';

const getAvailableRoleTypes = async (apiGroupID, catalogueUrl, caseTypeIdentification) => {
const response = await get(ROLE_TYPES_ENDPOINT, {
zgw_api_group: apiGroupID,
catalogue_url: catalogueUrl,
case_type_identification: caseTypeIdentification,
});
if (!response.ok) {
throw new Error('Loading available object types failed');
}
const roleTypes = response.data.sort((a, b) => a.description.localeCompare(b.description));
return roleTypes.map(({description, descriptionGeneric}) => ({
value: description,
label: description,
descriptionGeneric: descriptionGeneric, // omschrijvingGeneriek, which is an enum
}));
};

// Components

const MedewerkerRoltype = ({catalogueUrl = ''}) => {
const {
values: {zgwApiGroup = null, caseTypeIdentification = ''},
} = useFormikContext();
const [, , fieldHelpers] = useField('medewerkerRoltype');
const {setValue} = fieldHelpers;

const {
loading,
value: roleTypes = [],
error,
} = useAsync(async () => {
if (!zgwApiGroup || !catalogueUrl || !caseTypeIdentification) return [];
return await getAvailableRoleTypes(zgwApiGroup, catalogueUrl, caseTypeIdentification);
}, [zgwApiGroup, catalogueUrl, caseTypeIdentification]);
if (error) throw error;

const MedewerkerRoltype = () => {
const [fieldProps] = useField('medewerkerRoltype');
return (
<FormRow>
<Field
Expand All @@ -24,13 +65,26 @@ const MedewerkerRoltype = () => {
employees filling in a form for a citizen/company.`}
/>
}
noManageChildProps
>
<TextInput id="id_medewerkerRoltype" {...fieldProps} />
<ReactSelect
name="medewerkerRoltype"
options={roleTypes}
isLoading={loading}
isDisabled={!zgwApiGroup || !caseTypeIdentification}
required={false}
isClearable
onChange={selectedOption => {
setValue(selectedOption ? selectedOption.value : '');
}}
/>
</Field>
</FormRow>
);
};

MedewerkerRoltype.propTypes = {};
MedewerkerRoltype.propTypes = {
catalogueUrl: PropTypes.string,
};

export default MedewerkerRoltype;
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,23 @@ export const mockDocumenTypesGet = () =>
return HttpResponse.json(match);
});

// The backend must filter out the 'initiator' as there can only be one.
const ROLE_TYPES = [
{
description: 'Baliemedewerker',
descriptionGeneric: 'klantcontacter',
},
{
description: 'Belanghebbende',
descriptionGeneric: 'belanghebbende',
},
];

export const mockRoleTypesGet = () =>
http.get(`${API_BASE_URL}/api/v2/registration/plugins/zgw-api/role-types`, () =>
HttpResponse.json(ROLE_TYPES)
);

const PRODUCTS = [
{
url: 'https://example.com/product/1234',
Expand Down

0 comments on commit 50879f0

Please sign in to comment.