Angular upgrade
[osm/NG-UI.git] / src / app / vim-accounts / info-vim / InfoVimComponent.ts
index c82e9b6..42a3209 100644 (file)
 import { Component, Injector, OnInit } from '@angular/core';
 import { ActivatedRoute, Router } from '@angular/router';
 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
+import { TranslateService } from '@ngx-translate/core';
+import { NotifierService } from 'angular-notifier';
 import { ERRORDATA } from 'CommonModel';
 import { DataService } from 'DataService';
 import { environment } from 'environment';
 import * as HttpStatus from 'http-status-codes';
 import { RestService } from 'RestService';
-import { isNullOrUndefined } from 'util';
+import { isNullOrUndefined } from 'SharedService';
 import { CONFIG, VimAccountDetails, VIMData } from 'VimAccountModel';
 
 /**
@@ -82,12 +84,20 @@ export class InfoVimComponent implements OnInit {
   /** Utilizes modal service for any modal operations @private */
   private modalService: NgbModal;
 
+  /** Handle translate @public */
+  private translateService: TranslateService;
+
+  /** Notifier service to popup notification @private */
+  private notifierService: NotifierService;
+
   constructor(injector: Injector) {
     this.injector = injector;
     this.restService = this.injector.get(RestService);
     this.dataService = this.injector.get(DataService);
     this.activatedRoute = this.injector.get(ActivatedRoute);
     this.modalService = this.injector.get(NgbModal);
+    this.translateService = this.injector.get(TranslateService);
+    this.notifierService = this.injector.get(NotifierService);
     this.router = this.injector.get(Router);
   }
 
@@ -95,7 +105,6 @@ export class InfoVimComponent implements OnInit {
    * Lifecyle Hooks the trigger before component is instantiate
    */
   public ngOnInit(): void {
-    // tslint:disable-next-line:no-backbone-get-set-outside-model
     this.paramsID = this.activatedRoute.snapshot.paramMap.get('id');
     this.dataService.currentMessage.subscribe((data: VIMData) => {
       this.vimId = data.identifier;
@@ -116,28 +125,41 @@ export class InfoVimComponent implements OnInit {
     this.restService.getResource(environment.VIMACCOUNTS_URL + '/' + this.paramsID)
       .subscribe((vimAccountsData: VimAccountDetails) => {
         this.showDetails(vimAccountsData);
-        if (vimAccountsData.config.location !== undefined) {
-          const locationArr: string[] = vimAccountsData.config.location.split(',');
-          if (Array.isArray(locationArr)) {
-            vimAccountsData.config.location = locationArr[0];
+        if (!isNullOrUndefined(vimAccountsData.config)) {
+          if (vimAccountsData.config.location !== undefined) {
+            const locationArr: string[] = vimAccountsData.config.location.split(',');
+            if (Array.isArray(locationArr)) {
+              vimAccountsData.config.location = locationArr[0];
+            }
           }
+          Object.keys(vimAccountsData.config).forEach((key: string) => {
+            // eslint-disable-next-line security/detect-object-injection
+            if (Array.isArray(vimAccountsData.config[key]) || typeof vimAccountsData.config[key] === 'object') {
+              // eslint-disable-next-line security/detect-object-injection
+              vimAccountsData.config[key] = JSON.stringify(vimAccountsData.config[key]);
+            }
+            const keyArr: string[] = key.split('_');
+            if (keyArr.length > 1) {
+              // eslint-disable-next-line security/detect-object-injection
+              vimAccountsData.config[key.split('_').join(' ')] = vimAccountsData.config[key];
+              // eslint-disable-next-line @typescript-eslint/no-dynamic-delete, security/detect-object-injection
+              delete vimAccountsData.config[key];
+            }
+          });
+          this.configParams = vimAccountsData.config;
         }
-        Object.keys(vimAccountsData.config).forEach((key: string) => {
-          if (Array.isArray(vimAccountsData.config[key]) || typeof vimAccountsData.config[key] === 'object') {
-            vimAccountsData.config[key] = JSON.stringify(vimAccountsData.config[key]);
-          }
-          const keyArr: string[] = key.split('_');
-          if (keyArr.length > 1 ) {
-            vimAccountsData.config[key.split('_').join(' ')] = vimAccountsData.config[key];
-            delete vimAccountsData.config[key];
-          }
-        });
-        this.configParams = vimAccountsData.config;
         this.isLoadingResults = false;
       }, (error: ERRORDATA) => {
         this.isLoadingResults = false;
-        if (error.error.status === HttpStatus.NOT_FOUND || error.error.status === HttpStatus.UNAUTHORIZED) {
-          this.router.navigateByUrl('404', { skipLocationChange: true }).catch();
+        if (error.error.status === HttpStatus.NOT_FOUND) {
+          this.router.navigateByUrl('404', { skipLocationChange: true }).catch((): void => {
+            // Catch Navigation Error
+          });
+        } else if (error.error.status === HttpStatus.UNAUTHORIZED) {
+          this.notifierService.notify('error', this.translateService.instant('HTTPERROR.401'));
+          this.router.navigate(['/vim/details']).catch((): void => {
+            // Catch Navigation Error
+          });
         } else {
           this.restService.handleError(error, 'get');
         }