Skip to content

Commit

Permalink
replace save and saveAs plugin with custom ones to improve the saving…
Browse files Browse the repository at this point in the history
… workflow
  • Loading branch information
allyoucanmap committed Oct 14, 2020
1 parent 50dc57e commit f93bdb2
Show file tree
Hide file tree
Showing 14 changed files with 847 additions and 10 deletions.
48 changes: 48 additions & 0 deletions geonode_mapstore_client/client/js/actions/gnresource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2020, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

export const RESOURCE_LOADING = 'GEONODE:RESOURCE_LOADING';
export const SET_RESOURCE = 'GEONODE:SET_RESOURCE';
export const RESOURCE_ERROR = 'GEONODE:RESOURCE_ERROR';
export const UPDATE_RESOURCE_PROPERTIES = 'GEONODE:UPDATE_RESOURCE_PROPERTIES';
export const SET_RESOURCE_TYPE = 'GEONODE:SET_RESOURCE_TYPE';

export function resourceLoading() {
return {
type: RESOURCE_LOADING
};
}

export function setResource(data) {
return {
type: SET_RESOURCE,
data
};
}

export function setResourceType(resourceType) {
return {
type: SET_RESOURCE_TYPE,
resourceType
};
}


export function resourceError(error) {
return {
type: RESOURCE_ERROR,
error
};
}

export function updateResourceProperties(properties) {
return {
type: UPDATE_RESOURCE_PROPERTIES,
properties
};
}
57 changes: 57 additions & 0 deletions geonode_mapstore_client/client/js/actions/gnsave.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2020, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

export const SAVING_RESOURCE = 'GEONODE:SAVING_RESOURCE';
export const SAVE_SUCCESS = 'GEONODE:SAVE_SUCCESS';
export const SAVE_ERROR = 'GEONODE:SAVE_ERROR';
export const CLEAR_SAVE = 'GEONODE:CLEAR_SAVE';
export const SAVE_CONTENT = 'GEONODE:SAVE_CONTENT';
export const UPDATE_RESOURCE_BEFORE_SAVE = 'GEONODE:UPDATE_RESOURCE_BEFORE_SAVE';

export function savingResource() {
return {
type: SAVING_RESOURCE
};
}

export function saveSuccess(success) {
return {
type: SAVE_SUCCESS,
success
};
}

export function saveError(error) {
return {
type: SAVE_ERROR,
error
};
}

export function clearSave(error) {
return {
type: CLEAR_SAVE,
error
};
}

export function saveContent(metadata, id) {
return {
type: SAVE_CONTENT,
metadata,
id
};
}

export function updateResourceBeforeSave(id) {
return {
type: UPDATE_RESOURCE_BEFORE_SAVE,
id
};
}

40 changes: 40 additions & 0 deletions geonode_mapstore_client/client/js/api/geonode/adapter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2020, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

import axios from '@mapstore/framework/libs/ajax';
import { getConfigProp } from '@mapstore/framework/utils/ConfigUtils';
import { parseDevHostname } from '@js/utils/APIUtils';

export const creatMapStoreMap = (body = {}) => {
const baseUrl = getConfigProp('genode_rest_api') || '/mapstore/rest/';
return axios.post(parseDevHostname(`${baseUrl}resources/`),
body,
{
timeout: 10000,
params: {
full: true
}
})
.then(({ data }) => data);
};

export const updateMapStoreMap = (id, body = {}) => {
const baseUrl = getConfigProp('genode_rest_api') || '/mapstore/rest/';
return axios.patch(parseDevHostname(`${baseUrl}resources/${id}/`),
body,
{
params: {
full: true
}
})
.then(({ data }) => data);
};

export default {
creatMapStoreMap
};
29 changes: 29 additions & 0 deletions geonode_mapstore_client/client/js/api/geonode/v2/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2020, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

import axios from '@mapstore/framework/libs/ajax';
import { parseDevHostname } from '@js/utils/APIUtils';

let endpoints = {
'base_resources': '/api/v2/base_resources'
};

const RESOURCES = 'base_resources';
// const GROUPS = 'groups';
// const LAYERS = 'layers';
// const MAPS = 'maps';
// const USERS = 'users';

export const getResourceByPk = (pk) => {
return axios.get(parseDevHostname(`${endpoints[RESOURCES]}/${pk}`))
.then(({ data }) => data.resource);
};

export default {
getResourceByPk
};
136 changes: 136 additions & 0 deletions geonode_mapstore_client/client/js/epics/gnsave.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* Copyright 2020, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

