Skip to content

Commit 1388477

Browse files
committed
fix: warn when leaving page with selections
1 parent d30aaf9 commit 1388477

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

src/dashboard/Data/Browser/Browser.react.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,53 @@ import subscribeTo from 'lib/subscribeTo';
3838
import * as ColumnPreferences from 'lib/ColumnPreferences';
3939
import * as ClassPreferences from 'lib/ClassPreferences';
4040
import { Helmet } from 'react-helmet';
41-
import { unstable_usePrompt as usePrompt, useBeforeUnload } from 'react-router-dom';
41+
import {
42+
UNSAFE_NavigationContext,
43+
useBeforeUnload,
44+
} from 'react-router-dom';
4245
import generatePath from 'lib/generatePath';
4346
import { withRouter } from 'lib/withRouter';
4447
import { get } from 'lib/AJAX';
4548
import BrowserFooter from './BrowserFooter.react';
4649

50+
function useBlocker(blocker, when = true) {
51+
const { navigator } = React.useContext(UNSAFE_NavigationContext);
52+
53+
React.useEffect(() => {
54+
if (!when) {
55+
return;
56+
}
57+
58+
const unblock = navigator.block(tx => {
59+
const autoUnblockingTx = {
60+
...tx,
61+
retry() {
62+
unblock();
63+
tx.retry();
64+
}
65+
};
66+
blocker(autoUnblockingTx);
67+
});
68+
69+
return unblock;
70+
}, [navigator, blocker, when]);
71+
}
72+
73+
function usePrompt(message, when = true) {
74+
const blocker = React.useCallback(
75+
tx => {
76+
if (window.confirm(message)) {
77+
tx.retry();
78+
}
79+
},
80+
[message]
81+
);
82+
83+
useBlocker(blocker, when);
84+
}
85+
4786
function SelectedRowsNavigationPrompt({ when }) {
48-
usePrompt({
49-
when,
50-
message: 'There are selected rows. Are you sure you want to leave this page?'
51-
});
87+
usePrompt('There are selected rows. Are you sure you want to leave this page?', when);
5288
useBeforeUnload(
5389
React.useCallback(
5490
event => {

0 commit comments

Comments
 (0)