Skip to content

Commit

Permalink
code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cphelefu committed Sep 13, 2024
1 parent b2688e1 commit cddb86a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export function AddLayerTree(props: AddLayerTreeProps): JSX.Element | null {
// this use effect acts like onComponentDidMount
useEffect(() => {
if (isInitialized) return;
// Log
logger.logTraceUseEffect('Add Layer Tree - startingSelectedItems', startingSelectedItems);

setSelectedItems(startingSelectedItems);
setDefaultSelectedItems(startingSelectedItems);

Expand All @@ -42,9 +45,16 @@ export function AddLayerTree(props: AddLayerTreeProps): JSX.Element | null {
}, [startingSelectedItems]);

useEffect(() => {
// Log
logger.logTraceUseEffect('Add Layer Tree - selectedItems ', selectedItems);
onSelectedItemsChange(selectedItems);
}, [selectedItems]);

/**
* Recursive function to render tree item. It renders the layer and its children.
* @param layer - the layer to render
* @param parentId - the parent id of the layer
*/
const renderTreeItem = function (layer: GroupLayerEntryConfig, parentId: string | null): JSX.Element {
const curLayerId = `${parentId ? `${parentId}/` : ''}${layer.layerId}`;
return (
Expand All @@ -57,6 +67,11 @@ export function AddLayerTree(props: AddLayerTreeProps): JSX.Element | null {
);
};

/**
* Get all children of a layer
* @param treeLayerId - the id of the layer
* @returns - the list of children of the layer
*/
const getLayerChildren = function (treeLayerId: string): string[] {
const result: string[] = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { CONST_LAYER_ENTRY_TYPES } from '@/geo/map/map-schema-types';
import { GroupLayerEntryConfig } from '@/api/config/types/map-schema-types';
import { AddLayerTree } from './add-layer-tree';
import { buildGeoLayerToAdd } from './add-new-layers-utils';
import { useAppDisplayLanguage } from '@/core/stores/store-interface-and-intial-values/app-state';

export function AddNewLayer(): JSX.Element {
// Log
Expand Down Expand Up @@ -60,6 +61,7 @@ export function AddNewLayer(): JSX.Element {
// get values from store
const mapId = useGeoViewMapId();
const { setDisplayState } = useLayerStoreActions();
const language = useAppDisplayLanguage();

const isMultiple = (): boolean =>
hasMetadata && (layerType === ESRI_DYNAMIC || layerType === WFS || layerType === WMS || layerType === GEOJSON);
Expand Down Expand Up @@ -185,7 +187,7 @@ export function AddNewLayer(): JSX.Element {

const populateLayerList = async (curlayerType: TypeGeoviewLayerType) => {
try {
const layersTree = await api.config.createMetadataLayerTree(layerURL, curlayerType, [], 'en');
const layersTree = await api.config.createMetadataLayerTree(layerURL, curlayerType, [], language);
setLayerList(layersTree as GroupLayerEntryConfig[]);
setHasMetadata(true);
return true;
Expand Down Expand Up @@ -215,18 +217,7 @@ export function AddNewLayer(): JSX.Element {
// wmsValidation();
// wfsValidation();
}
// else if (layerType === WFS) promise = wfsValidation();
// else if (layerType === OGC_FEATURE) promise = ogcFeatureValidation();
// else if (layerType === XYZ_TILES) promise = xyzValidation();
// else if (layerType === ESRI_DYNAMIC) promise = esriValidation(ESRI_DYNAMIC);
// else if (layerType === ESRI_FEATURE) promise = esriValidation(ESRI_FEATURE);
// else if (layerType === ESRI_IMAGE) promise = esriImageValidation();
// else if (layerType === GEOJSON) promise = geoJSONValidation();
// else if (layerType === GEOPACKAGE) promise = Promise.resolve(geoPackageValidation());
else if (layerType === GEOCORE) {
// promise = geocoreValidation();
// else if (layerType === CSV) promise = csvValidation();
}

// If we have a promise of a layer validation
if (promise) {
promise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type ListOfLayerEntry = {
entryType?: string;
listOfLayerEntryConfig?: ListOfLayerEntry[];
};

type GeoViewLayerToAdd = {
geoviewLayerId: string;
geoviewLayerName: TypeLocalizedString;
Expand All @@ -29,21 +30,28 @@ export const getLayerById = (layersList: GroupLayerEntryConfig[], layerId: strin
if (branchGroup.layerId === layerId) return branchGroup;

const layer = branchGroup.listOfLayerEntryConfig?.find((childLayer) => childLayer.layerId === layerId);

if (layer) return layer as GroupLayerEntryConfig;

if (branchGroup.listOfLayerEntryConfig) {
let foundLayer: GroupLayerEntryConfig | null = null;

for (let i = 0; i < branchGroup.listOfLayerEntryConfig.length; i++) {
const layer2 = branchGroup.listOfLayerEntryConfig[i] as GroupLayerEntryConfig;

if (layer2.listOfLayerEntryConfig) {
const name = searchBranchForLayer(layer2 as GroupLayerEntryConfig);

if (name) {
foundLayer = name;
break;
}
}
}

return foundLayer;
}

return null;
}

Expand Down Expand Up @@ -77,6 +85,7 @@ export const buildGeoLayerToAdd = function (inputProps: BuildGeoViewLayerInput):
treeRoot.entryType = 'group';
// eslint-disable-next-line no-param-reassign
treeRoot.layerName = createLocalizedString(getLayerNameById(layersList, treeRoot.layerId) ?? 'unknown');

if (!treeRoot.listOfLayerEntryConfig) {
// eslint-disable-next-line no-param-reassign
treeRoot.listOfLayerEntryConfig = [];
Expand All @@ -86,8 +95,10 @@ export const buildGeoLayerToAdd = function (inputProps: BuildGeoViewLayerInput):
}

if (treeRoot.listOfLayerEntryConfig) {

for (let i = 0; i < treeRoot.listOfLayerEntryConfig.length; i++) {
const layer = treeRoot.listOfLayerEntryConfig[i] as ListOfLayerEntry;

if (layer.listOfLayerEntryConfig) {
appendChildLayerNode(layer, layerId, parentLayerId);
}
Expand All @@ -97,12 +108,14 @@ export const buildGeoLayerToAdd = function (inputProps: BuildGeoViewLayerInput):

function addRootLayerNode(layerId: string) {
const exists = geoviewLayerConfig.listOfLayerEntryConfig.find((entry) => entry.layerId === layerId);

if (exists) return;
geoviewLayerConfig.listOfLayerEntryConfig.push({ layerId });
}

layerIdsToAdd.forEach((layerId) => {
const layerTokens = layerId.split('/');

layerTokens.forEach((layerToken, index) => {
if (index === 0) {
addRootLayerNode(layerToken);
Expand Down

0 comments on commit cddb86a

Please sign in to comment.