Skip to content

Commit

Permalink
form: added support for json fields
Browse files Browse the repository at this point in the history
  • Loading branch information
0einstein0 committed Jun 3, 2024
1 parent b059cbf commit d9cae25
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export class AdminForm extends Component {
if (defaultValue) {
return defaultValue;
}
if (value.type === "bool") {
return false;
}
if (value.type === "object") {
return null;
}
return "";
}),
};
Expand All @@ -37,17 +43,40 @@ export class AdminForm extends Component {
const { apiEndpoint, pid, successCallback, create } = this.props;
const { addNotification } = this.context;
let response;
// eslint-disable-next-line no-debugger
debugger;

const transformedValues = mapValues(values, (value, key) => {
const fieldSchema = this.props.resourceSchema[key];

Check warning on line 50 in invenio_administration/assets/semantic-ui/js/invenio_administration/src/formik/AdminForm.js

View workflow job for this annotation

GitHub Actions / tests (3.9, postgresql14, opensearch2, 18.x)

Must use destructuring props assignment

Check warning on line 50 in invenio_administration/assets/semantic-ui/js/invenio_administration/src/formik/AdminForm.js

View workflow job for this annotation

GitHub Actions / tests (3.9, postgresql14, opensearch2, 20.x)

Must use destructuring props assignment

Check warning on line 50 in invenio_administration/assets/semantic-ui/js/invenio_administration/src/formik/AdminForm.js

View workflow job for this annotation

GitHub Actions / tests (3.9, postgresql14, elasticsearch7, 18.x)

Must use destructuring props assignment

Check warning on line 50 in invenio_administration/assets/semantic-ui/js/invenio_administration/src/formik/AdminForm.js

View workflow job for this annotation

GitHub Actions / tests (3.9, postgresql14, elasticsearch7, 20.x)

Must use destructuring props assignment

Check warning on line 50 in invenio_administration/assets/semantic-ui/js/invenio_administration/src/formik/AdminForm.js

View workflow job for this annotation

GitHub Actions / tests (3.10, postgresql14, opensearch2, 18.x)

Must use destructuring props assignment

Check warning on line 50 in invenio_administration/assets/semantic-ui/js/invenio_administration/src/formik/AdminForm.js

View workflow job for this annotation

GitHub Actions / tests (3.10, postgresql14, opensearch2, 20.x)

Must use destructuring props assignment

Check warning on line 50 in invenio_administration/assets/semantic-ui/js/invenio_administration/src/formik/AdminForm.js

View workflow job for this annotation

GitHub Actions / tests (3.10, postgresql14, elasticsearch7, 18.x)

Must use destructuring props assignment

Check warning on line 50 in invenio_administration/assets/semantic-ui/js/invenio_administration/src/formik/AdminForm.js

View workflow job for this annotation

GitHub Actions / tests (3.10, postgresql14, elasticsearch7, 20.x)

Must use destructuring props assignment

Check warning on line 50 in invenio_administration/assets/semantic-ui/js/invenio_administration/src/formik/AdminForm.js

View workflow job for this annotation

GitHub Actions / tests (3.11, postgresql14, opensearch2, 18.x)

Must use destructuring props assignment

Check warning on line 50 in invenio_administration/assets/semantic-ui/js/invenio_administration/src/formik/AdminForm.js

View workflow job for this annotation

GitHub Actions / tests (3.11, postgresql14, opensearch2, 20.x)

Must use destructuring props assignment

Check warning on line 50 in invenio_administration/assets/semantic-ui/js/invenio_administration/src/formik/AdminForm.js

View workflow job for this annotation

GitHub Actions / tests (3.11, postgresql14, elasticsearch7, 18.x)

Must use destructuring props assignment

Check warning on line 50 in invenio_administration/assets/semantic-ui/js/invenio_administration/src/formik/AdminForm.js

View workflow job for this annotation

GitHub Actions / tests (3.11, postgresql14, elasticsearch7, 20.x)

Must use destructuring props assignment

Check warning on line 50 in invenio_administration/assets/semantic-ui/js/invenio_administration/src/formik/AdminForm.js

View workflow job for this annotation

GitHub Actions / tests (3.12, postgresql14, opensearch2, 18.x)

Must use destructuring props assignment

Check warning on line 50 in invenio_administration/assets/semantic-ui/js/invenio_administration/src/formik/AdminForm.js

View workflow job for this annotation

GitHub Actions / tests (3.12, postgresql14, opensearch2, 20.x)

Must use destructuring props assignment

Check warning on line 50 in invenio_administration/assets/semantic-ui/js/invenio_administration/src/formik/AdminForm.js

View workflow job for this annotation

GitHub Actions / tests (3.12, postgresql14, elasticsearch7, 18.x)

Must use destructuring props assignment

Check warning on line 50 in invenio_administration/assets/semantic-ui/js/invenio_administration/src/formik/AdminForm.js

View workflow job for this annotation

GitHub Actions / tests (3.12, postgresql14, elasticsearch7, 20.x)

Must use destructuring props assignment
if (fieldSchema?.metadata?.type === "json") {
try {
if (value === "") {
return null;
} else if (typeof value === "object") {
return value;
} else {
return JSON.parse(value);
}
} catch (e) {
console.error(`Error parsing JSON for field ${key}:`, e);
actions.setFieldError(key, "Invalid JSON format");
throw e;
}
}
return value;
});

try {
if (create) {
response = await InvenioAdministrationActionsApi.createResource(
apiEndpoint,
values
transformedValues
);
} else {
response = await InvenioAdministrationActionsApi.editResource(
apiEndpoint,
pid,
values
transformedValues
);
}
actions.setSubmitting(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const mapFormFields = (obj, parentField, isCreate, formFieldsConfig, dropDumpOnl

if (fieldSchema.type === "bool") {
const description = fieldProps.description;

return (
<>
<BooleanField
Expand All @@ -134,7 +135,7 @@ const mapFormFields = (obj, parentField, isCreate, formFieldsConfig, dropDumpOnl
);
}

if (fieldSchema.type === "object") {
if (fieldSchema.type === "object" && fieldSchema.metadata?.type !== "json") {
// nested fields
return (
<React.Fragment key={fieldProps.fieldPath}>
Expand Down Expand Up @@ -169,7 +170,10 @@ const mapFormFields = (obj, parentField, isCreate, formFieldsConfig, dropDumpOnl
}

const rows = formFieldsConfig[fieldName]?.rows || fieldSchema?.metadata?.rows;
if (fieldSchema.type === "string" && rows) {
if (
(fieldSchema.type === "string" && rows) ||
fieldSchema.metadata?.type === "json"
) {
return (
<TextArea
key={fieldProps.fieldPath}
Expand Down

0 comments on commit d9cae25

Please sign in to comment.