Project

General

Profile

1
import {Component, Input} from '@angular/core';
2
import {ErrorCodes} from './properties/errorCodes';
3

    
4
@Component({
5
    selector: 'errorMessages',
6
    template: `
7
      <div *ngIf="status.some(checkErrorCode(errorCodes.LOADING))"
8
            [class]="(tab_error_class ? '' : 'uk-animation-fade') + ' uk-margin-top  uk-width-1-1'" role="alert"><span class="loading-gif  uk-align-center" ></span></div>
9
      <div *ngIf="status.every(checkErrorCode(errorCodes.NONE))"
10
            [class]="(tab_error_class ? 'uk-margin-top' : 'uk-animation-fade') + ' uk-alert uk-alert-primary'" role="alert">No {{type}} available</div>
11
      <div *ngIf="status.every(checkErrorCode(errorCodes.ERROR)) ||
12
                  (status.some(checkErrorCode(errorCodes.ERROR)) && (!status.every(checkErrorCode(errorCodes.DONE)) || !status.every(checkErrorCode(errorCodes.LOADING))))"
13
            [class]="(tab_error_class ? 'uk-margin-top' : 'uk-animation-fade') + ' uk-alert uk-alert-warning'" role="alert">
14
        An Error Occurred
15
      </div>
16
      <div *ngIf="status.every(checkErrorCode(errorCodes.NOT_AVAILABLE)) ||
17
                  (status.some(checkErrorCode(errorCodes.NOT_AVAILABLE)) && (!status.every(checkErrorCode(errorCodes.DONE)) || !status.every(checkErrorCode(errorCodes.LOADING))))"
18
            [class]="(tab_error_class ? 'uk-margin-top' : 'uk-animation-fade') + ' uk-alert uk-alert-warning'" role="alert">
19
        Service temporarily unavailable. Please try again later.
20
      </div>
21
      <div *ngIf="status.every(checkErrorCode(errorCodes.NOT_FOUND)) ||
22
                  (status.some(checkErrorCode(errorCodes.NOT_FOUND)) && (!status.every(checkErrorCode(errorCodes.DONE)) || !status.every(checkErrorCode(errorCodes.LOADING))))"
23
            [class]="(tab_error_class ? 'uk-margin-top' : 'uk-animation-fade') + ' uk-alert uk-alert-warning'" role="alert">
24
        No {{type}} found
25
      </div>
26
      <div *ngIf="status.every(checkErrorCode(errorCodes.OUT_OF_BOUND))"
27
            [class]="(tab_error_class ? 'uk-margin-top' : 'uk-animation-fade') + ' uk-alert uk-alert-warning'" role="alert">Requested page out of bounds
28
      </div>
29
      <div *ngIf="status.some(checkErrorCode(errorCodes.NOT_SAVED))"
30
            [class]="(tab_error_class ? 'uk-margin-top' : 'uk-animation-fade') + ' uk-alert uk-alert-warning'" role="alert">Changes could not be saved
31
      </div>
32
      <div *ngIf="status.some(checkErrorCode(errorCodes.FORBIDDEN))"
33
            [class]="(tab_error_class ? 'uk-margin-top' : 'uk-animation-fade') + ' uk-alert uk-alert-danger'" role="alert">You are not allowed to access this page
34
      </div>
35
    `
36
  })
37

    
38
  export class ErrorMessagesComponent {
39
      @Input() status: Array<number>;
40
      @Input() type: string;
41
      @Input() tab_error_class: boolean = false;
42

    
43
      public errorCodes:ErrorCodes;
44

    
45
      constructor () {
46
        this.errorCodes = new ErrorCodes();
47
      }
48

    
49
      ngOnInit() {
50
          if(!this.status) {
51
            this.status = [this.errorCodes.LOADING];
52
          }
53
      }
54

    
55
      ngOnDestroy() {}
56

    
57
      public checkErrorCode(code: number) {
58
        return function(status: number) {
59
          return (status == code);
60
        }
61
      }
62

    
63
      public getErrorCode(status: any) {
64
        if(status == '401' || status == 401) {
65
          return this.errorCodes.FORBIDDEN;
66
        } else if(status == "403" || status == 403) {
67
          return this.errorCodes.FORBIDDEN;
68
        } else if(status == "204" || status == 204) {
69
          return this.errorCodes.NONE;
70
        } else if(status == '404' || status == 404) {
71
          return this.errorCodes.NOT_FOUND;
72
        } else if(status == '500'|| status == 500) {
73
          return this.errorCodes.ERROR;
74
        } else {
75
          return this.errorCodes.NOT_AVAILABLE;
76
        }
77
      }
78
  }
(6-6/18)