Skip to content

Commit

Permalink
Merge pull request #9072 from marmelab/8505-undoable-mutations-not-ex…
Browse files Browse the repository at this point in the history
…ecuted-if-user-closes-browser-tab-too-soon

Alert user if he close the window before the end of undoable notification
  • Loading branch information
fzaninotto committed Jul 5, 2023
2 parents eea0072 + 8628e3e commit 42f4d2e
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/ra-ui-materialui/src/layout/Notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ export const Notification = (props: NotificationProps) => {
const translate = useTranslate();

useEffect(() => {
const beforeunload = (e: BeforeUnloadEvent) => {
e.preventDefault();
const confirmationMessage = '';
e.returnValue = confirmationMessage;
return confirmationMessage;
};

if (messageInfo?.notificationOptions?.undoable) {
window.addEventListener('beforeunload', beforeunload);
}

if (notifications.length && !messageInfo) {
// Set a new snack when we don't have an active one
setMessageInfo(takeNotification());
Expand All @@ -51,6 +62,12 @@ export const Notification = (props: NotificationProps) => {
// Close an active snack when a new one is added
setOpen(false);
}

return () => {
if (messageInfo?.notificationOptions?.undoable) {
window.removeEventListener('beforeunload', beforeunload);
}
};
}, [notifications, messageInfo, open, takeNotification]);

const handleRequestClose = useCallback(() => {
Expand Down

0 comments on commit 42f4d2e

Please sign in to comment.