appNotification.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import _ from 'lodash';
  2. import { AppNotification, AppNotificationSeverity, AppNotificationTimeout } from 'app/types';
  3. const defaultSuccessNotification: AppNotification = {
  4. title: '',
  5. text: '',
  6. severity: AppNotificationSeverity.Success,
  7. icon: 'fa fa-check',
  8. timeout: AppNotificationTimeout.Success,
  9. };
  10. const defaultWarningNotification: AppNotification = {
  11. title: '',
  12. text: '',
  13. severity: AppNotificationSeverity.Warning,
  14. icon: 'fa fa-exclamation',
  15. timeout: AppNotificationTimeout.Warning,
  16. };
  17. const defaultErrorNotification: AppNotification = {
  18. title: '',
  19. text: '',
  20. severity: AppNotificationSeverity.Error,
  21. icon: 'fa fa-exclamation-triangle',
  22. timeout: AppNotificationTimeout.Error,
  23. };
  24. export const createSuccessNotification = (title: string, text?: string): AppNotification => ({
  25. ...defaultSuccessNotification,
  26. title: title,
  27. text: text,
  28. id: Date.now(),
  29. });
  30. export const createErrorNotification = (title: string, text?: any): AppNotification => {
  31. // Handling if text is an error object
  32. if (text && !_.isString(text)) {
  33. if (text.message) {
  34. text = text.message;
  35. } else if (text.data && text.data.message) {
  36. text = text.data.message;
  37. } else {
  38. text = text.toString();
  39. }
  40. }
  41. return {
  42. ...defaultErrorNotification,
  43. title: title,
  44. text: text,
  45. id: Date.now(),
  46. };
  47. };
  48. export const createWarningNotification = (title: string, text?: string): AppNotification => ({
  49. ...defaultWarningNotification,
  50. title: title,
  51. text: text,
  52. id: Date.now(),
  53. });