apiNotification.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // show API error/sucess toastr
  2. import * as toastr from 'toastr';
  3. import { toArrayIfNot } from '~/utils/array-utils';
  4. const toastrOption = {
  5. error: {
  6. closeButton: true,
  7. progressBar: true,
  8. newestOnTop: false,
  9. showDuration: '100',
  10. hideDuration: '100',
  11. timeOut: '0',
  12. },
  13. success: {
  14. closeButton: true,
  15. progressBar: true,
  16. newestOnTop: false,
  17. showDuration: '100',
  18. hideDuration: '100',
  19. timeOut: '3000',
  20. },
  21. warning: {
  22. closeButton: true,
  23. progressBar: true,
  24. newestOnTop: false,
  25. showDuration: '100',
  26. hideDuration: '100',
  27. timeOut: '6000',
  28. },
  29. };
  30. // accepts both a single error and an array of errors
  31. export const toastError = (err, header = 'Error', option = toastrOption.error) => {
  32. const errs = toArrayIfNot(err);
  33. if (err.length === 0) {
  34. toastr.error('', header);
  35. }
  36. for (const err of errs) {
  37. toastr.error(err.message || err, header, option);
  38. }
  39. };
  40. // only accepts a single item
  41. export const toastSuccess = (body, header = 'Success', option = toastrOption.success) => {
  42. toastr.success(body, header, option);
  43. };
  44. export const toastWarning = (body, header = 'Warning', option = toastrOption.warning) => {
  45. toastr.warning(body, header, option);
  46. };