Skip to content

Commit 21c9e30

Browse files
authored
refactor: change toast component (#1211)
1 parent 8ae9dfb commit 21c9e30

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

src/generic/processing-notification/index.jsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import { Badge, Icon } from '@openedx/paragon';
55
import { Settings as IconSettings } from '@openedx/paragon/icons';
66
import { capitalize } from 'lodash';
77

8-
import { NOTIFICATION_MESSAGES } from '../../constants';
9-
108
const ProcessingNotification = ({ isShow, title }) => (
119
<Badge
1210
className={classNames('processing-notification', {
@@ -24,7 +22,7 @@ const ProcessingNotification = ({ isShow, title }) => (
2422

2523
ProcessingNotification.propTypes = {
2624
isShow: PropTypes.bool.isRequired,
27-
title: PropTypes.oneOf(Object.values(NOTIFICATION_MESSAGES)).isRequired,
25+
title: PropTypes.string.isRequired,
2826
};
2927

3028
export default ProcessingNotification;

src/generic/toast-context/index.test.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ describe('<ToastProvider />', () => {
5050
},
5151
});
5252
store = initializeStore();
53+
jest.useFakeTimers();
5354
});
5455

5556
afterEach(() => {
@@ -61,6 +62,13 @@ describe('<ToastProvider />', () => {
6162
expect(await screen.findByText('This is the toast!')).toBeInTheDocument();
6263
});
6364

65+
it('should close toast after 5000ms', async () => {
66+
render(<RootWrapper><TestComponentToShow /></RootWrapper>);
67+
expect(await screen.findByText('This is the toast!')).toBeInTheDocument();
68+
jest.advanceTimersByTime(6000);
69+
expect(screen.queryByText('This is the toast!')).not.toBeInTheDocument();
70+
});
71+
6472
it('should close toast', async () => {
6573
render(<RootWrapper><TestComponentToClose /></RootWrapper>);
6674
expect(await screen.findByText('Content')).toBeInTheDocument();

src/generic/toast-context/index.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
2-
import { Toast } from '@openedx/paragon';
2+
3+
import ProcessingNotification from '../processing-notification';
34

45
export interface ToastContextData {
56
toastMessage: string | null;
@@ -35,7 +36,13 @@ export const ToastProvider = (props: ToastProviderProps) => {
3536
setToastMessage(null);
3637
}, []);
3738

38-
const showToast = React.useCallback((message) => setToastMessage(message), [setToastMessage]);
39+
const showToast = React.useCallback((message) => {
40+
setToastMessage(message);
41+
// Close the toast after 5 seconds
42+
setTimeout(() => {
43+
setToastMessage(null);
44+
}, 5000);
45+
}, [setToastMessage]);
3946
const closeToast = React.useCallback(() => setToastMessage(null), [setToastMessage]);
4047

4148
const context = React.useMemo(() => ({
@@ -48,9 +55,7 @@ export const ToastProvider = (props: ToastProviderProps) => {
4855
<ToastContext.Provider value={context}>
4956
{props.children}
5057
{ toastMessage && (
51-
<Toast show={toastMessage !== null} onClose={closeToast}>
52-
{toastMessage}
53-
</Toast>
58+
<ProcessingNotification isShow={toastMessage !== null} title={toastMessage} />
5459
)}
5560
</ToastContext.Provider>
5661
);

0 commit comments

Comments
 (0)