Skip to content

Commit 15be798

Browse files
committed
refact title i18n and add test
1 parent 98af27d commit 15be798

File tree

2 files changed

+46
-66
lines changed

2 files changed

+46
-66
lines changed

geonode_mapstore_client/client/js/epics/__tests__/gnsave-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,26 @@ describe('gnsave epics', () => {
257257
{layers: {flat: [{name: "testLayer", id: "test_id", perms: ['download_resourcebase']}], selected: ["test_id"]}});
258258
});
259259

260+
it('test gnSetDatasetsPermissions trigger updateNode for ADD_LAYER and set title in multilanguage', (done) => {
261+
mockAxios.onGet().reply(() => [200,
262+
{datasets: [{title: 'testLayer Default', title_en: 'testLayer EN', title_it: 'testLayerIT'}]}]);
263+
const NUM_ACTIONS = 1;
264+
testEpic(gnSetDatasetsPermissions, NUM_ACTIONS, addLayer({name: "testLayer", pk: "1", extendedParams: {pk: "1"}}), (actions) => {
265+
try {
266+
expect(actions.map(({type}) => type)).toEqual(["UPDATE_NODE"]);
267+
expect(actions[0].options.title).toEqual({
268+
default: 'testLayer Default',
269+
'en-US': 'testLayer EN',
270+
'it-IT': 'testLayerIT'
271+
});
272+
done();
273+
} catch (error) {
274+
done(error);
275+
}
276+
},
277+
{layers: {flat: [{name: "testLayer", id: "test_id", perms: ['download_resourcebase']}], selected: ["test_id"]}});
278+
});
279+
260280
it('should trigger saveResource (gnSaveDirectContent)', (done) => {
261281
const NUM_ACTIONS = 3;
262282
const pk = 1;

geonode_mapstore_client/client/js/epics/index.js

Lines changed: 26 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,22 @@ import { startAsyncProcess, STOP_ASYNC_PROCESS } from "@js/actions/resourceservi
2929
import { error as errorNotification } from "@mapstore/framework/actions/notifications";
3030
import { getProcessErrorInfo } from "@js/utils/ErrorUtils";
3131
import axios from '@mapstore/framework/libs/ajax';
32-
33-
import { getMetadataSchema } from '@js/api/geonode/v2/metadata';
32+
import { getSupportedLocales } from '@mapstore/framework/utils/LocaleUtils';
3433

3534
// We need to include missing epics. The plugins that normally include this epic is not used.
3635

36+
function localeCodeToBCP47(iso2) {
37+
const supportedLocales = getSupportedLocales();
38+
supportedLocales['hy'] = {
39+
code: 'hy-AM',
40+
description: 'Armenian'
41+
};
42+
supportedLocales['ru'] = {
43+
code: 'ru-RU',
44+
description: 'Russian'
45+
};
46+
return supportedLocales[iso2] ? supportedLocales[iso2].code : iso2;
47+
}
3748
/**
3849
* @module epics/index
3950
*/
@@ -100,7 +111,7 @@ export const gnFetchMissingLayerData = (action$, { getState } = {}) =>
100111

101112
/**
102113
* Checks the permissions for layers when a map is loaded and when a new layer is added
103-
* to a map
114+
* to a map and update layer in multi-language support for layer title in TOC
104115
*/
105116
export const gnSetDatasetsPermissions = (actions$, { getState = () => {}} = {}) =>
106117
actions$.ofType(MAP_CONFIG_LOADED, ADD_LAYER)
@@ -125,76 +136,25 @@ export const gnSetDatasetsPermissions = (actions$, { getState = () => {}} = {})
125136
// skip layers of non-geonode origin
126137
if (!action.layer?.extendedParams?.pk) return Rx.Observable.empty();
127138

128-
// return Rx.Observable.defer(() => getDatasetByName(action.layer?.name))
129-
// .switchMap((layer = {}) => {
130-
// const layerId = layersSelector(getState())?.find((la) => la.name === layer.alternate)?.id;
131-
// return Rx.Observable.of(updateNode(layerId, 'layer', {perms: layer.perms}));
132-
// });
133139
if (action.type === ADD_LAYER) {
134140

135141
return Rx.Observable.defer(() => getDatasetByName(action.layer?.name))
136142
.switchMap((layer = {}) => {
137143
const layerId = layersSelector(getState())?.find((la) => la.name === layer.alternate)?.id;
138144
const layerPk = layer?.pk;
139-
140-
//TODO get settings MULTILANG_FIELDS from settings.py or get METADATA for specific layer
141-
142-
// http://localhost/api/v2/metadata/instance/16
143-
// getMetadataByPk(layerPk)
144-
// .then(({ schema }) => {
145-
// //non contiene informazioni sulla multilingua della risorsa specifica
146-
// });
147145

148-
//TODO we need to preload metadata schema at boot ???
149-
return Rx.Observable.of(
150-
getMetadataSchema()
151-
.then(({ schema }) => {
152-
layer._schema = schema;
153-
const multilangFields = Object.keys(schema.properties).filter(prop =>
154-
schema.properties[prop]['geonode:multilang'] === true
155-
);
156-
//only for layer title used in the TOC
157-
if (multilangFields.includes('title')) {
158-
159-
//all alng codes for titles fields
160-
const titleLangCodes = Object.entries(schema.properties)
161-
.filter(([key, value]) => value['geonode:multilang-group'] === 'title')
162-
.reduce((acc, [key, value]) => {
163-
//return values of 'geonode:multilang-lang'
164-
const langCode = value['geonode:multilang-lang'];
165-
if (langCode) {
166-
acc.push(langCode);
167-
}
168-
return acc;
169-
}, []);
170-
171-
//build a structure like:
172-
// title: {
173-
// default: "geosolutions:label light",
174-
// "it-IT": "title_IT",
175-
// "en-US": "title_EN"
176-
// }
177-
const titleObjectMultilang = titleLangCodes.reduce((acc, langCode) => {
178-
const propKey = `title_${langCode}`;
179-
acc[langCode] = layer[propKey];
180-
return acc;
181-
}, { default: layer.title });
182-
183-
return updateNode(layerId, 'layer', {
184-
perms: layer.perms,
185-
//MS multilang node structure
186-
// "title": {
187-
// "default": "geosolutions:label light",
188-
// "it-IT": "title_IT",
189-
// "en-US": "title_EN"
190-
// },
191-
title: titleObjectMultilang
192-
});
193-
}
194-
})
195-
);
196-
//old code without multilang support
197-
//return Rx.Observable.of(updateNode(layerId, 'layer', {perms: layer.perms, titles}));
146+
/*
147+
* Multilang support for only layer title in TOC
148+
*/
149+
const title = Object.keys(layer)
150+
.filter(key => key.startsWith('title_'))
151+
.reduce((acc, key) => {
152+
const code = localeCodeToBCP47(key.replace('title_', ''));
153+
acc[code] = layer[key];
154+
return acc;
155+
}, {default: layer.title}) || layer.title;
156+
157+
return Rx.Observable.of(updateNode(layerId, 'layer', {perms: layer.perms, title}));
198158
});
199159
}
200160
});

0 commit comments

Comments
 (0)