Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change toast component [FC-0059] #1211

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/generic/processing-notification/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { Badge, Icon } from '@openedx/paragon';
import { Settings as IconSettings } from '@openedx/paragon/icons';
import { capitalize } from 'lodash';

import { NOTIFICATION_MESSAGES } from '../../constants';

const ProcessingNotification = ({ isShow, title }) => (
<Badge
className={classNames('processing-notification', {
Expand All @@ -24,7 +22,7 @@ const ProcessingNotification = ({ isShow, title }) => (

ProcessingNotification.propTypes = {
isShow: PropTypes.bool.isRequired,
title: PropTypes.oneOf(Object.values(NOTIFICATION_MESSAGES)).isRequired,
title: PropTypes.string.isRequired,
};

export default ProcessingNotification;
8 changes: 8 additions & 0 deletions src/generic/toast-context/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe('<ToastProvider />', () => {
},
});
store = initializeStore();
jest.useFakeTimers();
});

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

it('should close toast after 5000ms', async () => {
render(<RootWrapper><TestComponentToShow /></RootWrapper>);
expect(await screen.findByText('This is the toast!')).toBeInTheDocument();
jest.advanceTimersByTime(6000);
expect(screen.queryByText('This is the toast!')).not.toBeInTheDocument();
});

it('should close toast', async () => {
render(<RootWrapper><TestComponentToClose /></RootWrapper>);
expect(await screen.findByText('Content')).toBeInTheDocument();
Expand Down
15 changes: 10 additions & 5 deletions src/generic/toast-context/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { Toast } from '@openedx/paragon';

import ProcessingNotification from '../processing-notification';

export interface ToastContextData {
toastMessage: string | null;
Expand Down Expand Up @@ -35,7 +36,13 @@ export const ToastProvider = (props: ToastProviderProps) => {
setToastMessage(null);
}, []);

const showToast = React.useCallback((message) => setToastMessage(message), [setToastMessage]);
const showToast = React.useCallback((message) => {
setToastMessage(message);
// Close the toast after 5 seconds
setTimeout(() => {
setToastMessage(null);
}, 5000);
}, [setToastMessage]);
const closeToast = React.useCallback(() => setToastMessage(null), [setToastMessage]);

const context = React.useMemo(() => ({
Expand All @@ -48,9 +55,7 @@ export const ToastProvider = (props: ToastProviderProps) => {
<ToastContext.Provider value={context}>
{props.children}
{ toastMessage && (
<Toast show={toastMessage !== null} onClose={closeToast}>
{toastMessage}
</Toast>
<ProcessingNotification isShow={toastMessage !== null} title={toastMessage} />
)}
</ToastContext.Provider>
);
Expand Down
Loading