Skip to content

Commit

Permalink
✨ [#4608] More changes to bring into line with backend
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Aug 27, 2024
1 parent 9424c20 commit 5e67b5b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const VARIABLES = [
name: 'User defined',
key: 'userDefined',
source: 'user_defined',
prefillPlugin: 'objects',
prefillPlugin: 'objects_api',
prefillAttribute: '',
prefillIdentifierRole: 'main',
dataType: 'array',
Expand Down Expand Up @@ -134,7 +134,7 @@ export default {
{id: 'stuf-bg', label: 'StUF-BG'},
{id: 'haalcentraal', label: 'BRP Personen (HaalCentraal)'},
{
id: 'objects',
id: 'objects_api',
label: 'Objects API',
extra: {
apiGroups: [
Expand Down Expand Up @@ -162,7 +162,7 @@ export default {
{id: 'bsn', label: 'BSN'},
{id: 'verblijfsAdres.postcode', label: 'Verblijfsadres > Postcode'},
],
objects: [
objects_api: [
{id: 'street', label: 'Address > street'},
{id: 'firstName', label: 'First name'},
{id: 'lastName', label: 'Last name'},
Expand Down Expand Up @@ -584,7 +584,7 @@ export const ConfigurePrefillObjectsAPI = {
const pluginDropdown = await screen.findByLabelText('Plugin');
expect(pluginDropdown).toBeVisible();

await userEvent.selectOptions(pluginDropdown, 'objects');
await userEvent.selectOptions(pluginDropdown, 'objects_api');

const variableSelect = await screen.findByLabelText('Formuliervariabele');
await expect(variableSelect.value).toBe('formioComponent');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Formik, useField, useFormikContext} from 'formik';
import PropTypes from 'prop-types';
import React, {useContext, useEffect, useState} from 'react';
import {FormattedMessage, useIntl} from 'react-intl';
import useAsync from 'react-use/esm/useAsync';
import useUpdateEffect from 'react-use/esm/useUpdateEffect';

import {FormContext} from 'components/admin/form_design/Context';
Expand Down Expand Up @@ -33,26 +34,27 @@ const PrefillConfigurationForm = ({
},
errors,
}) => {
const [choices, setChoices] = useState([]);
// const [choices, setChoices] = useState([]);

// Load the possible prefill attributes
// XXX: this would benefit from client-side caching
const {
loading,
value = [],
error,
} = useAsync(async () => {
if (!plugin) return [];

// XXX: we're not using formik's initialErrors yet because it doesn't support arrays of
// error messages, which our backend *can* produce.
// Taken from https://react.dev/reference/react/useEffect#fetching-data-with-effects
useEffect(() => {
let ignore = false;
setChoices(null);
const endpoint = `/api/v2/prefill/plugins/${plugin}/attributes`;
// XXX: clean up error handling here at some point...
get(endpoint).then(response => {
if (!response.ok) throw response.data;
if (!ignore) setChoices(response.data.map(attribute => [attribute.id, attribute.label]));
});
return () => {
ignore = true;
};
}, []);
const response = await get(endpoint);
if (!response.ok) throw response.data;
return response.data.map(attribute => [attribute.id, attribute.label]);
}, [plugin]);

const prefillAttributes = choices || LOADING_OPTION;
// throw errors to the nearest error boundary
if (error) throw error;
const prefillAttributes = loading ? LOADING_OPTION : value;

return (
<Formik
Expand All @@ -70,7 +72,7 @@ const PrefillConfigurationForm = ({
>
{({handleSubmit, values}) => (
<>
{values.plugin === 'objects' ? (
{values.plugin === 'objects_api' ? (
<ObjectsAPIPrefillFields
prefillAttributes={prefillAttributes}
values={values}
Expand Down Expand Up @@ -207,8 +209,9 @@ const ObjectsAPIPrefillFields = ({prefillAttributes, values, errors}) => {
plugins: {availablePrefillPlugins},
} = useContext(FormContext);
const {setFieldValue} = useFormikContext();
const objectsPlugin = availablePrefillPlugins.find(elem => elem.id === 'objects');
const objectsPlugin = availablePrefillPlugins.find(elem => elem.id === 'objects_api');
const apiGroups = objectsPlugin.extra.apiGroups;
console.log(values);

const prefillAttributeLabel = intl.formatMessage({
description: 'Accessible label for prefill attribute dropdown',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const PrefillSummary = ({
plugin = '',
attribute = '',
identifierRole = 'main',
prefillOptions = {},
prefillOptions = undefined,
onChange = undefined,
errors = {},
}) => {
Expand Down

0 comments on commit 5e67b5b

Please sign in to comment.