Skip to content

Commit

Permalink
🚧 [#4019] Handle special case for files
Browse files Browse the repository at this point in the history
The variable for file components has type 'array', but if only one file can be uploaded we want to be able to map it to attributes of type 'string' in the JSON schema. If multiple files can be uploaded, then we map it to 'array' attributes
  • Loading branch information
SilviaAmAm committed Mar 21, 2024
1 parent 63ab6e5 commit a5dd623
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React, {useContext} from 'react';
import {FormattedMessage} from 'react-intl';
import {useAsync, useToggle} from 'react-use';

import {APIContext} from 'components/admin/form_design/Context';
import {APIContext, FormContext} from 'components/admin/form_design/Context';
import {REGISTRATION_OBJECTS_TARGET_PATHS} from 'components/admin/form_design/constants';
import Field from 'components/admin/forms/Field';
import Fieldset from 'components/admin/forms/Fieldset';
Expand Down Expand Up @@ -34,7 +34,7 @@ import {asJsonSchema} from './utils';
*/
const ObjectsApiVariableConfigurationEditor = ({variable}) => {
const {csrftoken} = useContext(APIContext);

const {components} = useContext(FormContext);
const [jsonSchemaVisible, toggleJsonSchemaVisible] = useToggle(false);
const {values: backendOptions, getFieldProps, setFieldValue} = useFormikContext();

Expand Down Expand Up @@ -73,7 +73,7 @@ const ObjectsApiVariableConfigurationEditor = ({variable}) => {
const response = await post(REGISTRATION_OBJECTS_TARGET_PATHS, csrftoken, {
objecttypeUrl: objecttype,
objecttypeVersion,
variableJsonSchema: asJsonSchema(variable),
variableJsonSchema: asJsonSchema(variable, components),
});

return response.data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,20 @@ const FORMAT_TYPE_MAP = {
/**
* Return a JSON Schema definition matching the provided variable.
* @param {Object} variable - The current variable
* @param {Object} components - The components available in the form. The key is the component key, the value is the
* component definition.
* @returns {Object} - The JSON Schema
*/
const asJsonSchema = variable => {
const asJsonSchema = (variable, components) => {
// Figure out if the component is a file component (special case)
const componentDefinition = components[variable.key];
if (componentDefinition && componentDefinition.type === 'file') {
// If it is, and it has multiple == True, then type is array
if (componentDefinition.multiple) return {type: 'array'};
// Otherwise it's string (URL of the document)
return {type: 'string', format: 'uri'};
}

if (VARIABLE_TYPE_MAP.hasOwnProperty(variable.dataType))
return {type: VARIABLE_TYPE_MAP[variable.dataType]};
return {
Expand Down

0 comments on commit a5dd623

Please sign in to comment.