Skip to content

Commit 5f14dbc

Browse files
authored
Merge pull request #10794 from marmelab/fix-consecutive-notifications
Fix only the last notification is displayed when triggering consecutive notifications
2 parents 927d369 + fd4e66c commit 5f14dbc

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as React from 'react';
2-
import { render, screen, waitFor } from '@testing-library/react';
2+
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
33

44
import {
5+
ConsecutiveNotifications,
56
ConsecutiveUndoable,
67
CustomNotificationWithAction,
78
} from './Notification.stories';
@@ -43,4 +44,13 @@ describe('<Notification />', () => {
4344
});
4445
expect(consoleLog).toHaveBeenCalledWith('Custom action');
4546
});
47+
it('should display consecutive notifications', async () => {
48+
const { container } = render(<ConsecutiveNotifications />);
49+
await screen.findByText('hello, world');
50+
// This line ensures the test fails without the fix
51+
await new Promise(resolve => setTimeout(resolve, 200));
52+
expect(screen.queryByText('goodbye, world')).toBeNull();
53+
fireEvent.click(container);
54+
await screen.findByText('goodbye, world');
55+
});
4656
});

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ const Wrapper = ({ children }) => (
2727
</NotificationContextProvider>
2828
);
2929

30-
const BasicNotification = () => {
30+
const BasicNotification = ({
31+
message = 'hello, world',
32+
}: {
33+
message?: string;
34+
}) => {
3135
const notify = useNotify();
3236
React.useEffect(() => {
33-
notify('hello, world');
34-
}, [notify]);
37+
notify(message);
38+
}, [message, notify]);
3539
return null;
3640
};
3741

@@ -243,3 +247,10 @@ export const CustomNotificationWithAction = () => (
243247
<CustomNotificationElementWithAction />
244248
</Wrapper>
245249
);
250+
251+
export const ConsecutiveNotifications = () => (
252+
<Wrapper>
253+
<BasicNotification />
254+
<BasicNotification message="goodbye, world" />
255+
</Wrapper>
256+
);

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,22 @@ export const Notification = (inProps: NotificationProps) => {
6969
setCurrentNotification(notification);
7070
setOpen(true);
7171
}
72-
} else if (notifications.length && currentNotification && open) {
73-
// Close an active snack when a new one is added
74-
setOpen(false);
7572
}
7673

77-
const beforeunload = (e: BeforeUnloadEvent) => {
78-
e.preventDefault();
79-
const confirmationMessage = '';
80-
e.returnValue = confirmationMessage;
81-
return confirmationMessage;
82-
};
83-
84-
if (currentNotification?.notificationOptions?.undoable) {
85-
window.addEventListener('beforeunload', beforeunload);
86-
return () => {
87-
window.removeEventListener('beforeunload', beforeunload);
74+
if (currentNotification) {
75+
const beforeunload = (e: BeforeUnloadEvent) => {
76+
e.preventDefault();
77+
const confirmationMessage = '';
78+
e.returnValue = confirmationMessage;
79+
return confirmationMessage;
8880
};
81+
82+
if (currentNotification?.notificationOptions?.undoable) {
83+
window.addEventListener('beforeunload', beforeunload);
84+
return () => {
85+
window.removeEventListener('beforeunload', beforeunload);
86+
};
87+
}
8988
}
9089
}, [notifications, currentNotification, open, takeNotification]);
9190

0 commit comments

Comments
 (0)