Skip to content

Commit

Permalink
geosolutions-it#10111: Enhance GeoFence attribute rule
Browse files Browse the repository at this point in the history
  • Loading branch information
dsuren1 committed Apr 2, 2024
1 parent bbfa7d7 commit 7063d89
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion web/client/api/geofence/RuleService.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const cleanConstraints = (rule) => {
let constraints = { ...rule.constraints };
constraints.allowedStyles = constraints.allowedStyles && constraints.allowedStyles.style || [];
constraints.attributes = constraints.attributes && constraints.attributes.attribute || [];
constraints.restrictedAreaWkt = constraints.restrictedAreaWkt || "";
if (constraints.restrictedAreaWkt) constraints.restrictedAreaWkt = constraints.restrictedAreaWkt;
return { ...rule, constraints };
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
* LICENSE file in the root directory of this source tree.
*/

import { castArray } from 'lodash';
import React from 'react';
import React, { useEffect } from 'react';
import { Col, Grid, Row } from 'react-bootstrap';
import castArray from 'lodash/castArray';
import isEmpty from 'lodash/isEmpty';

import Message from '../../../I18N/Message';
import Select from '../AttributeAccessSelect';
Expand All @@ -22,12 +23,22 @@ const getAttributeValue = (name, constraints) => {

export default ({attributes = [], constraints = {}, setOption = () => {}, active = false, setEditedAttributes = () => {}, editedAttributes = []}) => {
const onChange = (at) => {
const {attributes: attrs} = constraints;
const attribute = ((attrs && attrs?.attribute?.length) ? attrs.attribute : (attrs?.attribute) ? [attrs.attribute] : [] || []).filter(e => e.name !== at.name).concat(at);
let {attributes: {attribute = []} = {}} = constraints ?? {};
attribute = castArray(attribute).map(attr => at.name === attr.name ? at : attr);
setOption({key: "attributes", value: {attribute}});
// add it to edited attribute
if (!editedAttributes.includes(at.name)) setEditedAttributes(at.name);
};
useEffect(() => {
if (!isEmpty(attributes)) {
const _constraints = attributes.map(attr => ({name: attr.name, access: "READONLY"}));
const {attributes: {attribute = []} = {}} = constraints ?? {};
const modifiedAttribute = _constraints.map(attr => {
return castArray(attribute).find(a=> a.name === attr.name) ?? attr;
});
setOption({key: "attributes", value: {attribute: modifiedAttribute}});
}
}, [attributes]);
return (
<Grid className="ms-rule-editor" fluid style={{ width: '100%', display: active ? 'block' : 'none'}}>
<Row>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import React from 'react';

import ReactDOM from 'react-dom';
import expect from 'expect';
import TestUtils from "react-dom/test-utils";
import AttributesEditor from '../AttributesEditor.jsx';
const constraints = {
attributes: {
Expand Down Expand Up @@ -51,6 +52,54 @@ describe('Attributes Editor component', () => {
expect(rows).toExist();
expect(rows.length).toBe(3);
});
it('render attributes on setOption', (done) => {
TestUtils.act(() => {
ReactDOM.render(<AttributesEditor
setOption={(value) => {
try {
expect(value.key).toBe('attributes');
expect(value.value).toEqual({"attribute": [{"name": "the_geom", "access": "READONLY"}, {"access": "READONLY", "name": "cat"}]});
} catch (e) {
done(e);
}
done();
}}
attributes={attributes} active
constraints={constraints}
/>, document.getElementById("container"));
});
const container = document.getElementById('container');
const rows = container.querySelectorAll('.row');
expect(rows).toBeTruthy();
});
it('render attributes on change value', (done) => {
TestUtils.act(() => {
ReactDOM.render(<AttributesEditor
setOption={(value) => {
try {
const isModified = value.value?.attribute?.some(attr => attr.access === 'READWRITE');
if (isModified) {
expect(value.key).toBe('attributes');
expect(value.value).toEqual({"attribute": [{"name": "cat", "access": "READWRITE"}]});
}
} catch (e) {
done(e);
}
done();
}}
attributes={attributes} active
constraints={constraints}
/>, document.getElementById("container"));
});
const container = document.getElementById('container');
const rows = container.querySelectorAll('.row');
expect(rows).toBeTruthy();
const rule = document.querySelectorAll('.Select-control')[1];
expect(rule).toBeTruthy();
TestUtils.Simulate.mouseDown(rule, { button: 0 });
TestUtils.Simulate.keyDown(rule, { keyCode: 40, key: 'ArrowDown' });
TestUtils.Simulate.keyDown(rule, { key: 'Enter', keyCode: 13 });
});
it('render attributes with highlighted DD', () => {
ReactDOM.render(<AttributesEditor editedAttributes={["cat"]} attributes={attributes} active constraints={constraints} />, document.getElementById("container"));
const container = document.getElementById('container');
Expand Down

0 comments on commit 7063d89

Please sign in to comment.