Skip to content

Commit d30aaf9

Browse files
committed
feat: warn when leaving page with selected rows
1 parent 1c94668 commit d30aaf9

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,30 @@ 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';
4142
import generatePath from 'lib/generatePath';
4243
import { withRouter } from 'lib/withRouter';
4344
import { get } from 'lib/AJAX';
4445
import BrowserFooter from './BrowserFooter.react';
4546

47+
function SelectedRowsNavigationPrompt({ when }) {
48+
usePrompt({
49+
when,
50+
message: 'There are selected rows. Are you sure you want to leave this page?'
51+
});
52+
useBeforeUnload(
53+
React.useCallback(
54+
event => {
55+
if (when) {
56+
event.preventDefault();
57+
}
58+
},
59+
[when]
60+
)
61+
);
62+
return null;
63+
}
64+
4665
// The initial and max amount of rows fetched by lazy loading
4766
const BROWSER_LAST_LOCATION = 'brower_last_location';
4867

@@ -2446,6 +2465,9 @@ class Browser extends DashboardView {
24462465
<Helmet>
24472466
<title>{pageTitle}</title>
24482467
</Helmet>
2468+
<SelectedRowsNavigationPrompt
2469+
when={Object.keys(this.state.selection).length > 0}
2470+
/>
24492471
{browser}
24502472
{notification}
24512473
{extras}

0 commit comments

Comments
 (0)