toastr.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import type { ToastContent, ToastOptions } from 'react-toastify';
  2. import { toast } from 'react-toastify';
  3. import { toArrayIfNot } from '~/utils/array-utils';
  4. export const toastErrorOption: ToastOptions = {
  5. autoClose: false,
  6. closeButton: true,
  7. };
  8. export const toastError = (err: string | Error | Error[], option: ToastOptions = toastErrorOption): void => {
  9. const errs = toArrayIfNot(err);
  10. if (errs.length === 0) {
  11. return;
  12. }
  13. for (const err of errs) {
  14. const message = (typeof err === 'string') ? err : err.message;
  15. toast.error(message, option);
  16. }
  17. };
  18. export const toastSuccessOption: ToastOptions = {
  19. autoClose: 2000,
  20. closeButton: true,
  21. };
  22. export const toastSuccess = (content: ToastContent, option: ToastOptions = toastSuccessOption): void => {
  23. toast.success(content, option);
  24. };
  25. export const toastWarningOption: ToastOptions = {
  26. autoClose: 5000,
  27. closeButton: true,
  28. };
  29. export const toastWarning = (content: ToastContent, option: ToastOptions = toastWarningOption): void => {
  30. toast.warning(content, option);
  31. };