Skip to content

Commit

Permalink
Merge pull request #3542 from open-formulieren/issue/3491-form-defini…
Browse files Browse the repository at this point in the history
…tions-fetching

⚡️ Disable pagination when requesting form definitions in the admin
  • Loading branch information
sergei-maertens authored Oct 24, 2023
2 parents dd45e7f + 91a4dd4 commit 7d0c223
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 37 deletions.
1 change: 1 addition & 0 deletions src/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,7 @@ paths:
required: true
tags:
- forms
- form-definitions
security:
- tokenAuth: []
- cookieAuth: []
Expand Down
6 changes: 6 additions & 0 deletions src/openforms/forms/api/filters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

from django.db.models import Q
from django.utils.translation import gettext_lazy as _

Expand Down Expand Up @@ -56,5 +58,9 @@ def filter_queryset(self, queryset):
_or |= q_expr

if _or:
warnings.warn(
"Using OR filters is deprecated and will be removed starting with Open Forms 3.0",
DeprecationWarning,
)
queryset = queryset.filter(_or)
return super().filter_queryset(queryset)
2 changes: 1 addition & 1 deletion src/openforms/forms/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def get_serializer_class(self):

@extend_schema(
summary=_("Retrieve form definition JSON schema"),
tags=["forms"],
tags=["forms", "form-definitions"],
responses=OpenApiTypes.OBJECT,
)
@action(methods=("GET",), detail=True)
Expand Down
1 change: 1 addition & 0 deletions src/openforms/js/components/admin/form_design/Context.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const FormContext = React.createContext({
components: {},
formSteps: [],
formDefinitions: [],
reusableFormDefinitionsLoaded: false,
formVariables: {},
registrationBackends: [],
plugins: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, {useContext, useState} from 'react';
import {FormattedMessage, useIntl} from 'react-intl';

import FAIcon from 'components/admin/FAIcon';
import Loader from 'components/admin/Loader';
import {SubmitAction} from 'components/admin/forms/ActionButton';
import Field from 'components/admin/forms/Field';
import FormRow from 'components/admin/forms/FormRow';
Expand Down Expand Up @@ -109,37 +110,43 @@ const NewStepFormDefinitionPicker = ({onReplace}) => {
/>
}
>
<FormRow>
<Field
name="form-definition"
label={
<FormattedMessage
description="Form definition select label"
defaultMessage="Select form definition"
/>
}
errors={validationErrors}
required
>
<Select
name="form-definition"
choices={formDefinitionChoices}
value={selectedFormDefinition}
onChange={onSelectChange}
allowBlank
/>
</Field>
</FormRow>
{formContext.reusableFormDefinitionsLoaded ? (
<>
<FormRow>
<Field
name="form-definition"
label={
<FormattedMessage
description="Form definition select label"
defaultMessage="Select form definition"
/>
}
errors={validationErrors}
required
>
<Select
name="form-definition"
choices={formDefinitionChoices}
value={selectedFormDefinition}
onChange={onSelectChange}
allowBlank
/>
</Field>
</FormRow>

<SubmitRow isDefault>
<SubmitAction
text={intl.formatMessage({
description: 'Form definition select confirm button',
defaultMessage: 'Confirm',
})}
onClick={onFormDefinitionConfirmed}
/>
</SubmitRow>
<SubmitRow isDefault>
<SubmitAction
text={intl.formatMessage({
description: 'Form definition select confirm button',
defaultMessage: 'Confirm',
})}
onClick={onFormDefinitionConfirmed}
/>
</SubmitRow>
</>
) : (
<Loader />
)}
</FormModal>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ const initialFormState = {
formSteps: [],
errors: {},
formDefinitions: [],
reusableFormDefinitionsLoaded: false,
availableRegistrationBackends: [],
availableAuthPlugins: [],
availablePrefillPlugins: [],
Expand Down Expand Up @@ -187,7 +188,7 @@ function reducer(draft, action) {
* Form-level actions
*/
case 'BACKEND_DATA_LOADED': {
const {formData, supportingData} = action.payload;
const {supportingData, formData} = action.payload;
const {form, selectedAuthPlugins, steps, variables, logicRules, priceRules} = formData;

for (const [stateVar, data] of Object.entries(supportingData)) {
Expand Down Expand Up @@ -246,6 +247,16 @@ function reducer(draft, action) {
);
break;
}
case 'REUSABLE_FORM_DEFINITIONS_LOADED': {
const reusableFormDefinitions = action.payload;
const formDefinitionsIds = draft.formDefinitions.map(fd => fd.uuid);
draft.formDefinitions = [
...draft.formDefinitions,
...reusableFormDefinitions.filter(fd => !formDefinitionsIds.includes(fd.uuid)),
];
draft.reusableFormDefinitionsLoaded = true;
break;
}
case 'ADD_REGISTRATION': {
const {key} = action.payload;
draft.form.registrationBackends.push({
Expand Down Expand Up @@ -953,18 +964,24 @@ const FormCreationForm = ({formUuid, formUrl, formHistoryUrl}) => {
const backendDataToLoad = [
{endpoint: LANGUAGE_INFO_ENDPOINT, stateVar: 'languageInfo'},
{endpoint: PAYMENT_PLUGINS_ENDPOINT, stateVar: 'availablePaymentBackends'},
{
endpoint: FORM_DEFINITIONS_ENDPOINT,
query: {is_reusable: true, used_in: formUuid || ''},
stateVar: 'formDefinitions',
},
{endpoint: REGISTRATION_BACKENDS_ENDPOINT, stateVar: 'availableRegistrationBackends'},
{endpoint: AUTH_PLUGINS_ENDPOINT, stateVar: 'availableAuthPlugins'},
{endpoint: CATEGORIES_ENDPOINT, stateVar: 'availableCategories'},
{endpoint: PREFILL_PLUGINS_ENDPOINT, stateVar: 'availablePrefillPlugins'},
{endpoint: STATIC_VARIABLES_ENDPOINT, stateVar: 'staticVariables'},
];

if (formUuid) {
// We only fetch FDs used in this form if it already exists, otherwise
// it will fetch all the FDs because the `used_in` query param will have no effect.
// Reusable FDs are fetched in the background afterwards to avoid long loading time.
backendDataToLoad.push({
endpoint: FORM_DEFINITIONS_ENDPOINT,
query: {used_in: formUuid},
stateVar: 'formDefinitions',
});
}

const {loading} = useAsync(async () => {
const promises = [loadFromBackend(backendDataToLoad), loadForm(formUuid)];

Expand All @@ -980,6 +997,20 @@ const FormCreationForm = ({formUuid, formUrl, formHistoryUrl}) => {
});
}, []);

useAsync(async () => {
// Waiting for the last dispatch to be done to avoid state race conditions.
if (!loading) {
const responses = await loadFromBackend([
{endpoint: FORM_DEFINITIONS_ENDPOINT, query: {is_reusable: true}},
]);
const [reusableFormDefinitions] = responses;
dispatch({
type: 'REUSABLE_FORM_DEFINITIONS_LOADED',
payload: reusableFormDefinitions,
});
}
}, [loading]);

/**
* Functions for handling events
*/
Expand Down Expand Up @@ -1181,6 +1212,7 @@ const FormCreationForm = ({formUuid, formUrl, formHistoryUrl}) => {
components: availableComponents,
formSteps: state.formSteps,
formDefinitions: state.formDefinitions,
reusableFormDefinitionsLoaded: state.reusableFormDefinitionsLoaded,
formVariables: state.formVariables,
staticVariables: state.staticVariables,
plugins: {
Expand Down

0 comments on commit 7d0c223

Please sign in to comment.