import { Observable } from 'rxjs';
import { mapSelector } from '@mapstore/framework/selectors/map';
import { layersSelector, groupsSelector } from '@mapstore/framework/selectors/layers';
import { backgroundListSelector } from '@mapstore/framework/selectors/backgroundselector';
import { mapOptionsToSaveSelector } from '@mapstore/framework/selectors/mapsave';
import { textSearchConfigSelector, bookmarkSearchConfigSelector } from '@mapstore/framework/selectors/searchconfig';
import { saveMapConfiguration } from '@mapstore/framework/utils/MapUtils';
import { getConfigProp } from '@mapstore/framework/utils/ConfigUtils';
import {
creatMapStoreMap,
updateMapStoreMap
} from '@js/api/geonode/adapter';
import {
SAVE_CONTENT,
UPDATE_RESOURCE_BEFORE_SAVE,
saveSuccess,
saveError,
savingResource
} from '@js/actions/gnsave';
import {
resourceLoading,
setResource,
resourceError,
updateResourceProperties
} from '@js/actions/gnresource';
import { getResourceByPk } from '@js/api/geonode/v2';

const SaveAPI = {
map: (state, metadata, id) => {
const map = mapSelector(state);
const layers = layersSelector(state);
const groups = groupsSelector(state);
const backgrounds = backgroundListSelector(state);
const textSearchConfig = textSearchConfigSelector(state);
const bookmarkSearchConfig = bookmarkSearchConfigSelector(state);
const additionalOptions = mapOptionsToSaveSelector(state);
const data = saveMapConfiguration(
map,
layers,
groups,
backgrounds,
textSearchConfig,
bookmarkSearchConfig,
additionalOptions
);
const name = metadata.name;
const description = metadata.description;
const thumbnail = metadata.thumbnail;
const body = {
name,
data,
attributes: [{
type: 'string',
name: 'title',
value: name,
label: 'Title'
},
{
type: 'string',
name: 'abstract',
value: description,
label: 'Abstract'
},
...(thumbnail
? [{
type: 'string',
name: 'thumbnail',
value: thumbnail,
label: 'Thumbnail'
}]
: [])
]
};
return id
? updateMapStoreMap(id, { ...body, id })
: creatMapStoreMap(body)
.then((response) => {
window.location.href = `${getConfigProp('geonode_url')}maps/${response.id}/edit`;
return response.data;
});
}
};

export const gnSaveContent = (action$, store) =>
action$.ofType(SAVE_CONTENT)
.switchMap((action) => {
const state = store.getState();
const contentType = state.gnresource?.type || 'map';
return Observable.defer(() => SaveAPI[contentType](state, action.metadata, action.id))
.switchMap((response) => {
return Observable.of(
saveSuccess(response),
updateResourceProperties({
'title': action.metadata.name,
'abstract': action.metadata.description,
'thumbnail_url': action.metadata.thumbnail
})
);
})
.catch((error) => {
return Observable.of(saveError(error.data || error.message));
})
.startWith(savingResource());
});

export const gnUpdateResource = (action$, store) =>
action$.ofType(UPDATE_RESOURCE_BEFORE_SAVE)
.switchMap((action) => {
const state = store.getState();
const currentResource = state.gnresource?.data || {};
if ( !action.id
|| currentResource.pk && action.id && currentResource.pk + '' === action.id + '') {
return Observable.empty();
}
return Observable.defer(() => getResourceByPk(action.id))
.switchMap((resource) => {
return Observable.of(setResource(resource));
})
.catch((error) => {
return Observable.of(resourceError(error.data || error.message));
})
.startWith(resourceLoading());
});

export default {
gnSaveContent,
gnUpdateResource
};
4 changes: 2 additions & 2 deletions geonode_mapstore_client/client/js/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ module.exports = {
NotificationsPlugin: require('../MapStore2/web/client/plugins/Notifications'),
FeatureEditorPlugin: require('../MapStore2/web/client/plugins/FeatureEditor').default,
QueryPanelPlugin: require('../MapStore2/web/client/plugins/QueryPanel'),
SavePlugin: require('../MapStore2/web/client/plugins/Save').default,
SaveAsPlugin: require('../MapStore2/web/client/plugins/SaveAs').default,
SavePlugin: require('@js/plugins/Save').default,
SaveAsPlugin: require('@js/plugins/SaveAs').default,
MetadataExplorerPlugin: require('../MapStore2/web/client/plugins/MetadataExplorer'),
GridContainerPlugin: require('../MapStore2/web/client/plugins/GridContainer'),
StyleEditorPlugin: require('../MapStore2/web/client/plugins/StyleEditor'),
Expand Down
Loading

0 comments on commit f93bdb2

Please sign in to comment.