Skip to content

Commit 15ceb7a

Browse files
authored
Reset pending changes on viewer page change (#2070)
1 parent 7cf01f9 commit 15ceb7a

File tree

2 files changed

+44
-47
lines changed

2 files changed

+44
-47
lines changed

geonode_mapstore_client/client/js/epics/gnresource.js

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import uuid from "uuid";
1212
import url from "url";
1313
import get from 'lodash/get';
1414
import isNil from 'lodash/isNil';
15+
1516
import {
1617
getNewMapConfiguration,
1718
getNewGeoStoryConfig,
@@ -33,12 +34,13 @@ import {
3334
} from '@js/api/geonode/v2';
3435
import { configureMap } from '@mapstore/framework/actions/config';
3536
import { isMapInfoOpen } from '@mapstore/framework/selectors/mapInfo';
36-
import { getSelectedLayer } from '@mapstore/framework/selectors/layers';
3737
import { isLoggedIn, userSelector } from '@mapstore/framework/selectors/security';
3838
import {
3939
browseData,
4040
selectNode,
41-
showSettings
41+
showSettings,
42+
updateNode,
43+
hideSettings
4244
} from '@mapstore/framework/actions/layers';
4345
import {
4446
setSelectedResource,
@@ -64,7 +66,8 @@ import {
6466
setMapViewerLinkedResource,
6567
REQUEST_RESOURCE,
6668
resourceLoading,
67-
resourceError
69+
resourceError,
70+
setSelectedLayer
6871
} from '@js/actions/gnresource';
6972

7073
import {
@@ -94,7 +97,8 @@ import {
9497
canAddResource,
9598
getResourceData,
9699
getResourceId,
97-
getResourceThumbnail
100+
getResourceThumbnail,
101+
getSelectedLayer
98102
} from '@js/selectors/resource';
99103
import { updateAdditionalLayer } from '@mapstore/framework/actions/additionallayers';
100104
import { STYLE_OWNER_NAME } from '@mapstore/framework/utils/StyleEditorUtils';
@@ -425,15 +429,26 @@ const resourceTypes = {
425429
};
426430

427431
// collect all the reset action needed before changing a viewer
428-
const getResetActions = (isSameResource) => [
429-
resetControls(),
430-
...(!isSameResource ? [ resetResourceState() ] : []),
431-
setControlProperty('rightOverlay', 'enabled', false),
432-
setControlProperty(FIT_BOUNDS_CONTROL, 'geometry', null),
433-
// reset style editor state to avoid persistence service configuration in between resource pages
434-
initStyleService(),
435-
resetStyleEditor()
436-
];
432+
const getResetActions = (state, isSameResource) => {
433+
const initialResource = state?.gnresource?.initialResource;
434+
const initialLayer = initialResource && initialResource.resource_type === ResourceTypes.DATASET && resourceToLayerConfig(initialResource);
435+
return [
436+
resetControls(),
437+
...(!isSameResource
438+
? [ resetResourceState() ]
439+
: [
440+
...(initialResource ? [setResource(initialResource)] : []),
441+
...(initialLayer ? [setSelectedLayer(initialLayer), updateNode(initialLayer.layerId, 'layers', initialLayer)] : [])
442+
]
443+
),
444+
setControlProperty('rightOverlay', 'enabled', false),
445+
setControlProperty(FIT_BOUNDS_CONTROL, 'geometry', null),
446+
// reset style editor state to avoid persistence service configuration in between resource pages
447+
initStyleService(),
448+
resetStyleEditor(),
449+
hideSettings()
450+
];
451+
};
437452

438453
export const gnViewerRequestNewResourceConfig = (action$, store) =>
439454
action$.ofType(REQUEST_NEW_RESOURCE_CONFIG)
@@ -502,7 +517,7 @@ export const gnViewerRequestResourceConfig = (action$, store) =>
502517
const isSamePreviousResource = !resourceData?.['@ms-detail'] && resourceData?.pk === action.pk;
503518
return Observable.concat(
504519
Observable.of(
505-
...getResetActions(isSamePreviousResource),
520+
...getResetActions(state, isSamePreviousResource),
506521
loadingResourceConfig(true),
507522
setResourceType(action.resourceType),
508523
setResourcePathParameters(action?.options?.params)

geonode_mapstore_client/client/js/plugins/save/withPrompt.jsx

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,23 @@
1-
import React, { useRef, useEffect } from 'react';
1+
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import { getMessageById } from '@mapstore/framework/utils/LocaleUtils';
4-
import { Prompt } from 'react-router-dom';
53

4+
import PendingStatePrompt from '@mapstore/framework/plugins/ResourcesCatalog/containers/PendingStatePrompt';
65

76
export default (Component) => {
8-
const PromptComponent = (props, {messages}) => {
9-
const dirtyState = useRef();
10-
dirtyState.current = props.dirtyState;
11-
useEffect(() => {
12-
function onBeforeUnload(event) {
13-
if (dirtyState.current) {
14-
(event || window.event).returnValue = null;
15-
}
16-
}
17-
window.addEventListener('beforeunload', onBeforeUnload);
18-
return () => {
19-
window.removeEventListener('beforeunload', onBeforeUnload);
20-
};
21-
}, []);
22-
7+
const PromptComponent = (props) => {
238
return props.enabled
24-
? <><Component {...props}/>
25-
<Prompt
26-
when={!!props.dirtyState}
27-
message={(/* nextLocation, action */) => {
28-
const confirmed = window.confirm(getMessageById(messages, 'gnviewer.prompPendingChanges')); // eslint-disable-line no-alert
29-
// if confirm the path should be the next one
30-
if (confirmed) {
31-
return true;
32-
}
33-
window.history.back(); // to return back to previous path
34-
// currently it's not possible to replace the pathname
35-
// without side effect
36-
// such as reloading of the page
37-
return false;
38-
}}
9+
? <>
10+
<Component {...props}/>
11+
<PendingStatePrompt
12+
show={props.show}
13+
onCancel={props.onCancel}
14+
onConfirm={props.onConfirm}
15+
pendingState={!!props.dirtyState}
16+
titleId="resourcesCatalog.detailsPendingChangesTitle"
17+
descriptionId="resourcesCatalog.detailsPendingChangesDescription"
18+
cancelId="resourcesCatalog.detailsPendingChangesCancel"
19+
confirmId="resourcesCatalog.detailsPendingChangesConfirm"
20+
variant="danger"
3921
/>
4022
</>
4123
: null

0 commit comments

Comments
 (0)