forked from geosolutions-it/geonode-mapstore-client
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace save and saveAs plugin with custom ones to improve the saving…
… workflow
- Loading branch information
1 parent
50dc57e
commit f93bdb2
Showing
14 changed files
with
847 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
geonode_mapstore_client/client/js/api/geonode/adapter/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.