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

#10236: Fix - Legend filter incompatibility with style editing #10742

Merged
merged 2 commits into from
Jan 13, 2025
Merged
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
24 changes: 4 additions & 20 deletions web/client/api/WMS.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import urlUtil from 'url';
import { isArray, castArray, get } from 'lodash';
import xml2js from 'xml2js';
import axios from '../libs/ajax';
import ConfigUtils, { getConfigProp } from '../utils/ConfigUtils';
import { getConfigProp } from '../utils/ConfigUtils';
import { getWMSBoundingBox } from '../utils/CoordinatesUtils';
import { isValidGetMapFormat, isValidGetFeatureInfoFormat } from '../utils/WMSUtils';
const capabilitiesCache = {};
Expand Down Expand Up @@ -321,30 +321,14 @@ export const getSupportedFormat = (url, includeGFIFormats = false) => {
.catch(() => includeGFIFormats ? { imageFormats: [], infoFormats: [] } : []);
};

let layerLegendJsonData = {};
export const getJsonWMSLegend = (url) => {
let request;

// enables caching of the JSON legend for a specified duration,
// while providing the possibility of re-fetching the legend data in case of external modifications
const cached = layerLegendJsonData[url];
if (cached && new Date().getTime() < cached.timestamp + (ConfigUtils.getConfigProp('cacheExpire') || 60) * 1000) {
request = () => Promise.resolve(cached.data);
} else {
request = () => axios.get(url).then((response) => {
return axios.get(url)
.then((response) => {
if (typeof response?.data === 'string' && response.data.includes("Exception")) {
throw new Error("Faild to get json legend");
}
layerLegendJsonData[url] = {
timestamp: new Date().getTime(),
data: response?.data?.Legend
};
return response?.data?.Legend || [];
});
}
return request().then((data) => data).catch(err => {
throw err;
});
}).catch(err => { throw err; });
};

const Api = {
Expand Down
9 changes: 8 additions & 1 deletion web/client/epics/__tests__/styleeditor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { testEpic } from './epicTestUtils';

import MockAdapter from 'axios-mock-adapter';
import axios from '../../libs/ajax';
import { INTERACTIVE_LEGEND_ID } from '../../utils/LegendUtils';

let mockAxios;

Expand Down Expand Up @@ -474,7 +475,11 @@ describe('Test styleeditor epics', () => {
name: 'layerName',
url: 'base/web/client/test-resources/geoserver/',
describeFeatureType: {},
style: 'test_style'
style: 'test_style',
layerFilter: {
filters: [{id: INTERACTIVE_LEGEND_ID, "test": "test"}]
},
enableInteractiveLegend: true
}
],
selected: [
Expand Down Expand Up @@ -503,6 +508,7 @@ describe('Test styleeditor epics', () => {
case UPDATE_SETTINGS_PARAMS:
const styleName = action.newParams.style.split('___');
expect(styleName[0]).toBe('style_title');
expect(action.newParams.layerFilter).toBeTruthy();
expect(action.update).toBe(true);
break;
case UPDATE_STATUS:
Expand Down Expand Up @@ -568,6 +574,7 @@ describe('Test styleeditor epics', () => {
case UPDATE_SETTINGS_PARAMS:
const styleName = action.newParams.style.split('___');
expect(styleName[0]).toBe(`${workspace}:style_title`);
expect(action.newParams.layerFilter).toBeFalsy();
expect(action.update).toBe(true);
break;
case UPDATE_STATUS:
Expand Down
7 changes: 5 additions & 2 deletions web/client/epics/styleeditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import { getSelectedLayer, layerSettingSelector } from '../selectors/layers';
import { generateTemporaryStyleId, generateStyleId, STYLE_OWNER_NAME, getNameParts, detectStyleCodeChanges } from '../utils/StyleEditorUtils';
import { updateStyleService } from '../api/StyleEditor';
import { getDefaultUrl } from '../utils/URLUtils';
import { resetLayerLegendFilter } from '../utils/FilterUtils';

/*
* Observable to get code of a style, it works only in edit status
Expand Down Expand Up @@ -528,6 +529,7 @@ export const createStyleEpic = (action$, store) =>
const format = formatStyleSelector(state);
const { title = '', _abstract = '' } = action.settings || {};
const { baseUrl = '' } = styleServiceSelector(state);
const layerFilter = resetLayerLegendFilter(layer, 'style', styleName);

const editorMetadata = {
msStyleJSON: null,
Expand Down Expand Up @@ -559,7 +561,7 @@ export const createStyleEpic = (action$, store) =>
)
.switchMap(() => Rx.Observable.of(
updateOptionsByOwner(STYLE_OWNER_NAME, [{}]),
updateSettingsParams({style: styleName || ''}, true),
updateSettingsParams({ ...(layerFilter && {layerFilter}), style: styleName || ''}, true),
updateStatus(''),
loadedStyle())
.merge(
Expand Down Expand Up @@ -637,7 +639,8 @@ export const updateStyleCodeEpic = (action$, store) =>
'layer',
{
_v_: Date.now(),
availableStyles
availableStyles,
styleVersion: `${styleName}-${Date.now()}`
}),
updateSettingsParams({
availableStyles
Expand Down
4 changes: 4 additions & 0 deletions web/client/plugins/TOC/components/StyleBasedWMSJsonLegend.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,15 @@ class StyleBasedWMSJsonLegend extends React.Component {
const prevLayerStyle = prevProps?.layer?.style;
const currentLayerStyle = this.props?.layer?.style;

const prevLayerStyleVersion = prevProps?.layer?.styleVersion;
const currLayerStyleVersion = this.props?.layer?.styleVersion;

const [prevFilter, currFilter] = [prevProps?.layer, this.props?.layer]
.map(_layer => getLayerFilterByLegendFormat(_layer, LEGEND_FORMAT.JSON));

// get the new json legend and rerender in case of change in style or layer filter
if (!isEqual(prevLayerStyle, currentLayerStyle)
|| !isEqual(prevLayerStyleVersion, currLayerStyleVersion)
|| !isEqual(prevFilter, currFilter)
|| !isEqual(prevProps.mapBbox, this.props.mapBbox)
) {
Expand Down
Loading