Skip to content

Commit 42f4d2e

Browse files
authored
Merge pull request #9072 from marmelab/8505-undoable-mutations-not-executed-if-user-closes-browser-tab-too-soon
Alert user if he close the window before the end of undoable notification
2 parents eea0072 + 8628e3e commit 42f4d2e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

packages/ra-ui-materialui/src/layout/Notification.tsx

+17
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ export const Notification = (props: NotificationProps) => {
4343
const translate = useTranslate();
4444

4545
useEffect(() => {
46+
const beforeunload = (e: BeforeUnloadEvent) => {
47+
e.preventDefault();
48+
const confirmationMessage = '';
49+
e.returnValue = confirmationMessage;
50+
return confirmationMessage;
51+
};
52+
53+
if (messageInfo?.notificationOptions?.undoable) {
54+
window.addEventListener('beforeunload', beforeunload);
55+
}
56+
4657
if (notifications.length && !messageInfo) {
4758
// Set a new snack when we don't have an active one
4859
setMessageInfo(takeNotification());
@@ -51,6 +62,12 @@ export const Notification = (props: NotificationProps) => {
5162
// Close an active snack when a new one is added
5263
setOpen(false);
5364
}
65+
66+
return () => {
67+
if (messageInfo?.notificationOptions?.undoable) {
68+
window.removeEventListener('beforeunload', beforeunload);
69+
}
70+
};
5471
}, [notifications, messageInfo, open, takeNotification]);
5572

5673
const handleRequestClose = useCallback(() => {

0 commit comments

Comments
 (0)