From d21e0388c78dc7939cd53e9c511cf0781f6e633c Mon Sep 17 00:00:00 2001 From: Rohit Gautam Date: Thu, 10 Oct 2024 17:09:49 +0545 Subject: [PATCH] fix: convert old group to new standard format --- web/client/utils/LayersUtils.js | 51 +++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/web/client/utils/LayersUtils.js b/web/client/utils/LayersUtils.js index f80ad6b938..e5b13b87a0 100644 --- a/web/client/utils/LayersUtils.js +++ b/web/client/utils/LayersUtils.js @@ -23,6 +23,9 @@ import { addAuthenticationParameter } from './SecurityUtils'; import { getEPSGCode } from './CoordinatesUtils'; import { ANNOTATIONS, updateAnnotationsLayer, isAnnotationLayer } from '../plugins/Annotations/utils/AnnotationsUtils'; import { getLocale } from './LocaleUtils'; +import { isSingleDefaultGroup } from '../plugins/TOC/utils/TOCUtils'; +import uuid from 'uuid'; + let LayersUtils; @@ -573,6 +576,52 @@ export const sortUsing = (sortFun, action) => { return action(node, reorder, sortFun); }; }; +/** + * Converts an array of legacy groups to a new standardized format. + * + * @param {Array} legecyGroups - An array of legacy group objects. + * + * @returns {Array} - An array containing a single default group object with modified nodes. + * + * @example + * const legacyGroups = [ + * { id: '1', name: 'Group 1' }, + * { id: DEFAULT_GROUP_ID } + * ]; + * + * const newFormat = convertLegecyGroupsToNewFormat(legacyGroups); + * // returns [ + * // { + * // id: "Default", + * // name: "Default", + * // nodes: [ + * // { id: '1', name: 'Group 1' }, + * // { id: 'DEFAULT_GROUP_ID.' } // Unique UUID generated + * // ], + * // expanded: true + * // } + * // ] + */ +function convertLegecyGroupsToNewFormat(legecyGroups) { + const modifiedNodes = legecyGroups.map(node => { + const id = uuid(); + if (node.id === DEFAULT_GROUP_ID) { + return { + ...node, + id: `${DEFAULT_GROUP_ID}.${id}`, + name: id + }; + } + return node; + }); + + return [{ + id: "Default", + name: "Default", + nodes: modifiedNodes, + expanded: true + }]; +} export const splitMapAndLayers = (mapState) => { if (mapState && isArray(mapState.layers)) { let groups = LayersUtils.getLayersByGroup(mapState.layers, mapState.groups); @@ -598,6 +647,8 @@ export const splitMapAndLayers = (mapState) => { return newGroups; }, [].concat(groups)); } + // to convert old format to new format: UI wise invisible default group as parent of all groups + groups = !isSingleDefaultGroup(groups) ? convertLegecyGroupsToNewFormat(groups) : groups; let layers = extractDataFromSources(mapState);