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

Fix #2812 parseInitialState function #2818

Merged
merged 6 commits into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 12 additions & 2 deletions web/client/components/app/StandardApp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const PluginsUtils = require('../../utils/PluginsUtils');

const assign = require('object-assign');
const url = require('url');
const {isObject} = require('lodash');
const {isObject, isArray} = require('lodash');

const urlQuery = url.parse(window.location.href, true).query;

Expand Down Expand Up @@ -120,9 +120,19 @@ class StandardApp extends React.Component {
this.store.dispatch(action());
});
};
/**
* A function used to parse the initialState in the localConfig.json
* If an array of simple values is parsed then just return their values, otherwise return the recursive func
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't return the recursive func:

It returns an object of the same structure of the initialState but replacing strings like "{someExpression}" with the result of the expression between brackets.
state the object to parse
context context for expression
return the modified object.

* @param {object} state the piece of state to be parsed
* @param {object} context contains the mode to be used
* @return {object} the object parsed.
*/
parseInitialState = (state, context) => {
return Object.keys(state || {}).reduce((previous, key) => {
return { ...previous, ...{ [key]: isObject(state[key]) ? this.parseInitialState(state[key], context) :
return { ...previous, ...{ [key]: isObject(state[key]) ?
(isArray(state[key]) ? state[key].map(s => {
return isObject(s) ? this.parseInitialState(s, context) : s;
}) : this.parseInitialState(state[key], context)) :
PluginsUtils.handleExpression({}, context, state[key])}};
}, {});
};
Expand Down
35 changes: 35 additions & 0 deletions web/client/components/app/__tests__/StandardApp-test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ describe('StandardApp', () => {
const app = ReactDOM.render(<StandardApp appStore={store} storeOpts={storeOpts}/>, document.getElementById("container"));
expect(app).toExist();
});

it('creates a default app and reads initialState with mode', (done) => {
const store = (plugins, storeOpts) => {
expect(storeOpts.initialState.defaultState.testMode).toBe('EXPRESSION_MODE_TEST');
Expand All @@ -129,6 +130,40 @@ describe('StandardApp', () => {
expect(app).toExist();
});

it('test the parseInitialState func', (done) => {
const store = (plugins, storeOpts) => {
expect(storeOpts.initialState.defaultState.test).toExist();
done();
return {
dispatch() {
}
};
};

const valueArr1 = "valueArr1";
const valueArr2 = "valueArr2";
const innerObjTestValue = "innerObjTestValue";
const storeOpts = {
initialState: {
defaultState: {
test: "test",
withArray: [valueArr1],
withArrayObj: [valueArr2, {
innerObjTest: innerObjTestValue
}]
},
mobile: {}
}
};
const app = ReactDOM.render(<StandardApp appStore={store} storeOpts={storeOpts}/>, document.getElementById("container"));
expect(app).toExist();
const parsedInitialState = app.parseInitialState(storeOpts.initialState, {});
expect(parsedInitialState.defaultState.withArray.length).toBe(1);
expect(parsedInitialState.defaultState.withArray[0]).toBe(valueArr1);
expect(parsedInitialState.defaultState.withArrayObj.length).toBe(2);
expect(parsedInitialState.defaultState.withArrayObj[0]).toBe(valueArr2);
expect(parsedInitialState.defaultState.withArrayObj[1].innerObjTest).toBe(innerObjTestValue);
});

it('creates a default app and renders the given component', () => {
const store = () => ({
Expand Down
6 changes: 3 additions & 3 deletions web/client/utils/FeatureGridUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* LICENSE file in the root directory of this source tree.
*/

const { get, findIndex, isNil, fill} = require('lodash');
const { get, findIndex, isNil, fill, isArray} = require('lodash');

const {getFeatureTypeProperties, isGeometryType, isValid, isValidValueForPropertyName, findGeometryProperty, getPropertyDesciptor} = require('./ogc/WFS/base');
const getGeometryName = (describe) => get(findGeometryProperty(describe), "name");
Expand Down Expand Up @@ -35,13 +35,13 @@ const getRow = (i, rows) => rows[i];

/* eslint-disable */

const toChangesMap = (changesArray) => changesArray.reduce((changes, c) => ({
const toChangesMap = (changesArray) => isArray(changesArray) ? changesArray.reduce((changes, c) => ({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the reason of this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because moving the reducer default state of featuregrid caused that changesArray to passed as empty object initially, this makes the map loading forever

Copy link
Member

@offtherailz offtherailz Apr 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may use a (changesArray = []) default that seems to be more clear.
Did you changed the initial state? It should be anyway consistent.

...changes,
[c.id]: {
...changes[c.id],
...c.updated
}
}), {});
}), {}) : {};
const applyChanges = (feature, changes) => {
const propChanges = Object.keys(changes).filter(k => k !== "geometry").reduce((acc, cur) => ({
...acc,
Expand Down