app.component.ts 7.71 KB
import { Component, OnInit } from '@angular/core';
import { UserService } from './components/userentity/user.service';
import { ManageDiscountCodeService } from './components/managediscountcode/managediscountcode.service';
import { SubscriptionPriceService } from './components/subscriptionprice/subscriptionprice.service';
import { LicenseService } from './components/licenseentity/license.service';
import { ReportService } from './components/reports/report.service';
import { UserManageRightsModel } from './components/userentity/datamodel';
//import { MyAuthService } from './shared/my-auth.service';
import { GlobalService } from './shared/global';
import { Router, NavigationEnd } from '@angular/router';
import { LoadingService } from './shared/loading.service'; 
import {Idle, DEFAULT_INTERRUPTSOURCES} from '@ng-idle/core';
import {Keepalive} from '@ng-idle/keepalive';
import { Title } from '@angular/platform-browser';
import { ConfirmService } from './shared/confirm/confirm.service';
import { timeout } from 'rxjs/operator/timeout';

//import { HttpClient } from '@angular/common/http';
//import { HttpErrorResponse } from '@angular/common/http';
//import { Observable } from 'rxjs/Observable';
@Component({
  selector: 'app-component',
  templateUrl: '../app/app.component.html',
  providers: [UserService, ManageDiscountCodeService, SubscriptionPriceService, LicenseService, ReportService]
})

export class AppComponent implements OnInit {

  public obj: any;
  public objMenu: Array<any>=[];
  public objMenuGernal: any;
  public menustaus: string;
  public submenustaus: string;
  public UpdateProfileVisible: boolean;
  public UserManageRightsList: Array<UserManageRightsModel>;

  public LicenseTypeId:any;
 
  constructor(private idle: Idle, private keepalive: Keepalive,private titleService: Title,private _confirmService: ConfirmService,private userservice: UserService,private _loadingService: LoadingService, public global: GlobalService, private router: Router,) {     
    const projectTitle= this.titleService.getTitle();
   
      //console.log("idleTime: "+this.global.aiaIdleTime+" aiaIdleTimeOut: "+this.global.aiaIdleTimeOut+" Interval: "+this.global.aiaPingInterval);
      // sets an idle timeout of 20 minutes.
      this.idle.setIdle(this.global.aiaIdleTime);
     
      // sets a timeout period of 30 seconds. after 30 seconds of inactivity, the user will be considered timed out.
      this.idle.setTimeout(this.global.aiaIdleTimeOut);

       // sets the ping interval to 10 seconds
       this.keepalive.interval(this.global.aiaPingInterval);
      
      // sets the default interrupts, in this case, things like clicks, scrolls,mousemove touches to the document
      this.idle.setInterrupts(DEFAULT_INTERRUPTSOURCES);

      this.idle.onInterrupt.subscribe(() => {
        this.titleService.setTitle(projectTitle);
      })

      this.idle.onTimeout.subscribe(() => {
        //console.log("Timed out!");
        this.titleService.setTitle('Your session has expired!');
        this._loadingService.ShowLoading("global-loading");
        this.loginManageStatus('logout'); 
      });

      this.idle.onTimeoutWarning.subscribe((countdown) => {
       // console.log("You will time out in "+countdown);
        var minute=Math.floor(countdown/60);
        var remaining = minute+':'+(("00" + (countdown - minute * 60)).slice(-2) );         
        this.titleService.setTitle(remaining+' until your session times out!');
      });     
   
      this.keepalive.onPing.subscribe(() =>{
       // this.lastPing = new Date();
       // console.log("last ping: "+this.lastPing);   
       this.loginManageStatus('update');  
      });

      // this.idle.onIdleEnd.subscribe(() => {
      //   console.log("No longer idle. ");
      // });
      // this.idle.onIdleStart.subscribe(() =>{
      //   console.log("You\'ve gone idle!");
      // }); 

      if(window.location.host=="localhost:4200")
        {
          // for 'ng serve --open' command
         //**** for localhost:4200 *****//
          //insert new session
          this.loginManageStatus('insert');
        }
      this.idle.watch();  
    
  }
  
  ngOnInit(): void {
    this.menustaus = "True";
    this.global.getJSON().subscribe(data => {
      this.obj = data["AdminSections"] ;
      this.LicenseTypeId=this.global.LicenseTypeId;   
      if(this.LicenseTypeId !=undefined && this.LicenseTypeId!=0 && this.global.UserTypeName=="Client Admin") 
      {
        if(this.obj[2].UserType=="Client Admin")
        {
          var mainsection=this.obj[2];
          var clientadminSection=mainsection.HeaderMenu[4];
          var siteUageReportSection=clientadminSection.SubMenu[6];
          var concurrentLicenseId=siteUageReportSection.LicenseTypeId;

          //do not show this report for concurrent license of client admin        
          if(concurrentLicenseId ==this.LicenseTypeId)
          {
            siteUageReportSection.SubMenuStatus="False";
          }
        }
      } 


      for (let i = 0; i < 7; i++) {
        if (this.obj[i].Id == this.global.UserType) {
          if (this.global.UserTypeName == "General Admin") {
            
            this.userservice.GetGeneralAdminMenu({
              UserId: this.global.UserId,
              UserType: this.global.UserTypeName
            }).subscribe(x => {
              this.UserManageRightsList = x;
              this.objMenuGernal = this.obj[i].HeaderMenu;
              //this.objMenuGernal = this.objMenu;
              //this.objMenu='';
              for (var imenu = 0; imenu < this.objMenuGernal.length; imenu++) {
                for (var k = 0; k < this.UserManageRightsList.length; k++) {
                  if (this.objMenuGernal[imenu].HeaderMenuName == this.UserManageRightsList[k].Title) {
                   // this.global.objMenu.push(this.objMenuGernal[imenu]);
                    this.objMenu.push(this.objMenuGernal[imenu]);
                    break;
                  }
                }
              }            
              this.router.navigate(['/updateuserprofile']);
              }, error => {
               
              });
            
            
          }
          else {
            this.objMenu = this.obj[i].HeaderMenu;
            this.router.navigate(['/updateuserprofile']);
          }
        }
      }
    }, error => console.log(error));
  }

  logout() {
    this._loadingService.ShowLoading("global-loading");
    this.loginManageStatus('logout');     
  }
  loginManageStatus(tagname:string) {
    this.userservice.ManageUserLoginStatus({
      userId: this.global.UserId,
      tagName: tagname,
      SessionId:this.global.SessionId,
      isSiteUser:this.global.isSiteUser,
      isAdmin:this.global.isAdmin
    }).subscribe(status => { 
      //console.log(status); 
      if(status=='False')
      {
        if(tagname=='logout')
        {
          this._loadingService.HideLoading("global-loading");
        }
       
         if(window.location.host!="localhost:4200")
          {
            localStorage.removeItem('loggedInUserDetails');    
            window.location.href = window.location.origin;

          }
         
      }
     
    },error =>{
      console.log(error); 
      this.idle.stop();
      this._confirmService.activate(error, "alertMsg");
      this._loadingService.HideLoading("global-loading");
      setTimeout(function(){
        window.location.href = window.location.origin;
      },5000);
    })   
  }

  Product() {
    //window.location.href = this.global.LiveURL;
    window.location.href = window.location.origin;
    //this.router.navigate([this.global.LiveURL]);
  }
  
  changeOfRoutes()
  {
    //used to hide nevigation on ipad device while nevigate to page
    
    $('#navbar').removeClass('navbar-collapse collapse in');
    $('#navbar').addClass('navbar-collapse collapse');
   
  }
  
}