Skip to content

Commit

Permalink
Merge branch 'mapviewer-resource' into mapviewer-resource-built
Browse files Browse the repository at this point in the history
  • Loading branch information
giohappy committed Mar 28, 2024
2 parents e3fb844 + c6115e5 commit 07d12ef
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 21 deletions.
11 changes: 6 additions & 5 deletions geonode_mapstore_client/client/js/api/geonode/v2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,11 @@ export const createGeoApp = (body) => {
.then(({ data }) => data.geoapp);
};

export const getGeoAppByPk = (pk) => {
export const getGeoAppByPk = (pk, includes = []) => {
return axios.get(parseDevHostname(`${endpoints[GEOAPPS]}/${pk}`), {
params: {
full: true,
include: ['data']
include: ['data', ...includes]
}
})
.then(({ data }) => data.geoapp);
Expand Down Expand Up @@ -631,14 +631,15 @@ export const updateMap = (id, body = {}) => {
/**
* Get a map configuration
* @memberof api.geonode.adapter
* @param {number|string} id resource id
* @param {number|string} pk resource id
* @param {string[]} includes property to be included in the response
* @return {promise} it returns an object with the success map object response
*/
export const getMapByPk = (pk) => {
export const getMapByPk = (pk, includes = []) => {
return axios.get(parseDevHostname(`${endpoints[MAPS]}/${pk}/`),
{
params: {
include: ['data']
include: ['data', ...includes]
}
})
.then(({ data }) => data?.map);
Expand Down
28 changes: 15 additions & 13 deletions geonode_mapstore_client/client/js/epics/gnresource.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import axios from '@mapstore/framework/libs/ajax';
import uuid from "uuid";
import url from "url";
import omit from 'lodash/omit';
import get from 'lodash/get';
import {
getNewMapConfiguration,
getNewGeoStoryConfig,
Expand All @@ -23,7 +24,6 @@ import {
getMapByPk,
getCompactPermissionsByPk,
setResourceThumbnail,
getLinkedResourcesByPk,
setLinkedResourcesByPk,
removeLinkedResourcesByPk
} from '@js/api/geonode/v2';
Expand Down Expand Up @@ -81,7 +81,8 @@ import {
ResourceTypes,
toMapStoreMapConfig,
parseStyleName,
getCataloguePath
getCataloguePath,
getResourceWithLinkedResources
} from '@js/utils/ResourceUtils';
import {
canAddResource,
Expand Down Expand Up @@ -203,28 +204,29 @@ const resourceTypes = {
resourceObservable: (pk, options) =>
Observable.defer(() => axios.all([
getNewMapConfiguration(),
getMapByPk(pk),
getLinkedResourcesByPk(pk)
.then(({ linked_to: linkedTo = [] } = {}) => {
const mapViewers = linkedTo.find(({ resource_type: resourceType }) => resourceType === ResourceTypes.VIEWER);
getMapByPk(pk, ['linked_resources'])
.then((_resource) => {
const resource = getResourceWithLinkedResources(_resource);
const mapViewers = get(resource, 'linkedResources.linkedTo', [])
.find(({ resource_type: type } = {}) => type === ResourceTypes.VIEWER);
return mapViewers?.pk
? axios.all([getGeoAppByPk(mapViewers?.pk), getLinkedResourcesByPk(mapViewers?.pk)])
: null;
? axios.all([{...resource}, getGeoAppByPk(mapViewers?.pk, ['linked_resources'])])
: Promise.resolve([{...resource}]);
})
.catch(() => null)
]))
.switchMap(([baseConfig, resource, mapViewerResources]) => {
const [mapViewerResource, viewerLinkedResource] = mapViewerResources ?? [];
.switchMap(([baseConfig, resource]) => {
const [mapResource, mapViewerResource] = resource ?? [];
const mapConfig = options.data
? options.data
: toMapStoreMapConfig(resource, baseConfig);
: toMapStoreMapConfig(mapResource, baseConfig);
return Observable.of(
configureMap(mapConfig),
setControlProperty('toolbar', 'expanded', false),
setContext(mapViewerResource ? mapViewerResource.data : null),
setResource(resource),
setResource(mapResource),
setResourceId(pk),
setMapViewerLinkedResource({resource: omit(mapViewerResource, ['data']), linkedResource: viewerLinkedResource}),
setMapViewerLinkedResource({...getResourceWithLinkedResources(omit(mapViewerResource, ['data']))}),
setResourcePathParameters({
...options?.params,
appPk: mapViewerResource?.pk,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ const ButtonLinkedResource = ({ hide, loading, labelId, showLoader, ...props})

const RemoveMapViewerButton = (props) => {
const resourceType = ResourceTypes.VIEWER;
const { linkedResources, ...mapViewerResource } = props.viewerLinkedResource ?? {};

// when map viewer has one association
const allowDelete = props.viewerLinkedResource?.linkedResource?.linked_by?.length === 1;
const allowDelete = linkedResources?.linkedBy?.length === 1;

const handleOnClick = () => {
const { resource: mapViewerResource } = props.viewerLinkedResource ?? {};
if (allowDelete) {
props.onDelete({resources: [mapViewerResource], resourceType});
} else {
Expand Down
12 changes: 12 additions & 0 deletions geonode_mapstore_client/client/js/utils/ResourceUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import uuid from 'uuid';
import url from 'url';
import isEmpty from 'lodash/isEmpty';
import omit from 'lodash/omit';
import { getConfigProp, convertFromLegacy, normalizeConfig } from '@mapstore/framework/utils/ConfigUtils';
import { getGeoNodeLocalConfig, parseDevHostname } from '@js/utils/APIUtils';
import { ProcessTypes, ProcessStatus } from '@js/utils/ResourceServiceUtils';
Expand Down Expand Up @@ -747,3 +748,14 @@ export const getCataloguePath = (path = '') => {
}
return path;
};

export const getResourceWithLinkedResources = (resource = {}) => {
let linkedResources = resource.linked_resources ?? {};
if (!isEmpty(linkedResources)) {
const linkedTo = linkedResources.linked_to ?? [];
const linkedBy = linkedResources.linked_by ?? [];
linkedResources = isEmpty(linkedTo) && isEmpty(linkedBy) ? {} : ({ linkedTo, linkedBy });
return { ...omit(resource, 'linked_resources'), linkedResources };
}
return resource;
};
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import {
FEATURE_INFO_FORMAT,
isDocumentExternalSource,
getDownloadUrlInfo,
getCataloguePath
getCataloguePath,
getResourceWithLinkedResources
} from '../ResourceUtils';

describe('Test Resource Utils', () => {
Expand Down Expand Up @@ -1039,4 +1040,12 @@ describe('Test Resource Utils', () => {
// reset value
set(window.__GEONODE_CONFIG__, cPath, prevValue);
});
it("getResourceWithLinkedResources", () => {
expect(getResourceWithLinkedResources({})).toEqual({});
expect(getResourceWithLinkedResources()).toEqual({});
expect(getResourceWithLinkedResources({pk: 1, linked_resources: {linked_to: ["1"], linked_by: ["1"]}}))
.toEqual({pk: 1, linkedResources: {linkedBy: ["1"], linkedTo: ["1"]}});
expect(getResourceWithLinkedResources({linked_resources: {linked_to: ["1"], linked_by: ["1"]}}))
.toEqual({linkedResources: {linkedBy: ["1"], linkedTo: ["1"]}});
});
});

0 comments on commit 07d12ef

Please # to comment.