|
| 1 | +import { |
| 2 | + toast, |
| 3 | + ToastContent, |
| 4 | + ToastId, |
| 5 | + ToastOptions, |
| 6 | + ToastPosition, |
| 7 | +} from "react-toastify"; |
| 8 | +import { ToastTemplates } from "../atoms/toasts/toast-templates"; |
| 9 | + |
| 10 | +const defaultToastOptions: ToastOptions = { |
| 11 | + draggable: false, |
| 12 | + position: ToastPosition.BOTTOM_RIGHT, |
| 13 | + autoClose: 3000, |
| 14 | + closeOnClick: true, |
| 15 | + hideProgressBar: false, |
| 16 | + pauseOnHover: true, |
| 17 | + pauseOnFocusLoss: true, |
| 18 | +}; |
| 19 | + |
| 20 | +const mergeDefaults = (...options: ToastOptions[]): ToastOptions => |
| 21 | + Object.assign({}, defaultToastOptions, ...options); |
| 22 | + |
| 23 | +class ToastManager { |
| 24 | + /** |
| 25 | + * Dismiss an existing toast programatically. |
| 26 | + * @param toastId the ID returned by the method which created the toast |
| 27 | + */ |
| 28 | + static dismiss(toastId: ToastId): void { |
| 29 | + toast.dismiss(toastId); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Dismiss all toasts programatically. |
| 34 | + */ |
| 35 | + static dismissAll(): void { |
| 36 | + toast.dismiss(); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Show an error style toast |
| 41 | + * @param content either a string or a TSX element |
| 42 | + * @param options optionally override default toast options |
| 43 | + */ |
| 44 | + static error( |
| 45 | + content: string | ToastContent, |
| 46 | + options: ToastOptions = defaultToastOptions |
| 47 | + ): ToastId { |
| 48 | + return toast.error( |
| 49 | + ToastTemplates.error(content), |
| 50 | + mergeDefaults({ autoClose: 5000 }, options) // increase default timeout for error toasts |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Show an info style toast |
| 56 | + * @param content either a string or a TSX element |
| 57 | + * @param options optionally override default toast options |
| 58 | + */ |
| 59 | + static info( |
| 60 | + content: string | ToastContent, |
| 61 | + options: ToastOptions = defaultToastOptions |
| 62 | + ): ToastId { |
| 63 | + return toast.info(ToastTemplates.info(content), mergeDefaults(options)); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Show a success style toast |
| 68 | + * @param content either a string or a TSX element |
| 69 | + * @param options optionally override default toast options |
| 70 | + */ |
| 71 | + static success( |
| 72 | + content: string | ToastContent, |
| 73 | + options: ToastOptions = defaultToastOptions |
| 74 | + ): ToastId { |
| 75 | + return toast.success( |
| 76 | + ToastTemplates.success(content), |
| 77 | + mergeDefaults(options) |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Update an existing toast with new content; this could be useful for |
| 83 | + * progress indicators, network state indicators, etc. |
| 84 | + * @param toastId the ID of the toast to update (returned from the method that created it) |
| 85 | + * @param newContent the new content to render into the toast |
| 86 | + */ |
| 87 | + static update(toastId: ToastId, newContent: string | ToastContent): void { |
| 88 | + toast.update(toastId, { render: newContent }); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Show a warning style toast |
| 93 | + * @param content either a string or a TSX element |
| 94 | + * @param options optionally override default toast options |
| 95 | + */ |
| 96 | + static warn( |
| 97 | + content: string | ToastContent, |
| 98 | + options: ToastOptions = defaultToastOptions |
| 99 | + ): ToastId { |
| 100 | + return toast.warn( |
| 101 | + ToastTemplates.warning(content), |
| 102 | + mergeDefaults({ autoClose: 5000 }, options) // increase default timeout for warning toasts |
| 103 | + ); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +export { ToastManager }; |
0 commit comments