Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

142 строки
4.5 KiB

  1. import {Component, OnDestroy, OnInit} from '@angular/core';
  2. import {MessageService} from '../../services/message.service';
  3. import {Subscription} from 'rxjs';
  4. @Component({
  5. selector: 'app-message',
  6. templateUrl: './message.component.html',
  7. styleUrls: ['./message.component.scss']
  8. })
  9. export class MessageComponent implements OnInit, OnDestroy {
  10. // Message display time in milliseconds
  11. static MESSAGE_DISPLAY_TIME = 2000;
  12. // Client Error codes
  13. static CLIENT_ERROR_DATA = 1;
  14. static CLIENT_ERROR_VALIDATION_FAILED = 2;
  15. private messageSub: Subscription;
  16. public showError: boolean;
  17. public showSuccess: boolean;
  18. public showInfo: boolean;
  19. public showClientError: boolean;
  20. public message: string;
  21. public optionalInfo: string;
  22. public errorMessages: string[];
  23. public successMessages: string[];
  24. public infoMessages: string[];
  25. public clientErrorMessages: string[];
  26. constructor(private messageService: MessageService) {
  27. // Init client error messages
  28. this.clientErrorMessages = [];
  29. this.clientErrorMessages[MessageComponent.CLIENT_ERROR_DATA] = 'Die Daten sind unvollständig oder fehlerhaft.';
  30. this.clientErrorMessages[MessageComponent.CLIENT_ERROR_VALIDATION_FAILED] = 'Die Daten sind nicht valide, kein Speichern möglich. Bitte überprüfen Sie Ihre Eingaben.';
  31. }
  32. ngOnInit() {
  33. this.messageSub = this.messageService.getMessage$().subscribe(
  34. data => {
  35. if (data !== null) {
  36. if (data.error_code != 0) {
  37. this.showErrorMessage(data.error_code, data.error_msg, data.optional_info);
  38. } else if (data.success_code != 0) {
  39. this.showSuccessMessage(data.success_code, data.success_msg);
  40. } else if (data.info_code != 0) {
  41. this.showInfoMessage(data.info_code, data.info_msg);
  42. } else if (data.client_error_code != 0) {
  43. this.showClientErrorMessage(data.client_error_code);
  44. }
  45. }
  46. });
  47. this.showError = false;
  48. this.showSuccess = false;
  49. this.showInfo = false;
  50. this.showClientError = false;
  51. this.message = '';
  52. this.optionalInfo = null;
  53. }
  54. /**
  55. * Shows error message
  56. * @param {number} errorCode
  57. * @param {string} errorMsg
  58. * @param {string} optionalInfo
  59. */
  60. public showErrorMessage(errorCode: number, errorMsg: string, optionalInfo: string = null): void {
  61. this.showError = true;
  62. this.showSuccess = false;
  63. this.showInfo = false;
  64. this.showClientError = false;
  65. this.message = errorMsg;
  66. // Set optional info (if exists)
  67. this.optionalInfo = null !== optionalInfo ? optionalInfo : null;
  68. }
  69. /**
  70. * Shows success message
  71. * @param {number} successCode
  72. * @param {string} successMsg
  73. */
  74. public showSuccessMessage(successCode: number, successMsg: string): void {
  75. this.showSuccess = true;
  76. this.showError = false;
  77. this.showInfo = false;
  78. this.showClientError = false;
  79. const me: MessageComponent = this;
  80. this.message = successMsg;
  81. setTimeout(function() {
  82. me.closeComponent();
  83. }, MessageComponent.MESSAGE_DISPLAY_TIME);
  84. }
  85. /**
  86. * Shows info message
  87. * @param {number} infoCode
  88. * @param {string} infoMsg
  89. */
  90. public showInfoMessage(infoCode: number, infoMsg: string): void {
  91. this.showInfo = true;
  92. this.showSuccess = false;
  93. this.showError = false;
  94. this.showClientError = false;
  95. this.message = infoMsg;
  96. }
  97. /**
  98. * Shows client error message
  99. * @param {number} clientErrorCode
  100. */
  101. public showClientErrorMessage(clientErrorCode: number): void {
  102. this.showInfo = false;
  103. this.showSuccess = false;
  104. this.showError = false;
  105. this.showClientError = true;
  106. if (this.clientErrorMessages[clientErrorCode]) {
  107. this.message = this.clientErrorMessages[clientErrorCode];
  108. }
  109. }
  110. /**
  111. * Hides error- / success-message
  112. */
  113. public closeComponent(): void {
  114. this.showError = false;
  115. this.showSuccess = false;
  116. this.showInfo = false;
  117. this.showClientError = false;
  118. this.message = '';
  119. }
  120. ngOnDestroy(): void {
  121. if (this.messageSub !== null) {
  122. this.messageSub.unsubscribe();
  123. }
  124. }
  125. }