Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested schema dropdown with dependency reference #1981

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion geonode_mapstore_client/client/MapStore2
Submodule MapStore2 updated 381 files
23 changes: 19 additions & 4 deletions geonode_mapstore_client/client/js/api/geonode/v2/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,34 @@ import {
RESOURCES,
getEndpointUrl
} from './constants';
import { isObject, isArray, castArray } from 'lodash';
import isObject from 'lodash/isObject';
import isArray from 'lodash/isArray';
import castArray from 'lodash/castArray';
import isEmpty from 'lodash/isEmpty';

const uiKeys = (entry) => Object.keys(entry).filter(propertyKey => propertyKey.indexOf('ui:') === 0);

const parseUiSchema = (properties) => {
return Object.keys(properties).reduce((acc, key) => {
const entry = properties[key];
const uiKeys = Object.keys(entry).filter(propertyKey => propertyKey.indexOf('ui:') === 0);
if (uiKeys.length) {
acc[key] = Object.fromEntries(uiKeys.map(uiKey => [uiKey, entry[uiKey]]));
const uiKeysRoot = uiKeys(entry);
if (uiKeysRoot.length) {
acc[key] = Object.fromEntries(uiKeysRoot.map(uiKey => [uiKey, entry[uiKey]]));
}
if (entry.type === 'array') {
const uiKeysNested = uiKeys(entry?.items);
if (uiKeysNested.length) {
acc[key] = Object.fromEntries(uiKeysNested.map(uiKey => [uiKey, entry?.items?.[uiKey]]));
}
}
if (entry.type === 'object') {
const nestedProperties = parseUiSchema(entry?.properties);
acc[key] = { ...acc[key], ...nestedProperties };
}
if (entry.type === 'array' && entry.items?.type === 'object') {
const nestedProperties = parseUiSchema(entry?.items?.properties);
acc[key] = { ...acc[key], ...(!isEmpty(nestedProperties) && {items: {...nestedProperties}}) };
}
return acc;
}, {});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import axios from '@mapstore/framework/libs/ajax';
import castArray from 'lodash/castArray';
import isEmpty from 'lodash/isEmpty';
import isString from 'lodash/isString';
import template from 'lodash/template';
import Autocomplete from '../Autocomplete';
import DefaultSchemaField from '@rjsf/core/lib/components/fields/SchemaField';
import useSchemaReference from './useSchemaReference';

function findProperty(name, properties) {
return Object.keys(properties || {}).some((key) => {
Expand Down Expand Up @@ -58,6 +60,7 @@ const SchemaField = (props) => {
(isSchemaItemObject && !isEmpty(schema?.items?.properties))
);
const isSingleSelect = schema?.type === 'object' && !isEmpty(schema?.properties);
const { referenceValue, referenceKey } = useSchemaReference({...props, isMultiSelect });

if (autocomplete && (isMultiSelect || isSingleSelect)) {
const {
Expand Down Expand Up @@ -87,7 +90,10 @@ const SchemaField = (props) => {
const autocompleteOptions = isString(autocomplete)
? { url: autocomplete }
: autocomplete;
const autocompleteUrl = autocompleteOptions?.url;
let autocompleteUrl = autocompleteOptions?.url;
if (referenceValue) {
autocompleteUrl = template(autocompleteUrl)({[referenceKey ?? 'id']: referenceValue });
}
const queryKey = autocompleteOptions?.queryKey || 'q';
const resultsKey = autocompleteOptions?.resultsKey || 'results';
const valueKey = autocompleteOptions?.valueKey || 'id';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {useEffect, useRef} from 'react';
import get from 'lodash/get';
import template from 'lodash/template';

export default ({
uiSchema,
idSchema,
onChange,
isMultiSelect,
formContext,
name
}) => {
const uiOptions = uiSchema?.['ui:options'];
const referenceValuePath = uiOptions?.['geonode-ui:referencevalue'];
const referenceKey = uiOptions?.['geonode-ui:referencekey'];

// Extract index from the ID schema
const match = idSchema.$id.match(/_(\d+)(_|$)/);
const index = match ? parseInt(match[1], 10) : null;
const referenceValue = referenceValuePath
? get(formContext, `metadata.${template(referenceValuePath)({'index': index})}`)
: null;
const prevReferenceValue = useRef(null);

const storeReferenceValue = (value) => {
prevReferenceValue.current = {
...prevReferenceValue.current, [name]: value
};
};

useEffect(() => {
// store the initial reference value
if (prevReferenceValue.current === null && referenceValuePath) {
storeReferenceValue(referenceValue);
}
}, []);

useEffect(()=> {
// to reset the form data when the parent field reference value changes
if (referenceValuePath && referenceValue !== prevReferenceValue.current?.[name]) {
storeReferenceValue(referenceValue);
onChange(isMultiSelect ? [] : {});
}
}, [referenceValuePath, referenceValue]);

return { referenceValue, referenceKey };
};
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ function MetadataEditor({
readonly={readOnly}
ref={initialize.current}
formContext={{
title: metadata?.title
title: metadata?.title,
metadata
}}
schema={schema}
widgets={widgets}
Expand Down